Skip to content

Commit

Permalink
fix(genaip): invalid code generation for top-level singletons
Browse files Browse the repository at this point in the history
The `fmt` and `strings` packages are imported but not used, resulting in compile errors.

Closes #310
  • Loading branch information
vallahaye authored and radhus committed Dec 16, 2024
1 parent 94d6868 commit 5faeef8
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 28 deletions.
41 changes: 13 additions & 28 deletions cmd/protoc-gen-go-aip/internal/genaip/resourcename.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ import (
"google.golang.org/protobuf/reflect/protoregistry"
)

const (
fmtPackage = protogen.GoImportPath("fmt")
resourcenamePackage = protogen.GoImportPath("go.einride.tech/aip/resourcename")
stringsPackage = protogen.GoImportPath("strings")
)

type resourceNameCodeGenerator struct {
resource *annotations.ResourceDescriptor
file *protogen.File
Expand Down Expand Up @@ -225,14 +231,8 @@ func (r resourceNameCodeGenerator) generateValidateMethod(
pattern string,
typeName string,
) error {
stringsIndexByte := g.QualifiedGoIdent(protogen.GoIdent{
GoImportPath: "strings",
GoName: "IndexByte",
})
fmtErrorf := g.QualifiedGoIdent(protogen.GoIdent{
GoImportPath: "fmt",
GoName: "Errorf",
})
stringsIndexByte := stringsPackage.Ident("IndexByte")
fmtErrorf := fmtPackage.Ident("Errorf")
g.P()
g.P("func (n ", typeName, ") Validate() error {")
var sc resourcename.Scanner
Expand Down Expand Up @@ -284,10 +284,7 @@ func (r *resourceNameCodeGenerator) generateStringMethod(
pattern string,
typeName string,
) error {
resourcenameSprint := g.QualifiedGoIdent(protogen.GoIdent{
GoImportPath: "go.einride.tech/aip/resourcename",
GoName: "Sprint",
})
resourcenameSprint := resourcenamePackage.Ident("Sprint")
g.P()
g.P("func (n ", typeName, ") String() string {")
g.P("return ", resourcenameSprint, "(")
Expand Down Expand Up @@ -323,10 +320,7 @@ func (r resourceNameCodeGenerator) generateUnmarshalStringMethod(
pattern string,
typeName string,
) error {
resourcenameSscan := g.QualifiedGoIdent(protogen.GoIdent{
GoImportPath: "go.einride.tech/aip/resourcename",
GoName: "Sscan",
})
resourcenameSscan := resourcenamePackage.Ident("Sscan")
g.P()
g.P("func (n *", typeName, ") UnmarshalString(name string) error {")
g.P("err := ", resourcenameSscan, "(")
Expand All @@ -349,10 +343,7 @@ func (r resourceNameCodeGenerator) generateUnmarshalStringMethod(
}

func (r resourceNameCodeGenerator) generateMultiPatternInterface(g *protogen.GeneratedFile) error {
fmtStringer := g.QualifiedGoIdent(protogen.GoIdent{
GoImportPath: "fmt",
GoName: "Stringer",
})
fmtStringer := fmtPackage.Ident("Stringer")
g.P()
g.P("type ", r.MultiPatternInterfaceName(), " interface {")
g.P(fmtStringer)
Expand All @@ -363,14 +354,8 @@ func (r resourceNameCodeGenerator) generateMultiPatternInterface(g *protogen.Gen
}

func (r *resourceNameCodeGenerator) generateMultiPatternParseMethod(g *protogen.GeneratedFile) error {
resourcenameMatch := g.QualifiedGoIdent(protogen.GoIdent{
GoImportPath: "go.einride.tech/aip/resourcename",
GoName: "Match",
})
fmtErrorf := g.QualifiedGoIdent(protogen.GoIdent{
GoImportPath: "fmt",
GoName: "Errorf",
})
resourcenameMatch := resourcenamePackage.Ident("Match")
fmtErrorf := fmtPackage.Ident("Errorf")
g.P()
g.P("func Parse", r.MultiPatternInterfaceName(), "(name string) (", r.MultiPatternInterfaceName(), ", error) {")
g.P("switch {")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package toplevelsingleton

import (
"testing"

"gotest.tools/v3/assert"
)

func TestConfigResourceName(t *testing.T) {
t.Run("good", func(t *testing.T) {
const pattern = "config"
var name ConfigResourceName
err := name.UnmarshalString(pattern)
assert.NilError(t, err)

marshalled, err := name.MarshalString()
assert.NilError(t, err)
assert.Equal(t, marshalled, pattern)
})

t.Run("invalid", func(t *testing.T) {
var name ConfigResourceName
err := name.UnmarshalString("other")
t.Log(err)
assert.Error(t, err, "parse resource name 'other' with pattern 'config': segment config: got other")
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
syntax = "proto3";

package test.toplevelsingleton;

import "google/api/resource.proto";

message Config {
option (google.api.resource) = {
type: "test1.testdata/Config"
singular: "config"
plural: "configs"
pattern: "config"
};

// The resource name of the config.
string name = 1;
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5faeef8

Please sign in to comment.