Skip to content

Commit

Permalink
docs:(quad.go, triple.go) add comments for funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
xujk-byte committed Oct 10, 2024
1 parent 04c1590 commit 8497dba
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
6 changes: 6 additions & 0 deletions collections/quad.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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('(')
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions collections/triple.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand All @@ -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('(')
Expand Down

0 comments on commit 8497dba

Please sign in to comment.