From a54620375817950f3f466b83bb28fcf79f223be7 Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Sat, 30 Nov 2024 08:53:08 -0300 Subject: [PATCH] nip11: support hex nip numbers as strings. --- nip11/nip11_test.go | 5 +---- nip11/types.go | 14 +++++++------- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/nip11/nip11_test.go b/nip11/nip11_test.go index a3472a4..f2d8ced 100644 --- a/nip11/nip11_test.go +++ b/nip11/nip11_test.go @@ -25,10 +25,7 @@ func TestAddSupportedNIP(t *testing.T) { info.AddSupportedNIP(1) info.AddSupportedNIP(18) - for i, v := range []int{0, 1, 2, 12, 13, 17, 18, 19, 44} { - assert.Equal(t, v, info.SupportedNIPs[i], "expected info.SupportedNIPs[%d] to equal %v, got %v", - i, v, info.SupportedNIPs) - } + assert.Contains(t, info.SupportedNIPs, 0, 1, 2, 12, 13, 17, 18, 19, 44) } func TestFetch(t *testing.T) { diff --git a/nip11/types.go b/nip11/types.go index 7fa3873..d687668 100644 --- a/nip11/types.go +++ b/nip11/types.go @@ -1,6 +1,8 @@ package nip11 -import "slices" +import ( + "slices" +) type RelayInformationDocument struct { URL string `json:"-"` @@ -9,7 +11,7 @@ type RelayInformationDocument struct { Description string `json:"description"` PubKey string `json:"pubkey"` Contact string `json:"contact"` - SupportedNIPs []int `json:"supported_nips"` + SupportedNIPs []any `json:"supported_nips"` Software string `json:"software"` Version string `json:"version"` @@ -24,14 +26,12 @@ type RelayInformationDocument struct { } func (info *RelayInformationDocument) AddSupportedNIP(number int) { - idx, exists := slices.BinarySearch(info.SupportedNIPs, number) - if exists { + idx := slices.IndexFunc(info.SupportedNIPs, func(n any) bool { return n == number }) + if idx != -1 { return } - info.SupportedNIPs = append(info.SupportedNIPs, -1) - copy(info.SupportedNIPs[idx+1:], info.SupportedNIPs[idx:]) - info.SupportedNIPs[idx] = number + info.SupportedNIPs = append(info.SupportedNIPs, number) } type RelayLimitationDocument struct {