Skip to content
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

Go types override #10

Merged
merged 5 commits into from
Nov 1, 2022
Merged

Go types override #10

merged 5 commits into from
Nov 1, 2022

Conversation

borosr
Copy link

@borosr borosr commented Oct 28, 2022

I created three new field options called:

  • go_type - the go type to override the existing one
  • go_import - the import to add into the import statement
  • go_import_alias - the alias of the import

Try

Run the following test file cmd/protoc-gen-go/internal_gengo/main_test.go.
This will generate 3 go files, the test.pb.go will be the interesting one.

Example proto file

Note: The field option extend part was extracted into a common proto file, so it can be imported. The location of this file is in cmd/protoc-gen-go/internal_gengo/test_data/config.proto.

syntax = "proto3";

package proto;

option go_package = ".;proto";

// This part is needed to extend messages
import "google/protobuf/descriptor.proto";

extend google.protobuf.FieldOptions {
  optional string go_type = 1000;
  optional string go_import = 1001;
  optional string go_import_alias = 1002;
}

message RepeatedString {
}

message Test {
  int64 created_at = 1 [(go_type) = "time.Time", (go_import) = "time"];
  map<uint64, RepeatedString> map_field = 2 [(go_type) = "map[uint64][]string", (go_import) = ""];
  string test = 3;
  string optStr = 43 [(go_type) = "null.String", (go_import) = "github.com/volatiletech/null/v9", (go_import_alias) = "null"];
}

extend the cmd with type and import overrides
@borosr borosr self-assigned this Oct 28, 2022
Copy link
Member

@PumpkinSeed PumpkinSeed left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to use it with libprotoc 3.21.7 as the parser, and it didn't do anything which is related to type override. Can you check that it's the correct version for libprotoc or something other issue.

Note: The parser what the test is using is not the original protoc, I found something which was an open-source copy written in Go.

protoc --version
libprotoc 3.21.7

@borosr borosr requested a review from PumpkinSeed October 31, 2022 19:04
Copy link
Member

@PumpkinSeed PumpkinSeed left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After a successful type override the getter's default return is NOT respecting the override value. Example:

func (x *TransactionRequest) GetIdempotencyTime() null.Uint64 {
    if x != nil {
        return x.IdempotencyTime
    }
    return ""
}

@PumpkinSeed PumpkinSeed linked an issue Nov 1, 2022 that may be closed by this pull request
@PumpkinSeed PumpkinSeed merged commit 502726f into master Nov 1, 2022
@PumpkinSeed PumpkinSeed deleted the 9-go-types-override branch November 1, 2022 21:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Annotation @gotypes for type override
2 participants