-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #139 from openconfig/request-values
Context RequestValues for ygnmi queries
- Loading branch information
Showing
11 changed files
with
457 additions
and
55 deletions.
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() { | ||
if q.IsState() { | ||
return context.WithValue(ctx, compressedStateQuery{}, true) | ||
} else { | ||
return context.WithValue(ctx, compressedConfigQuery{}, true) | ||
} | ||
} | ||
return ctx | ||
} | ||
|
||
type compressedConfigQuery struct{} | ||
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.