From 7c54992bbda2442b195147e48942047256cfccd1 Mon Sep 17 00:00:00 2001 From: wenovus Date: Tue, 31 Jan 2023 12:54:18 -0800 Subject: [PATCH] Handle CLI origin string values. --- ygnmi/gnmi.go | 4 +++- ygnmi/ygnmi_test.go | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/ygnmi/gnmi.go b/ygnmi/gnmi.go index 537666e..6ae5cf6 100644 --- a/ygnmi/gnmi.go +++ b/ygnmi/gnmi.go @@ -347,7 +347,9 @@ func populateSetRequest(req *gpb.SetRequest, path *gpb.Path, val interface{}, op case replacePath, updatePath: var typedVal *gpb.TypedValue var err error - if opt.preferProto { + if s, ok := val.(*string); ok && path.Origin == "cli" { + typedVal = &gpb.TypedValue{Value: &gpb.TypedValue_AsciiVal{AsciiVal: *s}} + } else if opt.preferProto { typedVal, err = ygot.EncodeTypedValue(val, gpb.Encoding_JSON_IETF, &ygot.RFC7951JSONConfig{AppendModuleName: true, PreferShadowPath: preferShadowPath}) } else { typedVal = &gpb.TypedValue{Value: &gpb.TypedValue_JsonIetfVal{}} diff --git a/ygnmi/ygnmi_test.go b/ygnmi/ygnmi_test.go index 94fa445..6117d0a 100644 --- a/ygnmi/ygnmi_test.go +++ b/ygnmi/ygnmi_test.go @@ -3205,8 +3205,15 @@ func TestSetBatch(t *testing.T) { }{{ desc: "leaf update replace delete", addPaths: func(sb *ygnmi.SetBatch) { + cliPath, err := schemaless.NewConfig[string]("", "cli") + if err != nil { + t.Fatalf("Failed to create CLI ygnmi query: %v", err) + } + ygnmi.BatchUpdate(sb, cliPath, "hello, mercury") ygnmi.BatchUpdate(sb, exampleocpath.Root().Parent().Child().One().Config(), "foo") + ygnmi.BatchReplace(sb, cliPath, "hello, venus") ygnmi.BatchReplace(sb, exampleocpath.Root().Parent().Child().One().Config(), "bar") + ygnmi.BatchDelete(sb, cliPath) ygnmi.BatchDelete(sb, exampleocpath.Root().Parent().Child().One().Config()) }, wantRequest: &gpb.SetRequest{ @@ -3214,14 +3221,21 @@ func TestSetBatch(t *testing.T) { Target: "dut", }, Update: []*gpb.Update{{ + Path: &gpb.Path{Origin: "cli"}, + Val: &gpb.TypedValue{Value: &gpb.TypedValue_AsciiVal{AsciiVal: "hello, mercury"}}, + }, { Path: testutil.GNMIPath(t, "parent/child/config/one"), Val: &gpb.TypedValue{Value: &gpb.TypedValue_JsonIetfVal{JsonIetfVal: []byte("\"foo\"")}}, }}, Replace: []*gpb.Update{{ + Path: &gpb.Path{Origin: "cli"}, + Val: &gpb.TypedValue{Value: &gpb.TypedValue_AsciiVal{AsciiVal: "hello, venus"}}, + }, { Path: testutil.GNMIPath(t, "parent/child/config/one"), Val: &gpb.TypedValue{Value: &gpb.TypedValue_JsonIetfVal{JsonIetfVal: []byte("\"bar\"")}}, }}, Delete: []*gpb.Path{ + {Origin: "cli"}, testutil.GNMIPath(t, "parent/child/config/one"), }, },