Skip to content

Commit

Permalink
postgres indexer fix
Browse files Browse the repository at this point in the history
  • Loading branch information
cool-develope committed Nov 4, 2024
1 parent b6c6da7 commit b1ccaf9
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
12 changes: 6 additions & 6 deletions collections/codec/indexing.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,20 +81,20 @@ func FallbackSchemaCodec[T any]() SchemaCodec[T] {
FromSchemaType: nil,
}
} else {
// we default to encoding everything to JSON
// we default to encoding everything to String
return SchemaCodec[T]{
Fields: []schema.Field{{Kind: schema.JSONKind}},
Fields: []schema.Field{{Kind: schema.StringKind}},
ToSchemaType: func(t T) (any, error) {
bz, err := json.Marshal(t)
return json.RawMessage(bz), err
return string(json.RawMessage(bz)), err
},
FromSchemaType: func(a any) (T, error) {
var t T
bz, ok := a.(json.RawMessage)
sz, ok := a.(string)
if !ok {
return t, fmt.Errorf("expected json.RawMessage, got %T", a)
return t, fmt.Errorf("expected string, got %T", a)
}
err := json.Unmarshal(bz, &t)
err := json.Unmarshal([]byte(sz), &t)
return t, err
},
}
Expand Down
3 changes: 3 additions & 0 deletions collections/indexing.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ func (c collectionImpl[K, V]) schemaCodec() (*collectionSchemaCodec, error) {
if err != nil {
return nil, err
}
if keyDecoder.ToSchemaType == nil {
return x, nil
}
return keyDecoder.ToSchemaType(x)
}
ensureFieldNames(c.m.kc, "key", res.objectType.KeyFields)
Expand Down
5 changes: 5 additions & 0 deletions collections/pair.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ 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
8 changes: 8 additions & 0 deletions indexer/postgres/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ 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 @@ -17,6 +22,9 @@ 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 b1ccaf9

Please sign in to comment.