Skip to content

Commit

Permalink
Merge pull request #22 from TheThingsIndustries/dependabot/github_act…
Browse files Browse the repository at this point in the history
…ions/actions/setup-go-4

Bump actions/setup-go from 3 to 4
  • Loading branch information
adriansmares authored Apr 3, 2023
2 parents fcd4345 + 169cdb0 commit 7b5e90a
Show file tree
Hide file tree
Showing 38 changed files with 132 additions and 10,334 deletions.
13 changes: 3 additions & 10 deletions .github/workflows/go-code.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,15 @@ on:
jobs:
test:
name: Go Test
runs-on: ubuntu-20.04
runs-on: ubuntu-22.04
timeout-minutes: 10
steps:
- name: Check out code
uses: actions/checkout@v3
- name: Set up Go
uses: actions/setup-go@v3
uses: actions/setup-go@v4
with:
go-version: "~1.17"
- name: Initialize Go module cache
uses: actions/cache@v3
with:
path: ~/go/pkg/mod
key: go-mod-${{ runner.os }}-${{ hashFiles('**/go.sum') }}
restore-keys: |
go-mod-${{ runner.os }}-
go-version: "~1.19"
- name: Clean Build
run: make clean build
- name: Go Test
Expand Down
26 changes: 3 additions & 23 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,10 @@ clean:
annotations/annotations.pb.go: .dev/protoc-gen-go-flags/annotations.proto .dev/golangproto/bin/protoc .dev/golangproto/bin/protoc-gen-go
PATH="$$PWD/.bin:$$PWD/.dev/golangproto/bin:$$PATH" protoc -I .dev --go_opt=module=github.com/TheThingsIndustries/protoc-gen-go-flags --go_out=./ $<

internal/gogoproto/gogo.pb.go: internal/gogoproto/gogo.proto .dev/golangproto/bin/protoc .dev/golangproto/bin/protoc-gen-go
PATH="$$PWD/.bin:$$PWD/.dev/golangproto/bin:$$PATH" protoc -I . --go_opt=paths=source_relative --go_out=./ ./internal/gogoproto/gogo.proto

internal/flagsplugin/annotations.pb.go: internal/flagsplugin/annotations.proto
protoc -I . --go_opt=paths=source_relative --go_out=./ ./internal/flagsplugin/annotations.proto

