-
Notifications
You must be signed in to change notification settings - Fork 50
feat(sidekick): parse discovery doc path templates #2241
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
Merged
coryan
merged 2 commits into
googleapis:main
from
coryan:pr02-sidekick-parse-path-templates
Sep 19, 2025
Merged
Changes from all commits
Commits
Show all changes
2 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
126 changes: 126 additions & 0 deletions
126
internal/sidekick/internal/parser/discovery/uritemplate.go
This file contains hidden or 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,126 @@ | ||||||||||
| // Copyright 2025 Google LLC | ||||||||||
| // | ||||||||||
| // 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 | ||||||||||
| // | ||||||||||
| // https://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 discovery | ||||||||||
|
|
||||||||||
| import ( | ||||||||||
| "fmt" | ||||||||||
| "regexp" | ||||||||||
| "strings" | ||||||||||
|
|
||||||||||
| "github.com/googleapis/librarian/internal/sidekick/internal/api" | ||||||||||
| ) | ||||||||||
|
|
||||||||||
| const ( | ||||||||||
| beginExpression = '{' | ||||||||||
| endExpression = '}' | ||||||||||
| slash = '/' | ||||||||||
| ) | ||||||||||
|
|
||||||||||
| var ( | ||||||||||
| identifierRe = regexp.MustCompile("[A-Za-z][A-Za-z0-9_]*") | ||||||||||
| ) | ||||||||||
|
|
||||||||||
| // ParseUriTemplate parses a [RFC 6570] URI template as an `api.PathTemplate`. | ||||||||||
| // | ||||||||||
| // In sidekick we need to capture the structure of the URI template for the | ||||||||||
| // codec(s) to emit good templates with them. | ||||||||||
| func ParseUriTemplate(uriTemplate string) (*api.PathTemplate, error) { | ||||||||||
| template := &api.PathTemplate{} | ||||||||||
| var pos int | ||||||||||
| for { | ||||||||||
| var err error | ||||||||||
| var segment *api.PathSegment | ||||||||||
| var width int | ||||||||||
|
|
||||||||||
| if pos == len(uriTemplate) { | ||||||||||
| return nil, fmt.Errorf("expected a segment, found eof: %s", uriTemplate) | ||||||||||
| } | ||||||||||
| if uriTemplate[pos] == beginExpression { | ||||||||||
| segment, width, err = parseExpression(uriTemplate[pos:]) | ||||||||||
| } else { | ||||||||||
| segment, width, err = parseLiteral(uriTemplate[pos:]) | ||||||||||
| } | ||||||||||
| if err != nil { | ||||||||||
| return nil, err | ||||||||||
| } | ||||||||||
| template.Segments = append(template.Segments, *segment) | ||||||||||
| pos += width | ||||||||||
| if pos == len(uriTemplate) || uriTemplate[pos] != slash { | ||||||||||
| break | ||||||||||
| } | ||||||||||
| pos++ // Skip slash | ||||||||||
| } | ||||||||||
| if pos != len(uriTemplate) { | ||||||||||
| return nil, fmt.Errorf("trailing data (%q) cannot be parsed as a URI template", uriTemplate[pos:]) | ||||||||||
| } | ||||||||||
| return template, nil | ||||||||||
| } | ||||||||||
|
|
||||||||||
| func parseExpression(input string) (*api.PathSegment, int, error) { | ||||||||||
| if input == "" || input[0] != beginExpression { | ||||||||||
| return nil, 0, fmt.Errorf("missing `{` character in expression %q", input) | ||||||||||
| } | ||||||||||
| tail := input[1:] | ||||||||||
| if strings.IndexAny(tail, "+#") == 0 { | ||||||||||
| return nil, 0, fmt.Errorf("level 2 expressions unsupported input=%q", input) | ||||||||||
| } | ||||||||||
| if strings.IndexAny(tail, "./?&") == 0 { | ||||||||||
| return nil, 0, fmt.Errorf("level 3 expressions unsupported input=%q", input) | ||||||||||
| } | ||||||||||
| if strings.IndexAny(tail, "=,!@|") == 0 { | ||||||||||
| return nil, 0, fmt.Errorf("reserved character on expression %q", input) | ||||||||||
| } | ||||||||||
| match := identifierRe.FindStringIndex(tail) | ||||||||||
| if match[0] != 0 { | ||||||||||
| return nil, 0, fmt.Errorf("no identifier found on expression %q", input) | ||||||||||
| } | ||||||||||
| id := tail[0:match[1]] | ||||||||||
| tail = tail[match[1]:] | ||||||||||
| if tail == "" || tail[0] != endExpression { | ||||||||||
|
coryan marked this conversation as resolved.
|
||||||||||
| return nil, 0, fmt.Errorf("missing `}` character at the end of the expression %q", input) | ||||||||||
| } | ||||||||||
| return &api.PathSegment{Variable: api.NewPathVariable(id)}, match[1] + 2, nil | ||||||||||
|
coryan marked this conversation as resolved.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I missed this. But I think we want to add in a Segment with a single match wildcard librarian/internal/sidekick/internal/parser/httprule/http_rule_parser.go Lines 220 to 223 in cb430fa
|
||||||||||
| } | ||||||||||
|
|
||||||||||
| // parseLiteral() extracts a literal value from `input`. | ||||||||||
| // | ||||||||||
| // The format for literals is defined in: | ||||||||||
| // | ||||||||||
| // https://www.rfc-editor.org/rfc/rfc6570.html#section-2.1 | ||||||||||
| // | ||||||||||
| // We simplify the parsing a bit assuming most discovery docs contain valid | ||||||||||
| // URI templates. | ||||||||||
| func parseLiteral(input string) (*api.PathSegment, int, error) { | ||||||||||
| index := strings.IndexAny(input, " \"'<>\\^`{|}/") | ||||||||||
| var literal string | ||||||||||
| var tail string | ||||||||||
| var width int | ||||||||||
| if index == -1 { | ||||||||||
| literal = input | ||||||||||
| tail = "" | ||||||||||
| width = len(input) | ||||||||||
| } else { | ||||||||||
| literal = input[:index] | ||||||||||
| tail = input[index:] | ||||||||||
| width = index | ||||||||||
| } | ||||||||||
| if literal == "" { | ||||||||||
| return nil, 0, fmt.Errorf("invalid empty literal with input=%q", input) | ||||||||||
| } | ||||||||||
| if tail != "" && tail[0] != slash { | ||||||||||
| return nil, index, fmt.Errorf("found unexpected character %v in literal %q, stopped at position %v", tail[0], input, index) | ||||||||||
| } | ||||||||||
| return &api.PathSegment{Literal: &literal}, width, nil | ||||||||||
| } | ||||||||||
131 changes: 131 additions & 0 deletions
131
internal/sidekick/internal/parser/discovery/uritemplate_test.go
This file contains hidden or 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,131 @@ | ||
| // Copyright 2025 Google LLC | ||
| // | ||
| // 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 | ||
| // | ||
| // https://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 discovery | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/google/go-cmp/cmp" | ||
| "github.com/google/go-cmp/cmp/cmpopts" | ||
| "github.com/googleapis/librarian/internal/sidekick/internal/api" | ||
| ) | ||
|
|
||
| func TestParseUriTemplateSuccess(t *testing.T) { | ||
| for _, test := range []struct { | ||
| input string | ||
| want *api.PathTemplate | ||
| }{ | ||
| {"locations/global/firewallPolicies", api.NewPathTemplate().WithLiteral("locations").WithLiteral("global").WithLiteral("firewallPolicies")}, | ||
| {"locations/global/operations/{operation}", api.NewPathTemplate().WithLiteral("locations").WithLiteral("global").WithLiteral("operations").WithVariable(api.NewPathVariable("operation"))}, | ||
| {"projects/{project}/zones/{zone}/{parentName}/reservationSubBlocks", api.NewPathTemplate().WithLiteral("projects").WithVariable(api.NewPathVariable("project")).WithLiteral("zones").WithVariable(api.NewPathVariable("zone")).WithVariable(api.NewPathVariable("parentName")).WithLiteral("reservationSubBlocks")}, | ||
| } { | ||
| got, err := ParseUriTemplate(test.input) | ||
| if err != nil { | ||
| t.Errorf("expected a successful parse with input=%s, err=%v", test.input, err) | ||
| continue | ||
| } | ||
| if diff := cmp.Diff(test.want, got, cmpopts.EquateEmpty()); diff != "" { | ||
| t.Errorf("mismatch [%s] (-want, +got):\n%s", test.input, diff) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestParseUriTemplateError(t *testing.T) { | ||
| for _, test := range []struct { | ||
| input string | ||
| }{ | ||
| {"v1/{+parent}/externalAccountKeys"}, | ||
| {"a/b/c/"}, | ||
| {"a/b/c|"}, | ||
| {"a/b/{c}|"}, | ||
| {"a/b/{c}}/d"}, | ||
| {"a/b/{c}}"}, | ||
| {"a/b/{c}/"}, | ||
| {"{foo}}bar"}, | ||
|
coryan marked this conversation as resolved.
|
||
| } { | ||
| if got, err := ParseUriTemplate(test.input); err == nil { | ||
| t.Errorf("expected a parsing error with input=%s, got=%v", test.input, got) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestParseExpression(t *testing.T) { | ||
| for _, test := range []struct { | ||
| input string | ||
| want string | ||
| }{ | ||
| {"{abc}", "abc"}, | ||
| {"{Abc}", "Abc"}, | ||
| {"{abc012}", "abc012"}, | ||
| {"{abc_012}", "abc_012"}, | ||
| {"{abc_012}/foo/{bar}", "abc_012"}, | ||
| } { | ||
| gotSegment, gotWidth, err := parseExpression(test.input) | ||
| if err != nil { | ||
| t.Errorf("expected a successful parse with input=%s, err=%v", test.input, err) | ||
| continue | ||
| } | ||
| if diff := cmp.Diff(&api.PathSegment{Variable: api.NewPathVariable(test.want)}, gotSegment); diff != "" { | ||
| t.Errorf("mismatch [%s] (-want, +got):\n%s", test.input, diff) | ||
| } | ||
| if len(test.want)+2 != gotWidth { | ||
| t.Errorf("mismatch want=%d, got=%d", len(test.want), gotWidth) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestParseExpressionError(t *testing.T) { | ||
| for _, input := range []string{ | ||
| "", "(a)", | ||
| "{+a}", "{#a}", | ||
| "{.a}", "{/a}", "{?a}", "{&a}", | ||
| "{=a}", "{,a}", "{!a}", "{@a}", "{|a}", | ||
| "{a,b}", "{_abc}", "{0abc}", "{ab"} { | ||
| if gotSegment, gotWidth, err := parseExpression(input); err == nil { | ||
| t.Errorf("expected a parsing error with input=%s, gotSegment=%v, gotWidth=%v", input, gotSegment, gotWidth) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestParseLiteral(t *testing.T) { | ||
| for _, test := range []struct { | ||
| input string | ||
| want string | ||
| }{ | ||
| {"abc/def", "abc"}, | ||
| {"abcde/f", "abcde"}, | ||
| {"abcdef", "abcdef"}, | ||
| } { | ||
| gotSegment, gotWidth, err := parseLiteral(test.input) | ||
| if err != nil { | ||
| t.Errorf("expected a successful parse with input=%s, err=%v", test.input, err) | ||
| continue | ||
| } | ||
| if diff := cmp.Diff(&api.PathSegment{Literal: &test.want}, gotSegment); diff != "" { | ||
| t.Errorf("mismatch [%s] (-want, +got):\n%s", test.input, diff) | ||
| } | ||
| if len(test.want) != gotWidth { | ||
| t.Errorf("mismatch want=%d, got=%d", len(test.want), gotWidth) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestParseLiteralError(t *testing.T) { | ||
| for _, input := range []string{"", "^", "'", "/", "abc^"} { | ||
| if gotSegment, gotWidth, err := parseLiteral(input); err == nil { | ||
| t.Errorf("expected a parsing error with input=%s, gotSegment=%v, gotWidth=%v", input, gotSegment, gotWidth) | ||
| } | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.