Skip to content

Commit c85f80f

Browse files
committed
fixing json builder by adjusting value types
1 parent 2ebda70 commit c85f80f

File tree

3 files changed

+26
-27
lines changed

3 files changed

+26
-27
lines changed

pkg/datastore/data_rpc.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -799,7 +799,7 @@ func (d *Datastore) expandUpdates(ctx context.Context, updates []*sdcpb.Update,
799799
return outUpdates, nil
800800
}
801801

802-
func (d *Datastore) expandUpdateKeysAsLeaf(_ context.Context, upd *sdcpb.Update) ([]*sdcpb.Update, error) {
802+
func (d *Datastore) expandUpdateKeysAsLeaf(ctx context.Context, upd *sdcpb.Update) ([]*sdcpb.Update, error) {
803803
upds := make([]*sdcpb.Update, 0)
804804
// expand update path if it contains keys
805805
for i, pe := range upd.GetPath().GetElem() {
@@ -822,7 +822,16 @@ func (d *Datastore) expandUpdateKeysAsLeaf(_ context.Context, upd *sdcpb.Update)
822822
)
823823
}
824824
intUpd.Path.Elem = append(intUpd.Path.Elem, &sdcpb.PathElem{Name: k})
825+
825826
intUpd.Value = &sdcpb.TypedValue{Value: &sdcpb.TypedValue_StringVal{StringVal: v}}
827+
schemaRsp, err := d._validationClientBound.GetSchema(ctx, upd.Path)
828+
if err != nil {
829+
return nil, err
830+
}
831+
intUpd.Value, err = d.typedValueToYANGType(upd.GetValue(), schemaRsp.GetSchema())
832+
if err != nil {
833+
return nil, err
834+
}
826835
upds = append(upds, intUpd)
827836
}
828837
}

pkg/datastore/datastore_rpc.go

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -675,30 +675,23 @@ func (d *Datastore) storeSyncMsg(ctx context.Context, syncup *target.SyncUpdate,
675675
store = cachepb.Store_STATE
676676
}
677677
}
678-
expandedUpds, err := d.expandUpdateKeysAsLeaf(ctx, upd)
678+
// TODO:[KR] convert update typedValue if needed
679+
cUpd, err := d.cacheClient.NewUpdate(upd)
679680
if err != nil {
680-
log.Errorf("datastore %s failed expanding Update keys as leafs %v: %v", d.config.Name, upd.GetPath(), err)
681+
log.Errorf("datastore %s failed to create update from %v: %v", d.config.Name, upd, err)
681682
continue
682683
}
683-
upds := append(expandedUpds, upd)
684-
for _, expUpd := range upds {
685-
// TODO:[KR] convert update typedValue if needed
686-
cUpd, err := d.cacheClient.NewUpdate(expUpd)
687-
if err != nil {
688-
log.Errorf("datastore %s failed to create update from %v: %v", d.config.Name, upd, err)
689-
continue
690-
}
691684

692-
rctx, cancel := context.WithTimeout(ctx, time.Minute) // TODO:[KR] make this timeout configurable ?
693-
defer cancel()
694-
err = d.cacheClient.Modify(rctx, d.Config().Name, &cache.Opts{
695-
Store: store,
696-
}, nil, []*cache.Update{cUpd})
697-
if err != nil {
698-
log.Errorf("datastore %s failed to send modify request to cache: %v", d.config.Name, err)
699-
}
685+
rctx, cancel := context.WithTimeout(ctx, time.Minute) // TODO:[KR] make this timeout configurable ?
686+
defer cancel()
687+
err = d.cacheClient.Modify(rctx, d.Config().Name, &cache.Opts{
688+
Store: store,
689+
}, nil, []*cache.Update{cUpd})
690+
if err != nil {
691+
log.Errorf("datastore %s failed to send modify request to cache: %v", d.config.Name, err)
700692
}
701693
}
694+
702695
}
703696

704697
// helper for GetSchema

pkg/datastore/jbuilderv2/jsonbuilder.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,10 @@ func convertKeyValue(value, valueType string) (interface{}, error) {
134134
switch valueType {
135135
case stringType:
136136
return value, nil
137-
case int8Type, int16Type, int32Type, uint8Type, uint16Type, uint32Type:
137+
case int8Type, int16Type, int32Type:
138138
return strconv.Atoi(value)
139+
case uint8Type, uint16Type, uint32Type:
140+
return strconv.ParseUint(value, 10, 64)
139141
case floatType:
140142
return strconv.ParseFloat(value, 64)
141143
case boolType:
@@ -219,16 +221,11 @@ func (j *jsonBuilder) addValueToObject(obj map[string]any, path []*pathElem, val
219221
// the provided keys.
220222
func findOrCreateMatchingObject(arr []map[string]interface{}, keys map[string]*vt) (map[string]interface{}, bool, error) {
221223
for _, obj := range arr {
222-
match := true
223224
for k, v := range keys {
224-
if !reflect.DeepEqual(obj[k], v.conVal) {
225-
match = false
226-
break
225+
if reflect.DeepEqual(obj[k], v.conVal) {
226+
return obj, true, nil
227227
}
228228
}
229-
if match {
230-
return obj, true, nil
231-
}
232229
}
233230

234231
// No match found, create a new object

0 commit comments

Comments
 (0)