Skip to content

Commit 0105d93

Browse files
committed
Merge remote-tracking branch 'origin/main' into dang/add-reset-method-for-circuit-module
2 parents 09906df + 2290c5e commit 0105d93

File tree

17 files changed

+154
-26
lines changed

17 files changed

+154
-26
lines changed

collections/codec/indexing.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func ValueSchemaCodec[V any](cdc ValueCodec[V]) (SchemaCodec[V], error) {
6565

6666
// FallbackSchemaCodec returns a fallback schema codec for T when one isn't explicitly
6767
// specified with HasSchemaCodec. It maps all simple types directly to schema kinds
68-
// and converts everything else to JSON.
68+
// and converts everything else to JSON String.
6969
func FallbackSchemaCodec[T any]() SchemaCodec[T] {
7070
var t T
7171
kind := schema.KindForGoValue(t)
@@ -81,20 +81,20 @@ func FallbackSchemaCodec[T any]() SchemaCodec[T] {
8181
FromSchemaType: nil,
8282
}
8383
} else {
84-
// we default to encoding everything to JSON
84+
// we default to encoding everything to JSON String
8585
return SchemaCodec[T]{
86-
Fields: []schema.Field{{Kind: schema.JSONKind}},
86+
Fields: []schema.Field{{Kind: schema.StringKind}},
8787
ToSchemaType: func(t T) (any, error) {
8888
bz, err := json.Marshal(t)
89-
return json.RawMessage(bz), err
89+
return string(json.RawMessage(bz)), err
9090
},
9191
FromSchemaType: func(a any) (T, error) {
9292
var t T
93-
bz, ok := a.(json.RawMessage)
93+
sz, ok := a.(string)
9494
if !ok {
95-
return t, fmt.Errorf("expected json.RawMessage, got %T", a)
95+
return t, fmt.Errorf("expected string, got %T", a)
9696
}
97-
err := json.Unmarshal(bz, &t)
97+
err := json.Unmarshal([]byte(sz), &t)
9898
return t, err
9999
},
100100
}

collections/indexes/multi.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,26 @@ func NewMulti[ReferenceKey, PrimaryKey, Value any](
5151
if o.uncheckedValue {
5252
return &Multi[ReferenceKey, PrimaryKey, Value]{
5353
getRefKey: getRefKeyFunc,
54-
refKeys: collections.NewKeySet(schema, prefix, name, collections.PairKeyCodec(refCodec, pkCodec), collections.WithKeySetUncheckedValue()),
54+
refKeys: collections.NewKeySet(
55+
schema,
56+
prefix,
57+
name,
58+
collections.PairKeyCodec(refCodec, pkCodec),
59+
collections.WithKeySetUncheckedValue(),
60+
collections.WithKeySetSecondaryIndex(),
61+
),
5562
}
5663
}
5764

5865
return &Multi[ReferenceKey, PrimaryKey, Value]{
5966
getRefKey: getRefKeyFunc,
60-
refKeys: collections.NewKeySet(schema, prefix, name, collections.PairKeyCodec(refCodec, pkCodec)),
67+
refKeys: collections.NewKeySet(
68+
schema,
69+
prefix,
70+
name,
71+
collections.PairKeyCodec(refCodec, pkCodec),
72+
collections.WithKeySetSecondaryIndex(),
73+
),
6174
}
6275
}
6376

collections/indexes/reverse_pair.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,25 @@ func NewReversePair[Value, K1, K2 any](
5555
}
5656
if o.uncheckedValue {
5757
return &ReversePair[K1, K2, Value]{
58-
refKeys: collections.NewKeySet(sb, prefix, name, collections.PairKeyCodec(pkc.KeyCodec2(), pkc.KeyCodec1()), collections.WithKeySetUncheckedValue()),
58+
refKeys: collections.NewKeySet(
59+
sb,
60+
prefix,
61+
name,
62+
collections.PairKeyCodec(pkc.KeyCodec2(), pkc.KeyCodec1()),
63+
collections.WithKeySetUncheckedValue(),
64+
collections.WithKeySetSecondaryIndex(),
65+
),
5966
}
6067
}
6168

6269
mi := &ReversePair[K1, K2, Value]{
63-
refKeys: collections.NewKeySet(sb, prefix, name, collections.PairKeyCodec(pkc.KeyCodec2(), pkc.KeyCodec1())),
70+
refKeys: collections.NewKeySet(
71+
sb,
72+
prefix,
73+
name,
74+
collections.PairKeyCodec(pkc.KeyCodec2(), pkc.KeyCodec1()),
75+
collections.WithKeySetSecondaryIndex(),
76+
),
6477
}
6578

6679
return mi

collections/indexing.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,9 @@ func (c collectionImpl[K, V]) schemaCodec() (*collectionSchemaCodec, error) {
133133
if err != nil {
134134
return nil, err
135135
}
136+
if keyDecoder.ToSchemaType == nil {
137+
return x, nil
138+
}
136139
return keyDecoder.ToSchemaType(x)
137140
}
138141
ensureFieldNames(c.m.kc, "key", res.objectType.KeyFields)

collections/keyset.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,17 @@ func WithKeySetUncheckedValue() func(opt *keySetOptions) {
2121
}
2222
}
2323

24-
type keySetOptions struct{ uncheckedValue bool }
24+
// WithKeySetSecondaryIndex changes the behavior of the KeySet to be a secondary index.
25+
func WithKeySetSecondaryIndex() func(opt *keySetOptions) {
26+
return func(opt *keySetOptions) {
27+
opt.isSecondaryIndex = true
28+
}
29+
}
30+
31+
type keySetOptions struct {
32+
uncheckedValue bool
33+
isSecondaryIndex bool
34+
}
2535

2636
// KeySet builds on top of a Map and represents a collection retaining only a set
2737
// of keys and no value. It can be used, for example, in an allow list.
@@ -44,7 +54,7 @@ func NewKeySet[K any](
4454
if o.uncheckedValue {
4555
vc = codec.NewAltValueCodec(vc, func(_ []byte) (NoValue, error) { return NoValue{}, nil })
4656
}
47-
return (KeySet[K])(NewMap(schema, prefix, name, keyCodec, vc))
57+
return (KeySet[K])(NewMap(schema, prefix, name, keyCodec, vc, withMapSecondaryIndex(o.isSecondaryIndex)))
4858
}
4959

5060
// Set adds the key to the KeySet. Errors on encoding problems.

collections/map.go

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,17 @@ type Map[K, V any] struct {
2727
isSecondaryIndex bool
2828
}
2929

30+
// withMapSecondaryIndex changes the behavior of the Map to be a secondary index.
31+
func withMapSecondaryIndex(isSecondaryIndex bool) func(opt *mapOptions) {
32+
return func(opt *mapOptions) {
33+
opt.isSecondaryIndex = isSecondaryIndex
34+
}
35+
}
36+
37+
type mapOptions struct {
38+
isSecondaryIndex bool
39+
}
40+
3041
// NewMap returns a Map given a StoreKey, a Prefix, human-readable name and the relative value and key encoders.
3142
// Name and prefix must be unique within the schema and name must match the format specified by NameRegex, or
3243
// else this method will panic.
@@ -36,13 +47,19 @@ func NewMap[K, V any](
3647
name string,
3748
keyCodec codec.KeyCodec[K],
3849
valueCodec codec.ValueCodec[V],
50+
options ...func(opt *mapOptions),
3951
) Map[K, V] {
52+
o := new(mapOptions)
53+
for _, opt := range options {
54+
opt(o)
55+
}
4056
m := Map[K, V]{
41-
kc: keyCodec,
42-
vc: valueCodec,
43-
sa: schemaBuilder.schema.storeAccessor,
44-
prefix: prefix.Bytes(),
45-
name: name,
57+
kc: keyCodec,
58+
vc: valueCodec,
59+
sa: schemaBuilder.schema.storeAccessor,
60+
prefix: prefix.Bytes(),
61+
name: name,
62+
isSecondaryIndex: o.isSecondaryIndex,
4663
}
4764
schemaBuilder.addCollection(collectionImpl[K, V]{m})
4865
return m

collections/pair.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,16 @@ func (p pairKeyCodec[K1, K2]) SchemaCodec() (codec.SchemaCodec[Pair[K1, K2]], er
247247

248248
return codec.SchemaCodec[Pair[K1, K2]]{
249249
Fields: []schema.Field{field1, field2},
250+
ToSchemaType: func(pair Pair[K1, K2]) (any, error) {
251+
return []interface{}{pair.K1(), pair.K2()}, nil
252+
},
253+
FromSchemaType: func(a any) (Pair[K1, K2], error) {
254+
aSlice, ok := a.([]interface{})
255+
if !ok || len(aSlice) != 2 {
256+
return Pair[K1, K2]{}, fmt.Errorf("expected slice of length 2, got %T", a)
257+
}
258+
return Join(aSlice[0].(K1), aSlice[1].(K2)), nil
259+
},
250260
}, nil
251261
}
252262

collections/quad.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,16 @@ func (t quadKeyCodec[K1, K2, K3, K4]) SchemaCodec() (codec.SchemaCodec[Quad[K1,
382382

383383
return codec.SchemaCodec[Quad[K1, K2, K3, K4]]{
384384
Fields: []schema.Field{field1, field2, field3, field4},
385+
ToSchemaType: func(q Quad[K1, K2, K3, K4]) (any, error) {
386+
return []interface{}{q.K1(), q.K2(), q.K3(), q.K4()}, nil
387+
},
388+
FromSchemaType: func(a any) (Quad[K1, K2, K3, K4], error) {
389+
aSlice, ok := a.([]interface{})
390+
if !ok || len(aSlice) != 4 {
391+
return Quad[K1, K2, K3, K4]{}, fmt.Errorf("expected slice of length 4, got %T", a)
392+
}
393+
return Join4(aSlice[0].(K1), aSlice[1].(K2), aSlice[2].(K3), aSlice[3].(K4)), nil
394+
},
385395
}, nil
386396
}
387397

collections/triple.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,16 @@ func (t tripleKeyCodec[K1, K2, K3]) SchemaCodec() (codec.SchemaCodec[Triple[K1,
311311

312312
return codec.SchemaCodec[Triple[K1, K2, K3]]{
313313
Fields: []schema.Field{field1, field2, field3},
314+
ToSchemaType: func(t Triple[K1, K2, K3]) (any, error) {
315+
return []interface{}{t.K1(), t.K2(), t.K3()}, nil
316+
},
317+
FromSchemaType: func(a any) (Triple[K1, K2, K3], error) {
318+
aSlice, ok := a.([]interface{})
319+
if !ok || len(aSlice) != 3 {
320+
return Triple[K1, K2, K3]{}, fmt.Errorf("expected slice of length 3, got %T", a)
321+
}
322+
return Join3(aSlice[0].(K1), aSlice[1].(K2), aSlice[2].(K3)), nil
323+
},
314324
}, nil
315325
}
316326

runtime/builder.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,14 @@ func (a *AppBuilder) registerIndexer() error {
9999
if indexerOpts := a.appOptions.Get("indexer"); indexerOpts != nil {
100100
moduleSet := map[string]any{}
101101
for modName, mod := range a.app.ModuleManager.Modules {
102-
moduleSet[modName] = mod
102+
storeKey := modName
103+
for _, cfg := range a.app.config.OverrideStoreKeys {
104+
if cfg.ModuleName == modName {
105+
storeKey = cfg.KvStoreKey
106+
break
107+
}
108+
}
109+
moduleSet[storeKey] = mod
103110
}
104111

105112
return a.app.EnableIndexer(indexerOpts, a.kvStoreKeys(), moduleSet)

0 commit comments

Comments
 (0)