Skip to content

Commit 4fa14af

Browse files
committed
feat(model): handle cached extensions
Update dependencies to the corim library that now caches unknown extensions. Extract cached extension values along side known extensions when converting. An ExtensionValue represents a cached value if its FieldName is empty. In that case, JSONTag is set to the map key (which could be the stringified CBOR code point) ValueBytes are set to the CBOR-encoded value. Signed-off-by: setrofim <[email protected]>
1 parent e6131c8 commit 4fa14af

File tree

4 files changed

+51
-10
lines changed

4 files changed

+51
-10
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ require (
1919
github.com/uptrace/bun/dialect/sqlitedialect v1.2.16
2020
github.com/uptrace/bun/driver/sqliteshim v1.2.15
2121
github.com/uptrace/bun/extra/bundebug v1.2.15
22-
github.com/veraison/corim v1.1.3-0.20251209103150-ef6dbb7ed63f
22+
github.com/veraison/corim v1.1.3-0.20251212144809-26e0f2a5f59d
2323
github.com/veraison/eat v0.0.0-20210331113810-3da8a4dd42ff
2424
github.com/veraison/swid v1.1.1-0.20251003121634-fd1f7f1e1897
2525
)

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ github.com/uptrace/bun/driver/sqliteshim v1.2.15 h1:M/rZJSjOPV4OmfTVnDPtL+wJmdMT
129129
github.com/uptrace/bun/driver/sqliteshim v1.2.15/go.mod h1:YqwxFyvM992XOCpGJtXyKPkgkb+aZpIIMzGbpaw1hIk=
130130
github.com/uptrace/bun/extra/bundebug v1.2.15 h1:IY2Z/pVyVg0ApWnQ/pEnwe6BWxlDDATCz7IFZghutCs=
131131
github.com/uptrace/bun/extra/bundebug v1.2.15/go.mod h1:JuE+BT7NjTZ9UKr74eC8s9yZ9dnQCeufDwFRTC8w3Xo=
132-
github.com/veraison/corim v1.1.3-0.20251209103150-ef6dbb7ed63f h1:ANVwskQLZ0YEzivFZqreGIftxakfd579fpOcjU8rHjo=
133-
github.com/veraison/corim v1.1.3-0.20251209103150-ef6dbb7ed63f/go.mod h1:96PQ0lk+O9bzutKTDz66G2DaARYUp1BeR06EYwEwSH0=
132+
github.com/veraison/corim v1.1.3-0.20251212144809-26e0f2a5f59d h1:ifSo+6zUb8I7yOBF1MgZflJwXmCYiZmiemd74VDcJW0=
133+
github.com/veraison/corim v1.1.3-0.20251212144809-26e0f2a5f59d/go.mod h1:96PQ0lk+O9bzutKTDz66G2DaARYUp1BeR06EYwEwSH0=
134134
github.com/veraison/eat v0.0.0-20210331113810-3da8a4dd42ff h1:r6I2eJL/z8dp5flsQIKHMeDjyV6UO8If3MaVBLvTjF4=
135135
github.com/veraison/eat v0.0.0-20210331113810-3da8a4dd42ff/go.mod h1:+kxt8iuFiVvKRs2VQ1Ho7bbAScXAB/kHFFuP5Biw19I=
136136
github.com/veraison/go-cose v1.2.1 h1:Gj4x20D0YP79J2+cK3anjGEMwIkg2xX+TKVVGUXwNAc=

pkg/model/extension.go

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func CoRIMExtensionsFromCoRIM(origin corim.Extensions) ([]*ExtensionValue, error
1717
}
1818

1919
func CoMIDExtensionsFromCoRIM(origin comid.Extensions) ([]*ExtensionValue, error) {
20-
var ret []*ExtensionValue
20+
var ret []*ExtensionValue // nolint: prealloc
2121
if origin.IsEmpty() {
2222
return ret, nil
2323
}
@@ -78,6 +78,21 @@ func CoMIDExtensionsFromCoRIM(origin comid.Extensions) ([]*ExtensionValue, error
7878
ret = append(ret, &retVal)
7979
}
8080

81+
for k, v := range origin.Cached {
82+
bytes, err := cbor.Marshal(v)
83+
if err != nil {
84+
return nil, fmt.Errorf("error CBOR encoding cached extension %s: %w", k, err)
85+
}
86+
87+
retVal := ExtensionValue{
88+
FieldName: "", // empty field name indicates cached value
89+
JSONTag: k,
90+
ValueBytes: bytes,
91+
}
92+
93+
ret = append(ret, &retVal)
94+
}
95+
8196
return ret, nil
8297
}
8398

