From 8497dba94b05b856c24cb5eff5c75e69d1372085 Mon Sep 17 00:00:00 2001 From: xujk-byte Date: Thu, 10 Oct 2024 18:26:44 +0800 Subject: [PATCH] docs:(quad.go, triple.go) add comments for funcs --- collections/quad.go | 6 ++++++ collections/triple.go | 3 +++ 2 files changed, 9 insertions(+) diff --git a/collections/quad.go b/collections/quad.go index a5dfc2929b0..2e0678e69ea 100644 --- a/collections/quad.go +++ b/collections/quad.go @@ -106,6 +106,8 @@ type quadKeyCodec[K1, K2, K3, K4 any] struct { type jsonQuadKey [4]json.RawMessage + +// EncodeJSON encodes Quads to json func (t quadKeyCodec[K1, K2, K3, K4]) EncodeJSON(value Quad[K1, K2, K3, K4]) ([]byte, error) { json1, err := t.keyCodec1.EncodeJSON(*value.k1) if err != nil { @@ -130,6 +132,7 @@ func (t quadKeyCodec[K1, K2, K3, K4]) EncodeJSON(value Quad[K1, K2, K3, K4]) ([] return json.Marshal(jsonQuadKey{json1, json2, json3, json4}) } +// DecodeJSON decodes json to Quads func (t quadKeyCodec[K1, K2, K3, K4]) DecodeJSON(b []byte) (Quad[K1, K2, K3, K4], error) { var jsonKey jsonQuadKey err := json.Unmarshal(b, &jsonKey) @@ -160,6 +163,7 @@ func (t quadKeyCodec[K1, K2, K3, K4]) DecodeJSON(b []byte) (Quad[K1, K2, K3, K4] return Join4(key1, key2, key3, key4), nil } +// Stringify converts Quads to string func (t quadKeyCodec[K1, K2, K3, K4]) Stringify(key Quad[K1, K2, K3, K4]) string { b := new(strings.Builder) b.WriteByte('(') @@ -206,6 +210,7 @@ func (t quadKeyCodec[K1, K2, K3, K4]) KeyType() string { return fmt.Sprintf("Quad[%s,%s,%s,%s]", t.keyCodec1.KeyType(), t.keyCodec2.KeyType(), t.keyCodec3.KeyType(), t.keyCodec4.KeyType()) } + func (t quadKeyCodec[K1, K2, K3, K4]) Encode(buffer []byte, key Quad[K1, K2, K3, K4]) (int, error) { writtenTotal := 0 if key.k1 != nil { @@ -239,6 +244,7 @@ func (t quadKeyCodec[K1, K2, K3, K4]) Encode(buffer []byte, key Quad[K1, K2, K3, return writtenTotal, nil } + func (t quadKeyCodec[K1, K2, K3, K4]) Decode(buffer []byte) (int, Quad[K1, K2, K3, K4], error) { readTotal := 0 read, key1, err := t.keyCodec1.DecodeNonTerminal(buffer) diff --git a/collections/triple.go b/collections/triple.go index e4a07970945..17f519d54d6 100644 --- a/collections/triple.go +++ b/collections/triple.go @@ -85,6 +85,7 @@ type tripleKeyCodec[K1, K2, K3 any] struct { type jsonTripleKey [3]json.RawMessage +// EncodeJSON convert triple keys to json func (t tripleKeyCodec[K1, K2, K3]) EncodeJSON(value Triple[K1, K2, K3]) ([]byte, error) { json1, err := t.keyCodec1.EncodeJSON(*value.k1) if err != nil { @@ -104,6 +105,7 @@ func (t tripleKeyCodec[K1, K2, K3]) EncodeJSON(value Triple[K1, K2, K3]) ([]byte return json.Marshal(jsonTripleKey{json1, json2, json3}) } +// DecodeJSON convert json to triple keys func (t tripleKeyCodec[K1, K2, K3]) DecodeJSON(b []byte) (Triple[K1, K2, K3], error) { var jsonKey jsonTripleKey err := json.Unmarshal(b, &jsonKey) @@ -129,6 +131,7 @@ func (t tripleKeyCodec[K1, K2, K3]) DecodeJSON(b []byte) (Triple[K1, K2, K3], er return Join3(key1, key2, key3), nil } +// Stringify convert triple keys to string func (t tripleKeyCodec[K1, K2, K3]) Stringify(key Triple[K1, K2, K3]) string { b := new(strings.Builder) b.WriteByte('(')