aip-go icon indicating copy to clipboard operation
aip-go copied to clipboard

Invalid code generation for top-level singletons

Open vallahaye opened this issue 5 months ago • 0 comments

It seems that the code generated for top-level singletons with [email protected] is invalid. Specifically, the fmt and strings packages are imported but not used, resulting in compilation errors.

For example, the following 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;
}

generates the following code:

// Code generated by protoc-gen-go-aip. DO NOT EDIT.
//
// versions:
// 	protoc-gen-go-aip development
// 	protoc (unknown)
// source: test/toplevelsingleton/testdata.proto

package toplevelsingleton

import (
	fmt "fmt"
	resourcename "go.einride.tech/aip/resourcename"
	strings "strings
)

type ConfigResourceName struct {
}

func (n ConfigResourceName) Validate() error {
	return nil
}

func (n ConfigResourceName) ContainsWildcard() bool {
	return false
}

func (n ConfigResourceName) String() string {
	return resourcename.Sprint(
		"config",
	)
}

func (n ConfigResourceName) MarshalString() (string, error) {
	if err := n.Validate(); err != nil {
		return "", err
	}
	return n.String(), nil
}

func (n *ConfigResourceName) UnmarshalString(name string) error {
	err := resourcename.Sscan(
		name,
		"config",
	)
	if err != nil {
		return err
	}
	return n.Validate()
}

which, when compiled, produces the following errors:

test/toplevelsingleton/testdata_aip.go:11:2: "fmt" imported and not used
test/toplevelsingleton/testdata_aip.go:13:2: "strings" imported and not used

vallahaye avatar Sep 08 '24 20:09 vallahaye