Skip to content

Commit

Permalink
fix pair codec
Browse files Browse the repository at this point in the history
  • Loading branch information
cool-develope committed Nov 5, 2024
1 parent 4a272ee commit 8a911e3
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 29 deletions.
4 changes: 2 additions & 2 deletions collections/codec/indexing.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func ValueSchemaCodec[V any](cdc ValueCodec[V]) (SchemaCodec[V], error) {

// FallbackSchemaCodec returns a fallback schema codec for T when one isn't explicitly
// specified with HasSchemaCodec. It maps all simple types directly to schema kinds
// and converts everything else to String.
// and converts everything else to JSON String.
func FallbackSchemaCodec[T any]() SchemaCodec[T] {
var t T
kind := schema.KindForGoValue(t)
Expand All @@ -81,7 +81,7 @@ func FallbackSchemaCodec[T any]() SchemaCodec[T] {
FromSchemaType: nil,
}
} else {
// we default to encoding everything to String
// we default to encoding everything to JSON String
return SchemaCodec[T]{
Fields: []schema.Field{{Kind: schema.StringKind}},
ToSchemaType: func(t T) (any, error) {
Expand Down
17 changes: 15 additions & 2 deletions collections/indexes/multi.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,26 @@ func NewMulti[ReferenceKey, PrimaryKey, Value any](
if o.uncheckedValue {
return &Multi[ReferenceKey, PrimaryKey, Value]{
getRefKey: getRefKeyFunc,
refKeys: collections.NewKeySet(schema, prefix, name, collections.PairKeyCodec(refCodec, pkCodec), collections.WithKeySetUncheckedValue()),
refKeys: collections.NewKeySet(
schema,
prefix,
name,
collections.PairKeyCodec(refCodec, pkCodec),
collections.WithKeySetUncheckedValue(),
collections.WithKeySetSecondaryIndex(),
),
}
}

return &Multi[ReferenceKey, PrimaryKey, Value]{
getRefKey: getRefKeyFunc,
refKeys: collections.NewKeySet(schema, prefix, name, collections.PairKeyCodec(refCodec, pkCodec)),
refKeys: collections.NewKeySet(
schema,
prefix,
name,
collections.PairKeyCodec(refCodec, pkCodec),
collections.WithKeySetSecondaryIndex(),
),
}
}

Expand Down
17 changes: 15 additions & 2 deletions collections/indexes/reverse_pair.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,25 @@ func NewReversePair[Value, K1, K2 any](
}
if o.uncheckedValue {
return &ReversePair[K1, K2, Value]{
refKeys: collections.NewKeySet(sb, prefix, name, collections.PairKeyCodec(pkc.KeyCodec2(), pkc.KeyCodec1()), collections.WithKeySetUncheckedValue(), collections.WithKeySetSecondaryIndex()),
refKeys: collections.NewKeySet(
sb,
prefix,
name,
collections.PairKeyCodec(pkc.KeyCodec2(), pkc.KeyCodec1()),
collections.WithKeySetUncheckedValue(),
collections.WithKeySetSecondaryIndex(),
),
}
}

mi := &ReversePair[K1, K2, Value]{
refKeys: collections.NewKeySet(sb, prefix, name, collections.PairKeyCodec(pkc.KeyCodec2(), pkc.KeyCodec1()), collections.WithKeySetSecondaryIndex()),
refKeys: collections.NewKeySet(
sb,
prefix,
name,
collections.PairKeyCodec(pkc.KeyCodec2(), pkc.KeyCodec1()),
collections.WithKeySetSecondaryIndex(),
),
}

return mi
Expand Down
15 changes: 10 additions & 5 deletions collections/pair.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@ func (p Pair[K1, K2]) K2() (k2 K2) {
return *p.key2
}

// Keys returns key1 and key2 as a slice.
func (p Pair[K1, K2]) Keys() []interface{} {
return []interface{}{p.K1(), p.K2()}
}

// Join creates a new Pair instance composed of the two provided keys, in order.
func Join[K1, K2 any](key1 K1, key2 K2) Pair[K1, K2] {
return Pair[K1, K2]{
Expand Down Expand Up @@ -252,6 +247,16 @@ func (p pairKeyCodec[K1, K2]) SchemaCodec() (codec.SchemaCodec[Pair[K1, K2]], er

return codec.SchemaCodec[Pair[K1, K2]]{
Fields: []schema.Field{field1, field2},
ToSchemaType: func(pair Pair[K1, K2]) (any, error) {
return []interface{}{pair.K1(), pair.K2()}, nil
},
FromSchemaType: func(a any) (Pair[K1, K2], error) {
aSlice, ok := a.([]interface{})
if !ok || len(aSlice) != 2 {
return Pair[K1, K2]{}, fmt.Errorf("expected slice of length 2, got %T", a)
}
return Join(aSlice[0].(K1), aSlice[1].(K2)), nil
},
}, nil
}

Expand Down
15 changes: 10 additions & 5 deletions collections/quad.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,6 @@ func (t Quad[K1, K2, K3, K4]) K4() (x K4) {
return x
}

// Keys returns key1, key2, key3 and key4 as a slice.
func (t Quad[K1, K2, K3, K4]) Keys() []interface{} {
return []interface{}{t.K1(), t.K2(), t.K3(), t.K4()}
}

// QuadPrefix creates a new Quad instance composed only of the first part of the key.
func QuadPrefix[K1, K2, K3, K4 any](k1 K1) Quad[K1, K2, K3, K4] {
return Quad[K1, K2, K3, K4]{k1: &k1}
Expand Down Expand Up @@ -387,6 +382,16 @@ func (t quadKeyCodec[K1, K2, K3, K4]) SchemaCodec() (codec.SchemaCodec[Quad[K1,

return codec.SchemaCodec[Quad[K1, K2, K3, K4]]{
Fields: []schema.Field{field1, field2, field3, field4},
ToSchemaType: func(q Quad[K1, K2, K3, K4]) (any, error) {
return []interface{}{q.K1(), q.K2(), q.K3(), q.K4()}, nil
},
FromSchemaType: func(a any) (Quad[K1, K2, K3, K4], error) {
aSlice, ok := a.([]interface{})
if !ok || len(aSlice) != 4 {
return Quad[K1, K2, K3, K4]{}, fmt.Errorf("expected slice of length 4, got %T", a)
}
return Join4(aSlice[0].(K1), aSlice[1].(K2), aSlice[2].(K3), aSlice[3].(K4)), nil
},
}, nil
}

Expand Down
15 changes: 10 additions & 5 deletions collections/triple.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,6 @@ func (t Triple[K1, K2, K3]) K3() (x K3) {
return x
}

// Keys returns key1, key2 and key3 as a slice.
func (t Triple[K1, K2, K3]) Keys() []interface{} {
return []interface{}{t.K1(), t.K2(), t.K3()}
}

// TriplePrefix creates a new Triple instance composed only of the first part of the key.
func TriplePrefix[K1, K2, K3 any](k1 K1) Triple[K1, K2, K3] {
return Triple[K1, K2, K3]{k1: &k1}
Expand Down Expand Up @@ -316,6 +311,16 @@ func (t tripleKeyCodec[K1, K2, K3]) SchemaCodec() (codec.SchemaCodec[Triple[K1,

return codec.SchemaCodec[Triple[K1, K2, K3]]{
Fields: []schema.Field{field1, field2, field3},
ToSchemaType: func(t Triple[K1, K2, K3]) (any, error) {
return []interface{}{t.K1(), t.K2(), t.K3()}, nil
},
FromSchemaType: func(a any) (Triple[K1, K2, K3], error) {
aSlice, ok := a.([]interface{})
if !ok || len(aSlice) != 3 {
return Triple[K1, K2, K3]{}, fmt.Errorf("expected slice of length 3, got %T", a)
}
return Join3(aSlice[0].(K1), aSlice[1].(K2), aSlice[2].(K3)), nil
},
}, nil
}

Expand Down
8 changes: 0 additions & 8 deletions indexer/postgres/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@ import (
"cosmossdk.io/schema"
)

type keyCollection interface {
// Keys returns the key fields for the collection.
Keys() []interface{}
}

// bindKeyParams binds the key to the key columns.
func (tm *objectIndexer) bindKeyParams(key interface{}) ([]interface{}, []string, error) {
n := len(tm.typ.KeyFields)
Expand All @@ -22,9 +17,6 @@ func (tm *objectIndexer) bindKeyParams(key interface{}) ([]interface{}, []string
} else if n == 1 {
return tm.bindParams(tm.typ.KeyFields, []interface{}{key})
} else {
if kc, ok := key.(keyCollection); ok {
return tm.bindParams(tm.typ.KeyFields, kc.Keys())
}
key, ok := key.([]interface{})
if !ok {
return nil, nil, errors.New("expected key to be a slice")
Expand Down

0 comments on commit 8a911e3

Please sign in to comment.