BINARY_DEPS = annotations/annotations.pb.go internal/gogoproto/gogo.pb.go $(wildcard cmd/protoc-gen-go-flags/*.go) $(wildcard internal/gen/*.go)
BINARY_DEPS = annotations/annotations.pb.go $(wildcard cmd/protoc-gen-go-flags/*.go) $(wildcard internal/gen/*.go)

VERSION ?= 0.0.0-dev

Expand All @@ -39,8 +36,6 @@ LDFLAGS = -X github.com/TheThingsIndustries/protoc-gen-go-flags/internal/gen.Ver
.bin/protoc-gen-go-flags-linux-arm64: $(BINARY_DEPS)
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -ldflags "$(LDFLAGS)" -o $@ ./cmd/protoc-gen-go-flags

REPLACES = Mgoogle/protobuf/descriptor.proto=github.com/gogo/protobuf/protoc-gen-gogo/descriptor,Mgoogle/protobuf/any.proto=github.com/gogo/protobuf/types,Mgoogle/protobuf/duration.proto=github.com/gogo/protobuf/types,Mgoogle/protobuf/empty.proto=github.com/gogo/protobuf/types,Mgoogle/protobuf/field_mask.proto=github.com/gogo/protobuf/types,Mgoogle/protobuf/struct.proto=github.com/gogo/protobuf/types,Mgoogle/protobuf/timestamp.proto=github.com/gogo/protobuf/types,Mgoogle/protobuf/wrappers.proto=github.com/gogo/protobuf/types

.PHONY: build

build: .bin/protoc-gen-go-flags .bin/protoc-gen-go-flags-linux-amd64 .bin/protoc-gen-go-flags-linux-arm64
Expand All @@ -63,33 +58,18 @@ endif
curl -sSL -o .dev/golangproto/protoc.zip https://github.com/protocolbuffers/protobuf/releases/download/v3.20.1/protoc-3.20.1-$(OS)-x86_64.zip
unzip -o .dev/golangproto/protoc.zip -d .dev/golangproto/

.dev/gogoproto/bin/protoc:
mkdir -p .dev/gogoproto/bin
curl -sSL -o .dev/gogoproto/protoc.zip https://github.com/protocolbuffers/protobuf/releases/download/v3.9.1/protoc-3.9.1-$(OS)-x86_64.zip
unzip -o .dev/gogoproto/protoc.zip -d .dev/gogoproto/
curl -sSL -o .dev/gogoproto/gogoproto.zip https://github.com/gogo/protobuf/archive/refs/heads/master.zip
unzip -o .dev/gogoproto/gogoproto.zip protobuf-master/protobuf/google/protobuf/*.proto -d .dev/gogoproto
mv .dev/gogoproto/protobuf-master/protobuf/google/protobuf/*.proto .dev/gogoproto/include/google/protobuf/

.dev/golangproto/bin/protoc-gen-go:
go build -o $@ google.golang.org/protobuf/cmd/protoc-gen-go

.dev/gogoproto/bin/protoc-gen-gogo:
go build -o $@ github.com/gogo/protobuf/protoc-gen-gogo

.PHONY: testprotos

testprotos: build .dev/golangproto/bin/protoc .dev/gogoproto/bin/protoc .dev/golangproto/bin/protoc-gen-go .dev/gogoproto/bin/protoc-gen-gogo
testprotos: build .dev/golangproto/bin/protoc .dev/golangproto/bin/protoc-gen-go
PATH="$$PWD/.bin:$$PWD/.dev/golangproto/bin:$$PATH" protoc -I ./test -I . \
--go_opt=paths=source_relative --go_out=./test/golang \
--go-flags_opt=paths=source_relative --go-flags_out=./test/golang \
./test/*.proto
PATH="$$PWD/.bin:$$PWD/.dev/gogoproto/bin:$$PATH" protoc -I ./test -I . \
--gogo_opt=paths=source_relative --gogo_opt=$(REPLACES) --gogo_out=./test/gogo \
--go-flags_opt=paths=source_relative --go-flags_opt=$(REPLACES) --go-flags_opt=lang=gogo --go-flags_out=./test/gogo \
./test/*.proto

.PHONY: test

test: testprotos
go test ./flagsplugin ./test/gogo ./test/golang
go test ./flagsplugin ./test/golang
2 changes: 1 addition & 1 deletion cmd/protoc-gen-go-flags/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func main() {
}

var flags flag.FlagSet
flags.StringVar(&plugin.Params.Lang, "lang", "go", "language (go or gogo)")
flags.StringVar(&plugin.Params.Lang, "lang", "go", "language (go)")
flags.StringVar(&plugin.Params.CustomTypeGetterPrefix, "customtype.getter-prefix", "Get", "prefix for customtype getter func")
flags.StringVar(&plugin.Params.CustomTypeGetterSuffix, "customtype.getter-suffix", "", "suffix for customtype getter func")

Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module github.com/TheThingsIndustries/protoc-gen-go-flags
go 1.17

require (
github.com/gogo/protobuf v1.3.2
github.com/google/go-cmp v0.5.9
github.com/spf13/pflag v1.0.5
google.golang.org/protobuf v1.30.0
Expand Down
30 changes: 0 additions & 30 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,40 +1,10 @@
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cng=
google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
29 changes: 0 additions & 29 deletions gogo/gogo.go

This file was deleted.

1 change: 0 additions & 1 deletion internal/gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ const (
pflagPackage = protogen.GoImportPath("github.com/spf13/pflag")

flagspluginPackage = protogen.GoImportPath("github.com/TheThingsIndustries/protoc-gen-go-flags/flagsplugin")
gogoPluginPackage = protogen.GoImportPath("github.com/TheThingsIndustries/protoc-gen-go-flags/gogo")
golangPluginPackage = protogen.GoImportPath("github.com/TheThingsIndustries/protoc-gen-go-flags/golang")
)

Expand Down
46 changes: 0 additions & 46 deletions internal/gen/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"strings"

"github.com/TheThingsIndustries/protoc-gen-go-flags/annotations"
"github.com/TheThingsIndustries/protoc-gen-go-flags/internal/gogoproto"
jsonannotations "github.com/TheThingsIndustries/protoc-gen-go-flags/internal/jsonplugin"
"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/proto"
Expand Down Expand Up @@ -45,64 +44,19 @@ func (g *generator) genMessage(message *protogen.Message) {
func fieldIsNullable(field *protogen.Field) bool {
// Typically, only message fields are nullable (use pointers).
nullable := field.Desc.Kind() == protoreflect.MessageKind

// If we use gogo, the nullability of fields (messages, but also bytes fields with custom types) is controlled with the (gogoproto.nullable) option.
if Params.Lang == "gogo" {
if proto.HasExtension(field.Desc.Options(), gogoproto.E_Customtype) {
nullable = true
}
if proto.HasExtension(field.Desc.Options(), gogoproto.E_Nullable) {
nullable = proto.GetExtension(field.Desc.Options(), gogoproto.E_Nullable).(bool)
}
}

return nullable
}

func fieldGoName(field *protogen.Field) interface{} {
var fieldGoName interface{} = field.GoName
if Params.Lang == "gogo" {
// If we use gogo, the GoName of a field can be overridden with the (gogoproto.customname) option.
if proto.HasExtension(field.Desc.Options(), gogoproto.E_Customname) {
fieldGoName = proto.GetExtension(field.Desc.Options(), gogoproto.E_Customname).(string)
}
// Fields with the (gogoproto.embed) option should use the name of the embedded message.
if proto.HasExtension(field.Desc.Options(), gogoproto.E_Embed) {
if proto.GetExtension(field.Desc.Options(), gogoproto.E_Embed).(bool) {
fieldGoName = field.Message.GoIdent
}
}
}
return fieldGoName
}

func fieldCustomType(field *protogen.Field) *protogen.GoIdent {
if Params.Lang == "gogo" {
// If we use gogo, the type of a field can be overridden with the (gogoproto.customtype) option.
if proto.HasExtension(field.Desc.Options(), gogoproto.E_Customtype) {
return parseGoIdent(proto.GetExtension(field.Desc.Options(), gogoproto.E_Customtype).(string))
}
}
return nil
}

func flagFromCustomType(field *protogen.Field) *protogen.GoIdent {
// We force customtype getter name only if the gogo-proto plugin is enabled.
if Params.Lang == "gogo" {
if proto.HasExtension(field.Desc.Options(), gogoproto.E_Customtype) {
// Retrieve customtype path.
customtype := proto.GetExtension(field.Desc.Options(), gogoproto.E_Customtype).(string)
i := strings.LastIndex(customtype, ".")
// Force custom getter to be of type `/path/to/customtype.Get{CustomType}`.
customGetter := customtype[:i+1] + Params.CustomTypeGetterPrefix + customtype[i+1:]
// Append slice if the field is repeated.
if field.Desc.IsList() {
customGetter += "Slice"
}
customGetter += Params.CustomTypeGetterSuffix
return parseGoIdent(customGetter)
}
}
return nil
}

Expand Down
10 changes: 0 additions & 10 deletions internal/gen/messages_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"

"github.com/TheThingsIndustries/protoc-gen-go-flags/annotations"
"github.com/TheThingsIndustries/protoc-gen-go-flags/internal/gogoproto"
"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
Expand Down Expand Up @@ -657,21 +656,12 @@ nextField:
// readWKTValue assigns a different proto field type to a WKT value based on the plugin used.
func (g *generator) readWKTValue(field *protogen.Field, message *protogen.Message, fieldName string) string {
pluginPackage := golangPluginPackage
if Params.Lang == "gogo" {
pluginPackage = gogoPluginPackage
}
switch message.Desc.FullName() {
case "google.protobuf.FieldMask":
return g.QualifiedGoIdent(pluginPackage.Ident("SetFieldMask(" + fieldName + ")"))
case "google.protobuf.Timestamp":
if Params.Lang == "gogo" && proto.HasExtension(field.Desc.Options(), gogoproto.E_Stdtime) && proto.GetExtension(field.Desc.Options(), gogoproto.E_Stdtime).(bool) {
return ifThenElse(fieldIsNullable(field), "&", "") + fieldName
}
return g.QualifiedGoIdent(pluginPackage.Ident("SetTimestamp(" + fieldName + ")"))
case "google.protobuf.Duration":
if Params.Lang == "gogo" && proto.HasExtension(field.Desc.Options(), gogoproto.E_Stdduration) && proto.GetExtension(field.Desc.Options(), gogoproto.E_Stdduration).(bool) {
return ifThenElse(fieldIsNullable(field), "&", "") + fieldName
}
return g.QualifiedGoIdent(pluginPackage.Ident("SetDuration(" + fieldName + ")"))
default:
g.gen.Error(fmt.Errorf("unsupported WKT %q", message.Desc.FullName()))
Expand Down
Loading

0 comments on commit 7b5e90a

Please sign in to comment.