-
Notifications
You must be signed in to change notification settings - Fork 4
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
Context RequestValues for ygnmi queries #139
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright 2023 Google Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package ygnmi | ||
|
||
import ( | ||
"context" | ||
) | ||
|
||
// RequestValues contains request-scoped values for ygnmi queries. | ||
type RequestValues struct { | ||
// CompressedConfigQuery is a key type that means that the query is | ||
// uninterested in /state paths. | ||
CompressedConfigQuery bool | ||
// CompressedStateQuery is a key type that means that the query is | ||
// uninterested in /config paths. | ||
CompressedStateQuery bool | ||
} | ||
|
||
// FromContext extracts certain ygnmi request-scoped values, if present. | ||
func FromContext(ctx context.Context) *RequestValues { | ||
compConfig, _ := ctx.Value(compressedConfigQuery{}).(bool) | ||
compState, _ := ctx.Value(compressedStateQuery{}).(bool) | ||
return &RequestValues{ | ||
CompressedConfigQuery: compConfig, | ||
CompressedStateQuery: compState, | ||
} | ||
} | ||
|
||
// NewContext returns a new Context carrying ygnmi request-scoped values. | ||
func NewContext(ctx context.Context, q UntypedQuery) context.Context { | ||
if q.isCompressedSchema() { | ||
wenovus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if q.IsState() { | ||
return context.WithValue(ctx, compressedStateQuery{}, true) | ||
} else { | ||
return context.WithValue(ctx, compressedConfigQuery{}, true) | ||
} | ||
} | ||
return ctx | ||
} | ||
|
||
type compressedConfigQuery struct{} | ||
wenovus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
type compressedStateQuery struct{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Copyright 2023 Google Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package ygnmi_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/openconfig/ygnmi/exampleoc/exampleocpath" | ||
"github.com/openconfig/ygnmi/internal/uexampleoc/uexampleocpath" | ||
"github.com/openconfig/ygnmi/ygnmi" | ||
) | ||
|
||
func TestFromContext(t *testing.T) { | ||
tests := []struct { | ||
desc string | ||
inContext context.Context | ||
wantRequestValues *ygnmi.RequestValues | ||
}{{ | ||
desc: "compress-config", | ||
inContext: ygnmi.NewContext(context.Background(), exampleocpath.Root().Parent().Config()), | ||
wantRequestValues: &ygnmi.RequestValues{ | ||
CompressedConfigQuery: true, | ||
CompressedStateQuery: false, | ||
}, | ||
}, { | ||
desc: "compress-config-leaf", | ||
inContext: ygnmi.NewContext(context.Background(), exampleocpath.Root().Parent().Child().Five().Config()), | ||
wantRequestValues: &ygnmi.RequestValues{ | ||
CompressedConfigQuery: true, | ||
CompressedStateQuery: false, | ||
}, | ||
}, { | ||
desc: "compress-state", | ||
inContext: ygnmi.NewContext(context.Background(), exampleocpath.Root().Parent().State()), | ||
wantRequestValues: &ygnmi.RequestValues{ | ||
CompressedConfigQuery: false, | ||
CompressedStateQuery: true, | ||
}, | ||
}, { | ||
desc: "compress-state-leaf", | ||
inContext: ygnmi.NewContext(context.Background(), exampleocpath.Root().Parent().Child().Five().State()), | ||
wantRequestValues: &ygnmi.RequestValues{ | ||
CompressedConfigQuery: false, | ||
CompressedStateQuery: true, | ||
}, | ||
}, { | ||
desc: "uncompressed-container", | ||
inContext: ygnmi.NewContext(context.Background(), uexampleocpath.Root().Parent()), | ||
wantRequestValues: &ygnmi.RequestValues{ | ||
CompressedConfigQuery: false, | ||
CompressedStateQuery: false, | ||
}, | ||
}, { | ||
desc: "uncompressed-config-container", | ||
inContext: ygnmi.NewContext(context.Background(), uexampleocpath.Root().Parent().Child().Config()), | ||
wantRequestValues: &ygnmi.RequestValues{ | ||
CompressedConfigQuery: false, | ||
CompressedStateQuery: false, | ||
}, | ||
}, { | ||
desc: "uncompressed-leaf", | ||
inContext: ygnmi.NewContext(context.Background(), uexampleocpath.Root().Parent().Child().Config().Five()), | ||
wantRequestValues: &ygnmi.RequestValues{ | ||
CompressedConfigQuery: false, | ||
CompressedStateQuery: false, | ||
}, | ||
}} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.desc, func(t *testing.T) { | ||
got := ygnmi.FromContext(tt.inContext) | ||
if diff := cmp.Diff(tt.wantRequestValues, got); diff != "" { | ||
t.Errorf("(-want, +got):\n%s", diff) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like q.IsState() now becomes two boolean. This added some impossible combinations that the consumer of this struct will have to deal with.
Why not just do
IsState bool
here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought about it, but I thought it would be more direct to have these fields and have the clear, directly comments rather than requiring any observers to know about the path filtering properties. It's impossible for the conflicting situation to occur when done correctly. WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Previously I thought
CompressedConfigQuery == false && CompressedStateQuery == false
is an impossible condition as well as both true, but #140 clarified that both could be false when the query is not compressed. I don't think I fully understand why it matters if it's a compressed query.Another way to design this with less ambiguity is to make it a call to action, i.e.
WantConfig
andWantState
. It focuses on telling what the caller should do rather than relying on their judgment on what the signals meant. I guess if the query is not compressed, then we do want to keep both config and state in the result?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ygot-generated code comes in two forms: compressed and uncompressed. When compressed it can only store either config or state, but not both. That's why it's possible for both
CompressedConfigQuery
andCompressedStateQuery
to be false.I agree a better name might be
ConfigFiltered
, andStateFiltered
. This is the most direct call-to-action.I've updated #140 to address this as well.