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

feat: add protobuf-gen-goshim #383

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/protobuf-dockerimage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Build the Docker image
run: docker build protobuf/. -t build-protobuf
run: docker build protobuf/. -t build-protobuf --build-context=src-gen-goshim=protoc-gen-goshim
env:
TARGETARCH: ${{ matrix.TARGETARCH }}
- name: Push the Docker image
Expand All @@ -38,4 +38,4 @@ jobs:
tag_and_push "${GITHUB_REF#"refs/tags/"}"
fi
env:
TARGETARCH: ${{ matrix.TARGETARCH }}
TARGETARCH: ${{ matrix.TARGETARCH }}
2 changes: 2 additions & 0 deletions protobuf/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.dockerignore
*.md
4 changes: 4 additions & 0 deletions protobuf/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ RUN mkdir -p ${GOPATH}/src/github.com/grpc-ecosystem/grpc-gateway && \
mkdir -p /out/usr/include/protoc-gen-openapiv2/options && \
install -D $(find ./protoc-gen-openapiv2/options -name '*.proto') -t /out/usr/include/protoc-gen-openapiv2/options

COPY --from=src-gen-goshim . /src/gen-goshim
RUN cd /src/gen-goshim && \
go build -ldflags '-w -s' -o /out/usr/bin/protoc-gen-goshim

FROM alpine:3.18 as packer
RUN apk add --no-cache curl

Expand Down
1 change: 1 addition & 0 deletions protobuf/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Forked from <https://github.com/jaegertracing/docker-protobuf>.
- <https://github.com/grpc/grpc>
- <https://github.com/grpc/grpc-java>
- <https://github.com/atoulme/protoc-gen-parquet>
- [protobuf-gen-goshim](../protoc-gen-goshim/)

## Supported languages

Expand Down
3 changes: 3 additions & 0 deletions protoc-gen-goshim/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.dockerignore
*.md
.gitignore
1 change: 1 addition & 0 deletions protoc-gen-goshim/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
protoc-gen-goshim*
5 changes: 5 additions & 0 deletions protoc-gen-goshim/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/open-telemetry/build-tools/protoc-gen-goshim

go 1.23

require google.golang.org/protobuf v1.36.1
6 changes: 6 additions & 0 deletions protoc-gen-goshim/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.36.1 h1:yBPeRvTftaleIgM3PZ/WBIZ7XM/eEYAaEyCwvyjq/gk=
google.golang.org/protobuf v1.36.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
78 changes: 78 additions & 0 deletions protoc-gen-goshim/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package main

import (
"flag"
"fmt"
"strings"

"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/types/pluginpb"
)

func main() {
var flags flag.FlagSet
old := flags.String("old", "", "old path")
new := flags.String("new", "", "new path")

protogen.Options{
ParamFunc: flags.Set,
}.Run(func(gen *protogen.Plugin) error {
for _, f := range gen.Files {
if f.Generate {
if *old == "" || *new == "" {
return fmt.Errorf("must specify old and new")
}
genShim(gen, f, *old, *new)
}
}
gen.SupportedFeatures = uint64(pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL | pluginpb.CodeGeneratorResponse_FEATURE_SUPPORTS_EDITIONS)
return nil
})
}

func genShim(gen *protogen.Plugin, file *protogen.File, old string, new string) {
importPath := strings.ReplaceAll(string(file.GoImportPath), old, new)

filename := file.GeneratedFilenamePrefix + ".shim.pb.go"
g := gen.NewGeneratedFile(filename, file.GoImportPath)
g.P("// Code generated by protoc-gen-goshim. DO NOT EDIT.")
g.P()
g.P("package ", file.GoPackageName)
g.P(`import slim "`, importPath, `"`)

for _, m := range file.Messages {
doMessage(g, m)
}

for _, m := range file.Enums {
doEnum(g, m)
}
}

func doMessage(g *protogen.GeneratedFile, m *protogen.Message) {
g.P(fmt.Sprintf("type %[1]s = slim.%[1]s", m.GoIdent.GoName))
for _, e := range m.Enums {
doEnum(g, e)
}

for _, o := range m.Oneofs {
// protobuf 3 optional like oneof
if !o.Desc.IsSynthetic() {
for _, f := range o.Fields {
g.P(fmt.Sprintf("type %[1]s = slim.%[1]s", f.GoIdent.GoName))
}
}
}
for _, m := range m.Messages {
doMessage(g, m)
}
}

func doEnum(g *protogen.GeneratedFile, m *protogen.Enum) {
g.P(fmt.Sprintf("type %[1]s = slim.%[1]s", m.GoIdent.GoName))
g.P("const (")
for _, v := range m.Values {
g.P(fmt.Sprintf("%[1]s = slim.%[1]s", v.GoIdent.GoName))
}
g.P(")")
}
29 changes: 29 additions & 0 deletions protoc-gen-goshim/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# protobuf-gen-goshim

This tool replaces Go type definitions with imports.

## usage

Run the protoc compiler with the -goshim_out option, specifying the old and new package paths, and the output directory.

```shell
protoc -goshim_out=old=go.opentelemetry.io/proto/otlp,new=go.opentelemetry.io/proto/slim/otlp:./dir file.proto
```

### output

The generated Go code imports the new package and aliases the types, instead of redefining them.

```go
// Code generated by protoc-gen-goshim. DO NOT EDIT.

package v1

import slim "go.opentelemetry.io/proto/slim/otlp/logs/v1"

type (
LogsData = slim.LogsData
)
```

This feature is related to an issue in the OpenTelemetry Go project: <https://github.com/open-telemetry/opentelemetry-go/issues/2579>