Skip to content

Commit 7559da7

Browse files
authored
Introduce a patch to retain Jipanyang's commit (#354)
Why I did it We need to keep Jipanyang's commit history How I did it Add a patch and move Jipanyang's commit to this patch How to verify it Run gnmi unit test
1 parent f43c8bc commit 7559da7

File tree

3 files changed

+107
-81
lines changed

3 files changed

+107
-81
lines changed

Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ endif
101101
patch -d vendor -p0 < patches/gnmi_set.patch
102102
patch -d vendor -p0 < patches/gnmi_get.patch
103103
git apply patches/0001-Updated-to-filter-and-write-to-file.patch
104+
git apply patches/0003-Fix-client-json-parsing-issue.patch
104105

105106
ifeq ($(CROSS_BUILD_ENVIRON),y)
106107
$(GO) build -o ${GOBIN}/gnmi_get -mod=vendor github.com/google/gnxi/gnmi_get
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
From c4660f5f1a5c38902c374f002174848aff2f1739 Sat Aug 18 00:00:00 2018
2+
From: Jipanyang <[email protected]>
3+
Date: Sat, 18 Aug 2018 00:00:00 +0000
4+
Subject: [PATCH] Used local gnmi client update to avoid json (value.ToScalar()) parsing issue
5+
6+
---
7+
vendor/github.com/openconfig/gnmi/client/gnmi/client.go | 13 +++-
8+
vendor/github.com/openconfig/gnmi/value/value.go | 2 +
9+
2 files changed, 49 insertions(+), 2 deletions(-)
10+
11+
diff --git a/vendor/github.com/openconfig/gnmi/client/gnmi/client.go b/vendor/github.com/openconfig/gnmi/client/gnmi/client.go
12+
index 81a8704..ca0a383 100644
13+
--- a/vendor/github.com//openconfig/gnmi/client/gnmi/client.go 2018-8-17 14:03:29.839103602 -0800
14+
+++ b/vendor/github.com//openconfig/gnmi/client/gnmi/client.go 2018-8-18 13:48:49.226145599 -0700
15+
@@ -37,7 +37,6 @@
16+
"github.com/openconfig/gnmi/client"
17+
"github.com/openconfig/gnmi/client/grpcutil"
18+
"github.com/openconfig/gnmi/path"
19+
- "github.com/openconfig/gnmi/value"
20+
21+
gpb "github.com/openconfig/gnmi/proto/gnmi"
22+
)
23+
@@ -257,11 +256,58 @@
24+
if err != nil {
25+
return nil, fmt.Errorf("invalid query path %q: %v", qq, err)
26+
}
27+
s.Subscribe.Subscription = append(s.Subscribe.Subscription, &gpb.Subscription{Path: pp, Mode: q.Streaming_type, SampleInterval: q.Streaming_sample_int, HeartbeatInterval: q.Heartbeat_int, SuppressRedundant: q.Suppress_redundant})
28+
}
29+
return &gpb.SubscribeRequest{Request: s}, nil
30+
}
31+
32+
+// decimalToFloat converts a *gnmi_proto.Decimal64 to a float32. Downcasting to float32 is performed as the
33+
+// precision of a float64 is not required.
34+
+func decimalToFloat(d *gpb.Decimal64) float32 {
35+
+ return float32(float64(d.Digits) / math.Pow(10, float64(d.Precision)))
36+
+}
37+
+
38+
+// ToScalar will convert TypedValue scalar types to a Go native type. It will
39+
+// return an error if the TypedValue does not contain a scalar type.
40+
+func toScalar(tv *gpb.TypedValue) (interface{}, error) {
41+
+ var i interface{}
42+
+ switch tv.Value.(type) {
43+
+ case *gpb.TypedValue_DecimalVal:
44+
+ i = decimalToFloat(tv.GetDecimalVal())
45+
+ case *gpb.TypedValue_StringVal:
46+
+ i = tv.GetStringVal()
47+
+ case *gpb.TypedValue_IntVal:
48+
+ i = tv.GetIntVal()
49+
+ case *gpb.TypedValue_UintVal:
50+
+ i = tv.GetUintVal()
51+
+ case *gpb.TypedValue_BoolVal:
52+
+ i = tv.GetBoolVal()
53+
+ case *gpb.TypedValue_FloatVal:
54+
+ i = tv.GetFloatVal()
55+
+ case *gpb.TypedValue_LeaflistVal:
56+
+ elems := tv.GetLeaflistVal().GetElement()
57+
+ ss := make([]interface{}, len(elems))
58+
+ for x, e := range elems {
59+
+ v, err := toScalar(e)
60+
+ if err != nil {
61+
+ return nil, fmt.Errorf("ToScalar for ScalarArray %+v: %v", e.Value, err)
62+
+ }
63+
+ ss[x] = v
64+
+ }
65+
+ i = ss
66+
+ case *gpb.TypedValue_BytesVal:
67+
+ i = tv.GetBytesVal()
68+
+ case *gpb.TypedValue_JsonIetfVal:
69+
+ var val interface{}
70+
+ val = tv.GetJsonIetfVal()
71+
+ json.Unmarshal(val.([]byte), &i)
72+
+ //i = tv.GetJsonIetfVal()
73+
+ default:
74+
+ return nil, fmt.Errorf("non-scalar type %+v", tv.Value)
75+
+ }
76+
+ return i, nil
77+
+}
78+
+
79+
func noti(prefix []string, pp *gpb.Path, ts time.Time, u *gpb.Update) (client.Notification, error) {
80+
sp := path.ToStrings(pp, false)
81+
// Make a full new copy of prefix + u.Path to avoid any reuse of underlying
82+
@@ -274,7 +320,7 @@
83+
return client.Delete{Path: p, TS: ts}, nil
84+
}
85+
if u.Val != nil {
86+
- val, err := value.ToScalar(u.Val)
87+
+ val, err := toScalar(u.Val)
88+
if err != nil {
89+
return nil, err
90+
}
91+
92+
diff --git a/vendor/github.com/openconfig/gnmi/value/value.go b/vendor/github.com/openconfig/gnmi/value/value.go
93+
index e851a4b..6e79264 100644
94+
--- a/vendor/github.com/openconfig/gnmi/value/value.go 2018-8-17 14:03:29.847103498 -0800
95+
+++ b/vendor/github.com/openconfig/gnmi/value/value.go 2018-8-18 13:48:49.234145530 -0700
96+
@@ -117,6 +117,8 @@
97+
i = ss
98+
case *pb.TypedValue_BytesVal:
99+
i = tv.GetBytesVal()
100+
+ case *pb.TypedValue_JsonIetfVal:
101+
+ i = string(tv.GetJsonIetfVal())
102+
default:
103+
return nil, fmt.Errorf("non-scalar type %+v", tv.Value)
104+
}

