Skip to content

Commit

Permalink
Added "big.Int" support for GORM data type in protoc generation (#222)
Browse files Browse the repository at this point in the history
  • Loading branch information
aazaiats authored Jan 20, 2022
1 parent 51b6c18 commit 2985d51
Show file tree
Hide file tree
Showing 15 changed files with 399 additions and 192 deletions.
2 changes: 1 addition & 1 deletion Makefile.buf
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ deps: $(BUF)
.PHONY: local
local: $(BUF)
buf lint
buf breaking --against '.git#branch=master'
buf breaking --against '.git#branch=main'

# https is what we run when testing in most CI providers.
# This does breaking change detection against our remote HTTPS git repository.
Expand Down
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,22 @@ The protobuf compiler (protoc) is required.

Get the golang protobuf code generator:

Before Go 1.17
```
go get -u github.com/golang/protobuf/protoc-gen-go
```
Starting from Go 1.17
```
go install github.com/golang/protobuf/protoc-gen-go
```


#### 3. Vendored Dependencies

Retrieve and install the vendored dependencies for this project with [dep](https://github.com/golang/dep):
Retrieve and install the vendored dependencies for this project with go mod:

```
dep ensure
go mod tidy
```

### Installation
Expand Down Expand Up @@ -115,6 +121,7 @@ to test the effects of changing the options and fields.
Within the proto files, the following types are supported:
- standard primitive types `uint32`, `uint64`, `int32`, `int64`, `float`,
`double`, `bool`, `string` map to the same type at ORM level
-
- [google wrapper types](https://github.com/golang/protobuf/blob/master/ptypes/wrappers/wrappers.proto)
`google.protobuf.StringValue`, `.BoolValue`, `.UInt32Value`, `.FloatValue`, etc.
map to pointers of the internal type at the ORM level, e.g.
Expand Down
4 changes: 4 additions & 0 deletions example/feature_demo/demo_multi_file_service_grpc.pb.go

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

4 changes: 4 additions & 0 deletions example/feature_demo/demo_service_grpc.pb.go

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

334 changes: 173 additions & 161 deletions example/feature_demo/demo_types.pb.go

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions example/feature_demo/demo_types.pb.gorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
emptypb "google.golang.org/protobuf/types/known/emptypb"
timestamppb "google.golang.org/protobuf/types/known/timestamppb"
wrapperspb "google.golang.org/protobuf/types/known/wrapperspb"
big "math/big"
strings "strings"
time "time"
)
Expand All @@ -26,6 +27,7 @@ type TestTypesORM struct {
Array pq.StringArray
Array2 pq.StringArray
BecomesInt string
Bigint *big.Int `gorm:"type:numeric"`
CreatedAt *time.Time
JsonField *postgres.Jsonb `gorm:"type:jsonb"`
NullableUuid *go_uuid.UUID `gorm:"type:uuid"`
Expand Down Expand Up @@ -85,6 +87,14 @@ func (m *TestTypes) ToORM(ctx context.Context) (TestTypesORM, error) {
return to, err
}
}
if m.Bigint != nil {
var ok bool
to.Bigint = new(big.Int)
to.Bigint, ok = to.Bigint.SetString(m.Bigint.Value, 0)
if !ok {
return to, fmt.Errorf("unable convert Bigint to big.Int")
}
}
if posthook, ok := interface{}(m).(TestTypesWithAfterToORM); ok {
err = posthook.AfterToORM(ctx, &to)
}
Expand Down Expand Up @@ -122,6 +132,7 @@ func (m *TestTypesORM) ToPB(ctx context.Context) (TestTypes, error) {
return to, err
}
}
to.Bigint = &types.BigInt{Value: m.Bigint.String()}
if posthook, ok := interface{}(m).(TestTypesWithAfterToPB); ok {
err = posthook.AfterToPB(ctx, &to)
}
Expand Down Expand Up @@ -1394,6 +1405,10 @@ func DefaultApplyFieldMaskTestTypes(ctx context.Context, patchee *TestTypes, pat
patchee.TimeOnly = patcher.TimeOnly
continue
}
if f == prefix+"Bigint" {
patchee.Bigint = patcher.Bigint
continue
}
}
if err != nil {
return nil, err
Expand Down
1 change: 1 addition & 0 deletions example/feature_demo/demo_types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ message TestTypes {
// The TimeOnly should act as uint32 value at business layer, but is automatically
// converted to/from string at API and ORM level
gorm.types.TimeOnly time_only = 11;
gorm.types.BigInt bigint = 12;
}

// TypeWithID demonstrates some basic assocation behavior
Expand Down
23 changes: 12 additions & 11 deletions example/feature_demo/marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,17 @@ func TestMarshalTypes(t *testing.T) {
// Will marshal with snake_case names and default values included
marshaler := &jsonpb.Marshaler{OrigName: true, EmitDefaults: true}
for expected, in := range map[string]TestTypes{
`{"api_only_string":"","numbers":[],"optional_string":null,"becomes_int":"UNKNOWN","nothingness":null,"uuid":null,"created_at":null,"type_with_id_id":0,"json_field":null,"nullable_uuid":null,"time_only":null}`: {},
`{"api_only_string":"Something","numbers":[],"optional_string":null,"becomes_int":"UNKNOWN","nothingness":null,"uuid":null,"created_at":null,"type_with_id_id":0,"json_field":null,"nullable_uuid":null,"time_only":null}`: {ApiOnlyString: "Something"},
`{"api_only_string":"","numbers":[0,1,2,3],"optional_string":null,"becomes_int":"UNKNOWN","nothingness":null,"uuid":null,"created_at":null,"type_with_id_id":0,"json_field":null,"nullable_uuid":null,"time_only":null}`: {Numbers: []int32{0, 1, 2, 3}},
`{"api_only_string":"","numbers":[],"optional_string":"Not nothing","becomes_int":"UNKNOWN","nothingness":null,"uuid":null,"created_at":null,"type_with_id_id":0,"json_field":null,"nullable_uuid":null,"time_only":null}`: {OptionalString: &wrappers.StringValue{Value: "Not nothing"}},
`{"api_only_string":"","numbers":[],"optional_string":null,"becomes_int":"GOOD","nothingness":null,"uuid":null,"created_at":null,"type_with_id_id":0,"json_field":null,"nullable_uuid":null,"time_only":null}`: {BecomesInt: TestTypes_GOOD},
`{"api_only_string":"","numbers":[],"optional_string":null,"becomes_int":"UNKNOWN","nothingness":null,"uuid":"6ba7b810-9dad-11d1-80b4-00c04fd430c8","created_at":null,"type_with_id_id":0,"json_field":null,"nullable_uuid":null,"time_only":null}`: {Uuid: &types.UUID{Value: "6ba7b810-9dad-11d1-80b4-00c04fd430c8"}},
`{"api_only_string":"","numbers":[],"optional_string":null,"becomes_int":"UNKNOWN","nothingness":null,"uuid":null,"created_at":"2009-11-17T20:34:58.651387237Z","type_with_id_id":0,"json_field":null,"nullable_uuid":null,"time_only":null}`: {CreatedAt: MustTimestampProto(time.Date(2009, 11, 17, 20, 34, 58, 651387237, time.UTC))},
`{"api_only_string":"","numbers":[],"optional_string":null,"becomes_int":"UNKNOWN","nothingness":null,"uuid":null,"created_at":null,"type_with_id_id":2,"json_field":null,"nullable_uuid":null,"time_only":null}`: {TypeWithIdId: 2},
`{"api_only_string":"","numbers":[],"optional_string":null,"becomes_int":"UNKNOWN","nothingness":null,"uuid":null,"created_at":null,"type_with_id_id":0,"json_field":{"text":[]},"nullable_uuid":null,"time_only":null}`: {JsonField: &types.JSONValue{Value: `{"text":[]}`}},
`{"api_only_string":"","numbers":[],"optional_string":null,"becomes_int":"UNKNOWN","nothingness":null,"uuid":null,"created_at":null,"type_with_id_id":0,"json_field":null,"nullable_uuid":null,"time_only":"01:59:18"}`: {TimeOnly: &types.TimeOnly{Value: 7158}},
`{"api_only_string":"","numbers":[],"optional_string":null,"becomes_int":"UNKNOWN","nothingness":null,"uuid":null,"created_at":null,"type_with_id_id":0,"json_field":null,"nullable_uuid":null,"time_only":null,"bigint":null}`: {},
`{"api_only_string":"Something","numbers":[],"optional_string":null,"becomes_int":"UNKNOWN","nothingness":null,"uuid":null,"created_at":null,"type_with_id_id":0,"json_field":null,"nullable_uuid":null,"time_only":null,"bigint":null}`: {ApiOnlyString: "Something"},
`{"api_only_string":"","numbers":[0,1,2,3],"optional_string":null,"becomes_int":"UNKNOWN","nothingness":null,"uuid":null,"created_at":null,"type_with_id_id":0,"json_field":null,"nullable_uuid":null,"time_only":null,"bigint":null}`: {Numbers: []int32{0, 1, 2, 3}},
`{"api_only_string":"","numbers":[],"optional_string":"Not nothing","becomes_int":"UNKNOWN","nothingness":null,"uuid":null,"created_at":null,"type_with_id_id":0,"json_field":null,"nullable_uuid":null,"time_only":null,"bigint":null}`: {OptionalString: &wrappers.StringValue{Value: "Not nothing"}},
`{"api_only_string":"","numbers":[],"optional_string":null,"becomes_int":"GOOD","nothingness":null,"uuid":null,"created_at":null,"type_with_id_id":0,"json_field":null,"nullable_uuid":null,"time_only":null,"bigint":null}`: {BecomesInt: TestTypes_GOOD},
`{"api_only_string":"","numbers":[],"optional_string":null,"becomes_int":"UNKNOWN","nothingness":null,"uuid":"6ba7b810-9dad-11d1-80b4-00c04fd430c8","created_at":null,"type_with_id_id":0,"json_field":null,"nullable_uuid":null,"time_only":null,"bigint":null}`: {Uuid: &types.UUID{Value: "6ba7b810-9dad-11d1-80b4-00c04fd430c8"}},
`{"api_only_string":"","numbers":[],"optional_string":null,"becomes_int":"UNKNOWN","nothingness":null,"uuid":null,"created_at":"2009-11-17T20:34:58.651387237Z","type_with_id_id":0,"json_field":null,"nullable_uuid":null,"time_only":null,"bigint":null}`: {CreatedAt: MustTimestampProto(time.Date(2009, 11, 17, 20, 34, 58, 651387237, time.UTC))},
`{"api_only_string":"","numbers":[],"optional_string":null,"becomes_int":"UNKNOWN","nothingness":null,"uuid":null,"created_at":null,"type_with_id_id":2,"json_field":null,"nullable_uuid":null,"time_only":null,"bigint":null}`: {TypeWithIdId: 2},
`{"api_only_string":"","numbers":[],"optional_string":null,"becomes_int":"UNKNOWN","nothingness":null,"uuid":null,"created_at":null,"type_with_id_id":0,"json_field":{"text":[]},"nullable_uuid":null,"time_only":null,"bigint":null}`: {JsonField: &types.JSONValue{Value: `{"text":[]}`}},
`{"api_only_string":"","numbers":[],"optional_string":null,"becomes_int":"UNKNOWN","nothingness":null,"uuid":null,"created_at":null,"type_with_id_id":0,"json_field":null,"nullable_uuid":null,"time_only":"01:59:18","bigint":null}`: {TimeOnly: &types.TimeOnly{Value: 7158}},
`{"api_only_string":"","numbers":[],"optional_string":null,"becomes_int":"UNKNOWN","nothingness":null,"uuid":null,"created_at":null,"type_with_id_id":0,"json_field":null,"nullable_uuid":null,"time_only":null,"bigint":"7158"}`: {Bigint: &types.BigInt{Value: "7158"}},
} {
out, err := marshaler.MarshalToString(&in)
if err != nil {
Expand Down Expand Up @@ -141,4 +142,4 @@ func MustTimestampProto(t time.Time) *timestamp.Timestamp {
panic(err)
}
return ts
}
}
9 changes: 6 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ require (
github.com/mattn/go-sqlite3 v1.14.6 // indirect
github.com/satori/go.uuid v1.2.0
go.opencensus.io v0.22.6
google.golang.org/genproto v0.0.0-20210426193834-eac7f76ac494
google.golang.org/grpc v1.37.0
golang.org/x/net v0.0.0-20220114011407-0dd24b26b47d // indirect
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/genproto v0.0.0-20220118154757-00ab72f36ad5
google.golang.org/grpc v1.43.0
google.golang.org/grpc/examples v0.0.0-20210601155443-8bdcb4c9ab8d // indirect
google.golang.org/protobuf v1.26.0
google.golang.org/protobuf v1.27.1
)

go 1.16
Loading

0 comments on commit 2985d51

Please sign in to comment.