Skip to content

Commit b46e000

Browse files
authored
impl(sidekick): bindings for discovery methods (#2259)
Parse the `path` field in a discovery method into the `PathInfo` field of the `api.Method` objects.
1 parent 2835eda commit b46e000

File tree

5 files changed

+81
-13
lines changed

5 files changed

+81
-13
lines changed

internal/sidekick/internal/parser/discovery/discovery.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func NewAPI(serviceConfig *serviceconfig.Service, contents []byte) (*api.API, er
8888
}
8989

9090
for _, resource := range doc.Resources {
91-
if err := addServiceRecursive(result, resource); err != nil {
91+
if err := addServiceRecursive(result, doc, resource); err != nil {
9292
return nil, err
9393
}
9494
}

internal/sidekick/internal/parser/discovery/methods.go

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@ package discovery
1616

1717
import (
1818
"fmt"
19+
"strings"
1920

2021
"github.com/googleapis/librarian/internal/sidekick/internal/api"
2122
)
2223

23-
func makeServiceMethods(model *api.API, serviceID string, resource *resource) ([]*api.Method, error) {
24+
func makeServiceMethods(model *api.API, serviceID string, doc *document, resource *resource) ([]*api.Method, error) {
2425
var methods []*api.Method
2526
for _, input := range resource.Methods {
26-
method, err := makeMethod(model, serviceID, input)
27+
method, err := makeMethod(model, serviceID, doc, input)
2728
if err != nil {
2829
return nil, err
2930
}
@@ -33,7 +34,7 @@ func makeServiceMethods(model *api.API, serviceID string, resource *resource) ([
3334
return methods, nil
3435
}
3536

36-
func makeMethod(model *api.API, serviceID string, input *method) (*api.Method, error) {
37+
func makeMethod(model *api.API, serviceID string, doc *document, input *method) (*api.Method, error) {
3738
id := fmt.Sprintf("%s.%s", serviceID, input.Name)
3839
if input.MediaUpload != nil {
3940
return nil, fmt.Errorf("media upload methods are not supported, id=%s", id)
@@ -46,6 +47,27 @@ func makeMethod(model *api.API, serviceID string, input *method) (*api.Method, e
4647
if err != nil {
4748
return nil, err
4849
}
50+
var uriTemplate string
51+
if strings.HasSuffix(doc.ServicePath, "/") {
52+
uriTemplate = fmt.Sprintf("%s%s", doc.ServicePath, input.Path)
53+
} else {
54+
uriTemplate = fmt.Sprintf("%s/%s", doc.ServicePath, input.Path)
55+
}
56+
uriTemplate = strings.TrimPrefix(uriTemplate, "/")
57+
path, err := ParseUriTemplate(uriTemplate)
58+
if err != nil {
59+
return nil, err
60+
}
61+
binding := &api.PathBinding{
62+
Verb: input.HTTPMethod,
63+
PathTemplate: path,
64+
QueryParameters: map[string]bool{},
65+
}
66+
for _, p := range input.Parameters {
67+
if p.Location != "path" {
68+
binding.QueryParameters[p.Name] = true
69+
}
70+
}
4971
method := &api.Method{
5072
ID: id,
5173
Name: input.Name,
@@ -54,6 +76,10 @@ func makeMethod(model *api.API, serviceID string, input *method) (*api.Method, e
5476
// Deprecated: ...,
5577
InputTypeID: inputID,
5678
OutputTypeID: outputID,
79+
PathInfo: &api.PathInfo{
80+
Bindings: []*api.PathBinding{binding},
81+
BodyFieldPath: "*",
82+
},
5783
}
5884
return method, nil
5985
}

internal/sidekick/internal/parser/discovery/methods_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ func TestMakeServiceMethodsError(t *testing.T) {
2121
if err != nil {
2222
t.Fatal(err)
2323
}
24+
doc := document{}
2425
input := &resource{
2526
Name: "testResource",
2627
Methods: []*method{
@@ -30,7 +31,7 @@ func TestMakeServiceMethodsError(t *testing.T) {
3031
},
3132
},
3233
}
33-
if methods, err := makeServiceMethods(model, "..testResource", input); err == nil {
34+
if methods, err := makeServiceMethods(model, "..testResource", &doc, input); err == nil {
3435
t.Errorf("expected error on method with media upload, got=%v", methods)
3536
}
3637
}
@@ -48,8 +49,10 @@ func TestMakeMethodError(t *testing.T) {
4849
{"mediaUploadMustBeNil", method{MediaUpload: &mediaUpload{}}},
4950
{"requestMustHaveRef", method{Request: &schema{}}},
5051
{"responseMustHaveRef", method{Response: &schema{}}},
52+
{"badPath", method{Path: "{+var"}},
5153
} {
52-
if method, err := makeMethod(model, "..Test", &test.Input); err == nil {
54+
doc := document{}
55+
if method, err := makeMethod(model, "..Test", &doc, &test.Input); err == nil {
5356
t.Errorf("expected error on method[%s], got=%v", test.Name, method)
5457
}
5558
}

internal/sidekick/internal/parser/discovery/services.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,23 @@ import (
2020
"github.com/googleapis/librarian/internal/sidekick/internal/api"
2121
)
2222

23-
func addServiceRecursive(model *api.API, resource *resource) error {
23+
func addServiceRecursive(model *api.API, doc *document, resource *resource) error {
2424
if len(resource.Methods) != 0 {
25-
if err := addService(model, resource); err != nil {
25+
if err := addService(model, doc, resource); err != nil {
2626
return err
2727
}
2828
}
2929
for _, child := range resource.Resources {
30-
if err := addServiceRecursive(model, child); err != nil {
30+
if err := addServiceRecursive(model, doc, child); err != nil {
3131
return err
3232
}
3333
}
3434
return nil
3535
}
3636

37-
func addService(model *api.API, resource *resource) error {
37+
func addService(model *api.API, doc *document, resource *resource) error {
3838
id := fmt.Sprintf(".%s.%s", model.PackageName, resource.Name)
39-
methods, err := makeServiceMethods(model, id, resource)
39+
methods, err := makeServiceMethods(model, id, doc, resource)
4040
if err != nil {
4141
return err
4242
}

internal/sidekick/internal/parser/discovery/services_test.go

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,50 @@ func TestService(t *testing.T) {
4444
Documentation: "Returns the specified Zone resource.",
4545
InputTypeID: ".google.protobuf.Empty",
4646
OutputTypeID: "..Zone",
47+
PathInfo: &api.PathInfo{
48+
Bindings: []*api.PathBinding{
49+
{
50+
Verb: "GET",
51+
PathTemplate: api.NewPathTemplate().
52+
WithLiteral("compute").
53+
WithLiteral("v1").
54+
WithLiteral("projects").
55+
WithVariableNamed("project").
56+
WithLiteral("zones").
57+
WithVariableNamed("zone"),
58+
QueryParameters: map[string]bool{},
59+
},
60+
},
61+
BodyFieldPath: "*",
62+
},
4763
},
4864
{
4965
ID: "..zones.list",
5066
Name: "list",
5167
Documentation: "Retrieves the list of Zone resources available to the specified project.",
5268
InputTypeID: ".google.protobuf.Empty",
5369
OutputTypeID: "..ZoneList",
70+
PathInfo: &api.PathInfo{
71+
Bindings: []*api.PathBinding{
72+
{
73+
Verb: "GET",
74+
PathTemplate: api.NewPathTemplate().
75+
WithLiteral("compute").
76+
WithLiteral("v1").
77+
WithLiteral("projects").
78+
WithVariableNamed("project").
79+
WithLiteral("zones"),
80+
QueryParameters: map[string]bool{
81+
"filter": true,
82+
"maxResults": true,
83+
"orderBy": true,
84+
"pageToken": true,
85+
"returnPartialSuccess": true,
86+
},
87+
},
88+
},
89+
BodyFieldPath: "*",
90+
},
5491
},
5592
},
5693
}
@@ -62,12 +99,13 @@ func TestServiceTopLevelMethodErrors(t *testing.T) {
6299
if err != nil {
63100
t.Fatal(err)
64101
}
102+
doc := document{}
65103
input := resource{
66104
Methods: []*method{
67105
{MediaUpload: &mediaUpload{}},
68106
},
69107
}
70-
if err := addServiceRecursive(model, &input); err == nil {
108+
if err := addServiceRecursive(model, &doc, &input); err == nil {
71109
t.Errorf("expected error in addServiceRecursive invalid top-level method, got=%v", model.Services)
72110
}
73111
}
@@ -77,6 +115,7 @@ func TestServiceChildMethodErrors(t *testing.T) {
77115
if err != nil {
78116
t.Fatal(err)
79117
}
118+
doc := document{}
80119
input := resource{
81120
Resources: []*resource{
82121
{
@@ -86,7 +125,7 @@ func TestServiceChildMethodErrors(t *testing.T) {
86125
},
87126
},
88127
}
89-
if err := addServiceRecursive(model, &input); err == nil {
128+
if err := addServiceRecursive(model, &doc, &input); err == nil {
90129
t.Errorf("expected error in addServiceRecursive invalid child method, got=%v", model.Services)
91130
}
92131
}

0 commit comments

Comments
 (0)