File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -24,10 +24,15 @@ const (
2424)
2525
2626// DeviationEntry is one deviating path with its expected and current values.
27+ // Expected and Current are display strings; ExpectedText and CurrentText carry
28+ // the same values in the stable text encoding that stored deviations use. An
29+ // absent value is empty in both forms.
2730type DeviationEntry struct {
28- Path string
29- Expected string
30- Current string
31+ Path string
32+ Expected string
33+ Current string
34+ ExpectedText string
35+ CurrentText string
3136}
3237
3338// DeviationEvent is emitted by a deviation watch. Intent, Reason and Entries
Original file line number Diff line number Diff line change @@ -145,6 +145,9 @@ func toDeviationEvent(rsp *sdcpb.WatchDeviationResponse) api.DeviationEvent {
145145 Path : rsp .GetPath ().ToXPath (false ),
146146 Expected : valueString (rsp .GetExpectedValue ()),
147147 Current : valueString (rsp .GetCurrentValue ()),
148+
149+ ExpectedText : valueText (rsp .GetExpectedValue ()),
150+ CurrentText : valueText (rsp .GetCurrentValue ()),
148151 }}
149152 }
150153 return ev
Original file line number Diff line number Diff line change @@ -13,6 +13,7 @@ import (
1313 "github.com/sdcio/data-server/pkg/datastore"
1414 targettypes "github.com/sdcio/data-server/pkg/datastore/target/types"
1515 sdcpb "github.com/sdcio/sdc-protos/sdcpb"
16+ "google.golang.org/protobuf/encoding/prototext"
1617)
1718
1819const ifPath = "/interface[name=ethernet-1/1]"
@@ -375,3 +376,21 @@ func TestDeviationWatcherSendUnblocksOnCancel(t *testing.T) {
375376 t .Fatal ("send after close must fail" )
376377 }
377378}
379+
380+ func TestToDeviationEventEntryText (t * testing.T ) {
381+ rsp := & sdcpb.WatchDeviationResponse {
382+ Event : sdcpb .DeviationEvent_UPDATE ,
383+ ExpectedValue : & sdcpb.TypedValue {Value : & sdcpb.TypedValue_StringVal {StringVal : "a" }},
384+ }
385+ e := toDeviationEvent (rsp ).Entries [0 ]
386+ var tv sdcpb.TypedValue
387+ if err := prototext .Unmarshal ([]byte (e .ExpectedText ), & tv ); err != nil {
388+ t .Fatalf ("ExpectedText is not prototext: %v" , err )
389+ }
390+ if tv .GetStringVal () != "a" {
391+ t .Fatalf ("round trip mismatch: %v" , & tv )
392+ }
393+ if e .CurrentText != "" {
394+ t .Fatalf ("absent value must be empty, got %q" , e .CurrentText )
395+ }
396+ }
Original file line number Diff line number Diff line change 44 "context"
55 "fmt"
66
7+ "google.golang.org/protobuf/encoding/prototext"
8+
79 "github.com/sdcio/data-server/pkg/api"
810 "github.com/sdcio/data-server/pkg/datastore"
911 "github.com/sdcio/data-server/pkg/datastore/types"
@@ -175,3 +177,15 @@ func valueString(tv *sdcpb.TypedValue) string {
175177 }
176178 return tv .ToString ()
177179}
180+
181+ // valueText encodes a typed value as prototext; nil encodes as empty.
182+ func valueText (tv * sdcpb.TypedValue ) string {
183+ if tv == nil {
184+ return ""
185+ }
186+ b , err := prototext .Marshal (tv )
187+ if err != nil {
188+ return ""
189+ }
190+ return string (b )
191+ }
You can’t perform that action at this time.
0 commit comments