patches/gnmi_cli.all.patch

+2-81
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
11
patch--- ./github.com/openconfig/gnmi/client/gnmi/client.go 2019-11-22 14:03:29.839103602 -0800
22
+++ ./github.com/openconfig/gnmi/client/gnmi/client.go 2019-10-11 13:48:49.226145599 -0700
3-
@@ -37,7 +37,6 @@
4-
"github.com/openconfig/gnmi/client"
5-
"github.com/openconfig/gnmi/client/grpcutil"
6-
"github.com/openconfig/gnmi/path"
7-
- "github.com/openconfig/gnmi/value"
8-
9-
gpb "github.com/openconfig/gnmi/proto/gnmi"
10-
)
11-
@@ -246,7 +245,7 @@
3+
@@ -246,7 +246,7 @@
124
s := &gpb.SubscribeRequest_Subscribe{
135
Subscribe: &gpb.SubscriptionList{
146
Mode: getType(q.Type),
@@ -17,7 +9,7 @@ patch--- ./github.com/openconfig/gnmi/client/gnmi/client.go 2019-11-22 14:03:29.
179
},
1810
}
1911
if q.UpdatesOnly {
20-
@@ -257,11 +256,58 @@
12+
@@ -257,7 +257,7 @@
2113
if err != nil {
2214
return nil, fmt.Errorf("invalid query path %q: %v", qq, err)
2315
}
@@ -26,66 +18,6 @@ patch--- ./github.com/openconfig/gnmi/client/gnmi/client.go 2019-11-22 14:03:29.
2618
}
2719
return &gpb.SubscribeRequest{Request: s}, nil
2820
}
29-
30-
+// decimalToFloat converts a *gnmi_proto.Decimal64 to a float32. Downcasting to float32 is performed as the
31-
+// precision of a float64 is not required.
32-
+func decimalToFloat(d *gpb.Decimal64) float32 {
33-
+ return float32(float64(d.Digits) / math.Pow(10, float64(d.Precision)))
34-
+}
35-
+
36-
+// ToScalar will convert TypedValue scalar types to a Go native type. It will
37-
+// return an error if the TypedValue does not contain a scalar type.
38-
+func toScalar(tv *gpb.TypedValue) (interface{}, error) {
39-
+ var i interface{}
40-
+ switch tv.Value.(type) {
41-
+ case *gpb.TypedValue_DecimalVal:
42-
+ i = decimalToFloat(tv.GetDecimalVal())
43-
+ case *gpb.TypedValue_StringVal:
44-
+ i = tv.GetStringVal()
45-
+ case *gpb.TypedValue_IntVal:
46-
+ i = tv.GetIntVal()
47-
+ case *gpb.TypedValue_UintVal:
48-
+ i = tv.GetUintVal()
49-
+ case *gpb.TypedValue_BoolVal:
50-
+ i = tv.GetBoolVal()
51-
+ case *gpb.TypedValue_FloatVal:
52-
+ i = tv.GetFloatVal()
53-
+ case *gpb.TypedValue_LeaflistVal:
54-
+ elems := tv.GetLeaflistVal().GetElement()
55-
+ ss := make([]interface{}, len(elems))
56-
+ for x, e := range elems {
57-
+ v, err := toScalar(e)
58-
+ if err != nil {
59-
+ return nil, fmt.Errorf("ToScalar for ScalarArray %+v: %v", e.Value, err)
60-
+ }
61-
+ ss[x] = v
62-
+ }
63-
+ i = ss
64-
+ case *gpb.TypedValue_BytesVal:
65-
+ i = tv.GetBytesVal()
66-
+ case *gpb.TypedValue_JsonIetfVal:
67-
+ var val interface{}
68-
+ val = tv.GetJsonIetfVal()
69-
+ json.Unmarshal(val.([]byte), &i)
70-
+ //i = tv.GetJsonIetfVal()
71-
+ default:
72-
+ return nil, fmt.Errorf("non-scalar type %+v", tv.Value)
73-
+ }
74-
+ return i, nil
75-
+}
76-
+
77-
func noti(prefix []string, pp *gpb.Path, ts time.Time, u *gpb.Update) (client.Notification, error) {
78-
sp := path.ToStrings(pp, false)
79-
// Make a full new copy of prefix + u.Path to avoid any reuse of underlying
80-
@@ -274,7 +320,7 @@
81-
return client.Delete{Path: p, TS: ts}, nil
82-
}
83-
if u.Val != nil {
84-
- val, err := value.ToScalar(u.Val)
85-
+ val, err := toScalar(u.Val)
86-
if err != nil {
87-
return nil, err
88-
}
8921
--- ./github.com/openconfig/gnmi/cmd/gnmi_cli/gnmi_cli.go 2019-11-22 14:03:29.839103602 -0800
9022
+++ ./github.com/openconfig/gnmi/cmd/gnmi_cli/gnmi_cli.go 2019-11-21 09:30:52.453893674 -0800
9123
@@ -76,6 +76,11 @@
@@ -153,14 +85,3 @@ patch--- ./github.com/openconfig/gnmi/client/gnmi/client.go 2019-11-22 14:03:29.
15385
}
15486

15587
// Destination extracts a Destination instance out of Query fields.
156-
--- ./github.com/openconfig/gnmi/value/value.go 2019-11-22 14:03:29.847103498 -0800
157-
+++ ./github.com/openconfig/gnmi/value/value.go 2019-10-11 13:48:49.234145530 -0700
158-
@@ -117,6 +117,8 @@
159-
i = ss
160-
case *pb.TypedValue_BytesVal:
161-
i = tv.GetBytesVal()
162-
+ case *pb.TypedValue_JsonIetfVal:
163-
+ i = string(tv.GetJsonIetfVal())
164-
default:
165-
return nil, fmt.Errorf("non-scalar type %+v", tv.Value)
166-
}

0 commit comments

Comments
 (0)