From 01125efdac5d9724750c1b8ba48796db321b61c8 Mon Sep 17 00:00:00 2001 From: Javad Date: Mon, 9 Jun 2025 09:42:42 +0330 Subject: [PATCH 1/3] feat: add json marshal/unmarshal support --- .idea/.gitignore | 8 ++++++++ .idea/ksuid.iml | 9 +++++++++ .idea/modules.xml | 8 ++++++++ .idea/vcs.xml | 6 ++++++ ksuid.go | 25 +++++++++++++++++++++++-- 5 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/ksuid.iml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/ksuid.iml b/.idea/ksuid.iml new file mode 100644 index 0000000..5e764c4 --- /dev/null +++ b/.idea/ksuid.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..062b9b2 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/ksuid.go b/ksuid.go index 79bbe56..8425736 100644 --- a/ksuid.go +++ b/ksuid.go @@ -5,6 +5,7 @@ import ( "crypto/rand" "database/sql/driver" "encoding/binary" + "encoding/json" "fmt" "io" "math" @@ -38,8 +39,9 @@ const ( ) // KSUIDs are 20 bytes: -// 00-03 byte: uint32 BE UTC timestamp with custom epoch -// 04-19 byte: random "payload" +// +// 00-03 byte: uint32 BE UTC timestamp with custom epoch +// 04-19 byte: random "payload" type KSUID [byteLength]byte var ( @@ -116,6 +118,10 @@ func (i KSUID) MarshalBinary() ([]byte, error) { return i.Bytes(), nil } +func (i KSUID) MarshalJSON() ([]byte, error) { + return json.Marshal(i.String()) +} + func (i *KSUID) UnmarshalText(b []byte) error { id, err := Parse(string(b)) if err != nil { @@ -134,6 +140,21 @@ func (i *KSUID) UnmarshalBinary(b []byte) error { return nil } +func (i *KSUID) UnmarshalJSON(data []byte) error { + var s string + if err := json.Unmarshal(data, &s); err != nil { + return err + } + + id, err := Parse(s) + if err != nil { + return fmt.Errorf("invalid KSUID: %w", err) + } + + *i = id + return nil +} + // Value converts the KSUID into a SQL driver value which can be used to // directly use the KSUID as parameter to a SQL query. func (i KSUID) Value() (driver.Value, error) { From 718c2d427a27da8c65376c70d82e5fc5df83c292 Mon Sep 17 00:00:00 2001 From: Javad Date: Mon, 9 Jun 2025 09:47:05 +0330 Subject: [PATCH 2/3] feat: add equal check two ksuid --- ksuid.go | 5 +++++ ksuid_test.go | 30 ++++++++++++++++++++++-------- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/ksuid.go b/ksuid.go index 8425736..d2a4909 100644 --- a/ksuid.go +++ b/ksuid.go @@ -104,6 +104,11 @@ func (i KSUID) Get() interface{} { return i } +// Equal compares two KSUIDs for equality. It returns true if the two KSUIDs +func (i KSUID) Equal(k2 KSUID) bool { + return bytes.Equal(i[:], k2[:]) +} + // Set satisfies the flag.Value interface, making it possible to use KSUIDs as // part of of the command line options of a program. func (i *KSUID) Set(s string) error { diff --git a/ksuid_test.go b/ksuid_test.go index 3620187..bbf448f 100644 --- a/ksuid_test.go +++ b/ksuid_test.go @@ -2,7 +2,6 @@ package ksuid import ( "bytes" - "encoding/json" "flag" "fmt" "sort" @@ -122,6 +121,20 @@ func TestEncodeAndDecode(t *testing.T) { } } +func TestEqual(t *testing.T) { + x := New() + y := x + + if !x.Equal(y) { + t.Error(x, "!=", y) + } + + z := New() + if x.Equal(z) { + t.Error(x, "==", z) + } +} + func TestMarshalText(t *testing.T) { var id1 = New() var id2 KSUID @@ -164,12 +177,13 @@ func TestMashalJSON(t *testing.T) { var id1 = New() var id2 KSUID - if b, err := json.Marshal(id1); err != nil { - t.Fatal(err) - } else if err := json.Unmarshal(b, &id2); err != nil { - t.Fatal(err) - } else if id1 != id2 { - t.Error(id1, "!=", id2) + b, err := id1.MarshalJSON() + if err != nil { + t.Error("unexpected error on marshal:", err) + } + + if err := id2.UnmarshalJSON(b); err != nil { + t.Error("failed to unmarshal JSON:", err) } } @@ -309,7 +323,7 @@ func TestGetTimestamp(t *testing.T) { x, _ := NewRandomWithTime(nowTime) xTime := int64(x.Timestamp()) unix := nowTime.Unix() - if xTime != unix - epochStamp { + if xTime != unix-epochStamp { t.Fatal(xTime, "!=", unix) } } From 45211267bfa4765e8c3008b2fb1a4538f24ee889 Mon Sep 17 00:00:00 2001 From: Javad Date: Mon, 9 Jun 2025 09:49:38 +0330 Subject: [PATCH 3/3] fix: ignored IDE configuration --- .gitignore | 3 +++ .idea/.gitignore | 8 -------- .idea/ksuid.iml | 9 --------- .idea/modules.xml | 8 -------- .idea/vcs.xml | 6 ------ 5 files changed, 3 insertions(+), 31 deletions(-) delete mode 100644 .idea/.gitignore delete mode 100644 .idea/ksuid.iml delete mode 100644 .idea/modules.xml delete mode 100644 .idea/vcs.xml diff --git a/.gitignore b/.gitignore index 4b7a3f3..e39ad0d 100644 --- a/.gitignore +++ b/.gitignore @@ -29,3 +29,6 @@ _testmain.go # govendor /vendor/*/ + +# ide +.idea diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 13566b8..0000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Editor-based HTTP Client requests -/httpRequests/ -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml diff --git a/.idea/ksuid.iml b/.idea/ksuid.iml deleted file mode 100644 index 5e764c4..0000000 --- a/.idea/ksuid.iml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index 062b9b2..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 35eb1dd..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file