Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use new UntypedQuery for Batch.AddPaths #134

Merged
merged 1 commit into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
36 changes: 25 additions & 11 deletions ygnmi/ygnmi.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@ import (
gpb "github.com/openconfig/gnmi/proto/gnmi"
)

// AnyQuery is a generic gNMI query for wildcard or non-wildcard state or config paths.
// UntypedQuery is a generic gNMI query for wildcard or non-wildcard state or config paths.
// Supported operations: Batch.
type AnyQuery[T any] interface {
//
// Since it is untyped, it is only suitable for representing the metadata of
// the query instead of initiating gNMI operations.
type UntypedQuery interface {
// PathStruct returns to path struct used for unmarshalling and schema validation.
// This path must correspond to T (the parameterized type of the interface).
PathStruct() PathStruct
Expand All @@ -46,9 +49,6 @@ type AnyQuery[T any] interface {
// goStruct returns the struct that query should be unmarshalled into.
// For leaves, this is the parent.
goStruct() ygot.ValidatedGoStruct
// extract is used for leaves to return the field from the parent GoStruct.
// For non-leaves, this casts the GoStruct to the concrete type.
extract(ygot.ValidatedGoStruct) (T, bool)
// IsState returns if the path for this query is a state node.
IsState() bool
// isLeaf returns if the path for this query is a leaf.
Expand All @@ -66,6 +66,14 @@ type AnyQuery[T any] interface {
compressInfo() *CompressionInfo
}

// AnyQuery is a generic gNMI query for wildcard or non-wildcard state or config paths.
type AnyQuery[T any] interface {
UntypedQuery
// extract is used for leaves to return the field from the parent GoStruct.
// For non-leaves, this casts the GoStruct to the concrete type.
extract(ygot.ValidatedGoStruct) (T, bool)
}

// SingletonQuery is a non-wildcard gNMI query.
type SingletonQuery[T any] interface {
AnyQuery[T]
Expand Down Expand Up @@ -727,21 +735,24 @@ func NewBatch[T any](root SingletonQuery[T]) *Batch[T] {
}

// AddPaths adds the paths to the batch. Paths must be children of the root.
func (b *Batch[T]) AddPaths(paths ...PathStruct) error {
func (b *Batch[T]) AddPaths(paths ...UntypedQuery) error {
wenovus marked this conversation as resolved.
Show resolved Hide resolved
root, err := resolvePath(b.root.PathStruct())
if err != nil {
return err
}
var pathstructs []PathStruct
for _, path := range paths {
p, err := resolvePath(path)
ps := path.PathStruct()
pathstructs = append(pathstructs, ps)
p, err := resolvePath(ps)
if err != nil {
return err
}
if !util.PathMatchesQuery(p, root) {
return fmt.Errorf("root path %v is not a prefix of %v", root, p)
}
}
b.paths = append(b.paths, paths...)
b.paths = append(b.paths, pathstructs...)
return nil
}

Expand Down Expand Up @@ -782,21 +793,24 @@ func NewWildcardBatch[T any](root WildcardQuery[T]) *WildcardBatch[T] {
}

// AddPaths adds the paths to the batch. Paths must be children of the root.
func (b *WildcardBatch[T]) AddPaths(paths ...PathStruct) error {
func (b *WildcardBatch[T]) AddPaths(paths ...UntypedQuery) error {
root, err := resolvePath(b.root.PathStruct())
if err != nil {
return err
}
var pathstructs []PathStruct
for _, path := range paths {
p, err := resolvePath(path)
ps := path.PathStruct()
pathstructs = append(pathstructs, ps)
p, err := resolvePath(ps)
if err != nil {
return err
}
if !util.PathMatchesQuery(p, root) {
return fmt.Errorf("root path %v is not a prefix of %v", root, p)
}
}
b.paths = append(b.paths, paths...)
b.paths = append(b.paths, pathstructs...)
return nil
}

Expand Down
26 changes: 13 additions & 13 deletions ygnmi/ygnmi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4221,16 +4221,16 @@ func TestCustomRootBatch(t *testing.T) {
tests := []struct {
desc string
stub func(s *testutil.Stubber)
paths []ygnmi.PathStruct
paths []ygnmi.UntypedQuery
wantSubscriptionPath []*gpb.Path
wantVal *ygnmi.Value[*exampleoc.Parent]
wantAddErr string
wantLookupErr string
}{{
desc: "not prefix",
stub: func(s *testutil.Stubber) {},
paths: []ygnmi.PathStruct{
exampleocpath.Root().Model(),
paths: []ygnmi.UntypedQuery{
exampleocpath.Root().Model().Config(),
},
wantAddErr: "is not a prefix",
}, {
Expand All @@ -4244,8 +4244,8 @@ func TestCustomRootBatch(t *testing.T) {
}},
}).Sync()
},
paths: []ygnmi.PathStruct{
exampleocpath.Root().Parent().Child().Two(),
paths: []ygnmi.UntypedQuery{
exampleocpath.Root().Parent().Child().Two().State(),
},
wantSubscriptionPath: []*gpb.Path{
twoPath,
Expand Down Expand Up @@ -4333,8 +4333,8 @@ func TestCustomRootBatch(t *testing.T) {
modelPath := exampleocpath.Root().Model()
b := ygnmi.NewBatch(modelPath.SingleKeyMap().State())
if err := b.AddPaths(
modelPath.SingleKeyAny().Key().State().PathStruct(),
modelPath.SingleKeyAny().Value().State().PathStruct(),
modelPath.SingleKeyAny().Key().State(),
modelPath.SingleKeyAny().Value().State(),
); err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -4384,16 +4384,16 @@ func TestCustomRootWildcardBatch(t *testing.T) {
tests := []struct {
desc string
stub func(s *testutil.Stubber)
paths []ygnmi.PathStruct
paths []ygnmi.UntypedQuery
wantSubscriptionPath []*gpb.Path
wantVal []*ygnmi.Value[*exampleoc.Model_SingleKey]
wantAddErr string
wantLookupErr string
}{{
desc: "not prefix",
stub: func(s *testutil.Stubber) {},
paths: []ygnmi.PathStruct{
exampleocpath.Root().Model(),
paths: []ygnmi.UntypedQuery{
exampleocpath.Root().Model().Config(),
},
wantAddErr: "is not a prefix",
}, {
Expand All @@ -4410,9 +4410,9 @@ func TestCustomRootWildcardBatch(t *testing.T) {
}},
}).Sync()
},
paths: []ygnmi.PathStruct{
exampleocpath.Root().Model().SingleKeyAny().Value().State().PathStruct(),
exampleocpath.Root().Model().SingleKeyAny().Key().State().PathStruct(),
paths: []ygnmi.UntypedQuery{
exampleocpath.Root().Model().SingleKeyAny().Value().State(),
exampleocpath.Root().Model().SingleKeyAny().Key().State(),
},
wantSubscriptionPath: []*gpb.Path{
keyPathWild,
Expand Down
6 changes: 3 additions & 3 deletions ygnmi/ygnmi_uncompressed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,15 +459,15 @@ func TestUncompressedCustomRootBatch(t *testing.T) {
tests := []struct {
desc string
stub func(s *testutil.Stubber)
paths []ygnmi.PathStruct
paths []ygnmi.UntypedQuery
wantSubscriptionPath []*gpb.Path
wantVal *ygnmi.Value[*uexampleoc.OpenconfigSimple_Parent]
wantAddErr string
wantLookupErr string
}{{
desc: "not prefix",
stub: func(s *testutil.Stubber) {},
paths: []ygnmi.PathStruct{
paths: []ygnmi.UntypedQuery{
uexampleocpath.Root().Model(),
},
wantAddErr: "is not a prefix",
Expand All @@ -482,7 +482,7 @@ func TestUncompressedCustomRootBatch(t *testing.T) {
}},
}).Sync()
},
paths: []ygnmi.PathStruct{
paths: []ygnmi.UntypedQuery{
uexampleocpath.Root().Parent().Child().State().Two(),
},
wantSubscriptionPath: []*gpb.Path{
Expand Down