tcncloud/protoc-gen-persist

enums are mapped incorrectly

Closed this issue · 0 comments

given a enum definition, and message that use it:

enum TestEnum {
  first = 0;
  second = 1;
}
message TwoMappedAndEnum {
  google.protobuf.Timestamp start_time = 1;
  google.protobuf.Timestamp end_time = 2;
  TestEnum gen_enum = 3;
}

and if we map our enum to this TestEnum to a new type "MyEnum" with a proto service like this example:

  option (persist.mapping) = {
    types: [
      {
        proto_type_name: ".google.protobuf.Timestamp"
        proto_type:  TYPE_MESSAGE
        go_type: "MyTime"
        go_package: "github.com/tcncloud/protoc-gen-persist/examples/mytime"
      },
      {
        proto_type_name: ".examples.test.TestEnum"
        proto_type: TYPE_ENUM
        go_type: "MyEnum"
        go_package: "github.com/tcncloud/protoc-gen-persist/examples/mytime"
      }
    ]
  };
  rpc TestMultiMappedFields(test.TwoMappedAndEnum) returns (test.TwoMappedAndEnum) {
    option (persist.ql) = {
      query: ["SELECT * FROM example_table WHERE start_time = ? AND end_time = ? AND gen_enum = ?"]
      arguments: ["start_time", "end_time", "gen_enum"]
    };
  };

We get code that is generated to look like this:

// persist creates a local variable GenEnum  that is of type MyEnum
// MyEnum has a ToProto method
	res := test.TwoMappedAndEnum{

		EndTime:   EndTime.ToProto(),
		GenEnum:   test.TestEnum(GenEnum).ToProto(),
		StartTime: StartTime.ToProto(),
	}

it is type casting before calling ToProto() which is wrong. It gives this error on build:

examples/spanner/basic/basic_example.persist.go:999: cannot convert GenEnum (type mytime.MyEnum) to type test.TestEnum
examples/spanner/basic/basic_example.persist.go:999: test.TestEnum(GenEnum).ToProto undefined (type test.TestEnum has no field or method ToProto)