@@ -91,12 +106,24 @@ func CoMIDExtensionsToCoRIM(origin []*ExtensionValue) (comid.Extensions, error)
91106
return comid.Extensions{}, nil
92107
}
93108

94-
values := make([]any, 0, len(origin))
109+
values := make(map[string]any, len(origin))
95110
fields := make([]reflect.StructField, 0, len(origin))
111+
cached := make(map[string]any, len(origin))
96112

97113
for _, origVal := range origin {
98114
var val any
99115

116+
if origVal.FieldName == "" {
117+
// empty field name means this is a cached value
118+
if err := cbor.Unmarshal(origVal.ValueBytes, &val); err != nil {
119+
return comid.Extensions{}, fmt.Errorf(
120+
"error decoding CBOR for %s: %w", origVal.JSONTag, err)
121+
}
122+
123+
cached[origVal.JSONTag] = val
124+
continue
125+
}
126+
100127
switch origVal.FieldKind {
101128
case reflect.String:
102129
val = origVal.ValueText
@@ -147,17 +174,21 @@ func CoMIDExtensionsToCoRIM(origin []*ExtensionValue) (comid.Extensions, error)
147174
origVal.CBORTag, origVal.JSONTag)),
148175
})
149176

150-
values = append(values, val)
177+
values[origVal.FieldName] = val
151178
}
152179

153180
structType := reflect.StructOf(fields)
154181
structPtr := reflect.New(structType)
155182
structValue := structPtr.Elem()
156183

157-
for i, origVal := range origin {
184+
for _, origVal := range origin {
185+
if origVal.FieldName == "" {
186+
continue
187+
}
188+
158189
field := structValue.FieldByName(origVal.FieldName)
159190
if field.IsValid() && field.CanSet() {
160-
field.Set(reflect.ValueOf(values[i]))
191+
field.Set(reflect.ValueOf(values[origVal.FieldName]))
161192
} else {
162193
return comid.Extensions{}, fmt.Errorf("could not set field %q", origVal.FieldName)
163194
}
@@ -166,6 +197,10 @@ func CoMIDExtensionsToCoRIM(origin []*ExtensionValue) (comid.Extensions, error)
166197
var ret comid.Extensions
167198
ret.IMapValue = structPtr.Interface()
168199

200+
if len(cached) != 0 {
201+
ret.Cached = cached
202+
}
203+
169204
return ret, nil
170205
}
171206

pkg/model/extension_test.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,14 @@ func TestExtensionValue_round_trip(t *testing.T) {
3737

3838
var original comid.Extensions
3939
original.Register(&extStruct)
40+
original.Cached = map[string]any{
41+
"-1": uint64(7),
42+
"fum": false,
43+
}
4044

4145
extVals, err := CoMIDExtensionsFromCoRIM(original)
4246
assert.NoError(t, err)
43-
assert.Len(t, extVals, 8)
47+
assert.Len(t, extVals, 10)
4448

4549
for _, ev := range extVals {
4650
ev.OwnerID = 1
@@ -53,7 +57,7 @@ func TestExtensionValue_round_trip(t *testing.T) {
5357

5458
err = db.NewSelect().Model(&resVals).Scan(ctx)
5559
assert.NoError(t, err)
56-
assert.Len(t, resVals, 8)
60+
assert.Len(t, resVals, 10)
5761

5862
returnedExts, err := CoMIDExtensionsToCoRIM(resVals)
5963
assert.NoError(t, err)
@@ -63,6 +67,8 @@ func TestExtensionValue_round_trip(t *testing.T) {
6367
val, err := returnedExts.Get("Qux")
6468
assert.NoError(t, err)
6569
assert.Equal(t, map[interface{}]interface{}{"Zap": true}, val)
70+
71+
assert.Equal(t, original.Cached, returnedExts.Cached)
6672
}
6773

6874
func TestExtensionValue_Select(t *testing.T) {

0 commit comments

Comments
 (0)