Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.18

require (
github.com/derekparker/trie v0.0.0-20221221181808-1424fce0c981
github.com/goccy/go-json v0.10.0
github.com/golang/glog v1.0.0
github.com/golang/protobuf v1.5.2
github.com/google/go-cmp v0.5.9
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbS
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/goccy/go-json v0.10.0 h1:mXKd9Qw4NuzShiRlOXKews24ufknHO7gx30lsDyokKA=
github.com/goccy/go-json v0.10.0/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/glog v1.0.0 h1:nfP3RFugxnNRyKgeWd4oI1nYvXpxrx8ck8ZrcizshdQ=
github.com/golang/glog v1.0.0/go.mod h1:EWib/APOK0SL3dFbYqvxE3UYd8E6s1ouQ7iEp/0LWV4=
Expand Down
18 changes: 9 additions & 9 deletions testcmp/cmp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func mustPath(s string) *gnmipb.Path {
func jsonIETF(s string) *gnmipb.TypedValue {
return &gnmipb.TypedValue{
Value: &gnmipb.TypedValue_JsonIetfVal{
[]byte(s),
JsonIetfVal: []byte(s),
},
}
}
Expand Down Expand Up @@ -86,7 +86,7 @@ func TestGNMIUpdateComparer(t *testing.T) {
wantDiff: &gnmipb.Notification{
Update: []*gnmipb.Update{{
Path: mustPath("/system/config/hostname"),
Val: &gnmipb.TypedValue{Value: &gnmipb.TypedValue_StringVal{"bus"}},
Val: &gnmipb.TypedValue{Value: &gnmipb.TypedValue_StringVal{StringVal: "bus"}},
}},
},
}, {
Expand Down Expand Up @@ -157,23 +157,23 @@ func TestGNMIUpdateComparer(t *testing.T) {
desc: "equal: not IETF JSON",
inA: &gnmipb.Update{
Path: mustPath("/system/config/hostname"),
Val: &gnmipb.TypedValue{Value: &gnmipb.TypedValue_StringVal{"fish"}},
Val: &gnmipb.TypedValue{Value: &gnmipb.TypedValue_StringVal{StringVal: "fish"}},
},
inB: &gnmipb.Update{
Path: mustPath("/system/config/hostname"),
Val: &gnmipb.TypedValue{Value: &gnmipb.TypedValue_StringVal{"fish"}},
Val: &gnmipb.TypedValue{Value: &gnmipb.TypedValue_StringVal{StringVal: "fish"}},
},
inSpec: commonSpec,
wantEqual: true,
}, {
desc: "not equal: different paths",
inA: &gnmipb.Update{
Path: mustPath("/system/config/domain-name"),
Val: &gnmipb.TypedValue{Value: &gnmipb.TypedValue_StringVal{"fish"}},
Val: &gnmipb.TypedValue{Value: &gnmipb.TypedValue_StringVal{StringVal: "fish"}},
},
inB: &gnmipb.Update{
Path: mustPath("/system/config/hostname"),
Val: &gnmipb.TypedValue{Value: &gnmipb.TypedValue_StringVal{"fish"}},
Val: &gnmipb.TypedValue{Value: &gnmipb.TypedValue_StringVal{StringVal: "fish"}},
},
inSpec: commonSpec,
wantEqual: false,
Expand All @@ -191,7 +191,7 @@ func TestGNMIUpdateComparer(t *testing.T) {
desc: "not equal: one value nil",
inA: &gnmipb.Update{
Path: mustPath("/system/config/domain-name"),
Val: &gnmipb.TypedValue{Value: &gnmipb.TypedValue_StringVal{"fish"}},
Val: &gnmipb.TypedValue{Value: &gnmipb.TypedValue_StringVal{StringVal: "fish"}},
},
inB: &gnmipb.Update{
Path: mustPath("/system/config/domain-name"),
Expand All @@ -202,11 +202,11 @@ func TestGNMIUpdateComparer(t *testing.T) {
desc: "not equal: different types",
inA: &gnmipb.Update{
Path: mustPath("/system/config/hostname"),
Val: &gnmipb.TypedValue{Value: &gnmipb.TypedValue_StringVal{"fish"}},
Val: &gnmipb.TypedValue{Value: &gnmipb.TypedValue_StringVal{StringVal: "fish"}},
},
inB: &gnmipb.Update{
Path: mustPath("/system/config/hostname"),
Val: &gnmipb.TypedValue{Value: &gnmipb.TypedValue_UintVal{42}},
Val: &gnmipb.TypedValue{Value: &gnmipb.TypedValue_UintVal{UintVal: 42}},
},
inSpec: commonSpec,
wantEqual: false,
Expand Down
27 changes: 27 additions & 0 deletions util/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,16 @@ func InsertIntoStruct(parentStruct interface{}, fieldName string, fieldValue int
// generated code. Here we cast the value to the type in the generated code.
if ft.Type.Kind() == reflect.Bool && t.Kind() == reflect.Bool {
nv := reflect.New(ft.Type).Elem()

// If the field is not nil, do not create a new pointer which modifies the
// field's memory address under its parent.
if pv.Elem().FieldByName(fieldName).Addr().UnsafePointer() != nil {
nv = reflect.NewAt(
ft.Type,
pv.Elem().FieldByName(fieldName).Addr().UnsafePointer(),
).Elem()
}

nv.SetBool(v.Bool())
v = nv
}
Expand All @@ -265,13 +275,30 @@ func InsertIntoStruct(parentStruct interface{}, fieldName string, fieldValue int
// This will also cast a []uint8 value since byte is an alias for uint8.
if ft.Type.Kind() == reflect.Slice && t.Kind() == reflect.Slice && ft.Type.Elem().Kind() == reflect.Uint8 && t.Elem().Kind() == reflect.Uint8 {
nv := reflect.New(ft.Type).Elem()

// If the field is not nil, do not create a new pointer which modifies the
// field's memory address under its parent.
if pv.Elem().FieldByName(fieldName).Addr().UnsafePointer() != nil {
nv = reflect.NewAt(
ft.Type,
pv.Elem().FieldByName(fieldName).Addr().UnsafePointer(),
).Elem()
}

nv.SetBytes(v.Bytes())
v = nv
}

n := v
if n.IsValid() && (ft.Type.Kind() == reflect.Ptr && t.Kind() != reflect.Ptr) {
n = reflect.New(t)

// If the field is not nil, do not create a new pointer which modifies the
// field's memory address under its parent.
if pv.Elem().FieldByName(fieldName).UnsafePointer() != nil {
n = reflect.NewAt(t, pv.Elem().FieldByName(fieldName).UnsafePointer())
}

n.Elem().Set(v)
}

Expand Down
Loading