Skip to content

Commit 5e9c48e

Browse files
committed
feat(api): expose deviation values in stable text encoding
Signed-off-by: Markus Vahlenkamp <markus.vahlenkamp@nokia.com>
1 parent 606993a commit 5e9c48e

4 files changed

Lines changed: 44 additions & 3 deletions

File tree

‎pkg/api/deviation.go‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff 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.
2730
type 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

‎pkg/core/deviation.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff 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

‎pkg/core/rpc_test.go‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff 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

1819
const 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+
}

‎pkg/core/transaction.go‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
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+
}

0 commit comments

Comments
 (0)