Skip to content

Commit 61a28ae

Browse files
committed
feat: add protobuf-gen-goshim
relate otlp*http modules import grpc open-telemetry/opentelemetry-go#2579
1 parent 62d3c33 commit 61a28ae

File tree

9 files changed

+101
-2
lines changed

9 files changed

+101
-2
lines changed

.github/workflows/protobuf-dockerimage.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
steps:
2020
- uses: actions/checkout@v4
2121
- name: Build the Docker image
22-
run: docker build protobuf/. -t build-protobuf
22+
run: docker build protobuf/. -t build-protobuf --build-context=src-gen-goshim=protoc-gen-goshim
2323
env:
2424
TARGETARCH: ${{ matrix.TARGETARCH }}
2525
- name: Push the Docker image
@@ -38,4 +38,4 @@ jobs:
3838
tag_and_push "${GITHUB_REF#"refs/tags/"}"
3939
fi
4040
env:
41-
TARGETARCH: ${{ matrix.TARGETARCH }}
41+
TARGETARCH: ${{ matrix.TARGETARCH }}

protobuf/.dockerignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.dockerignore
2+
*.md

protobuf/Dockerfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ RUN mkdir -p ${GOPATH}/src/github.com/grpc-ecosystem/grpc-gateway && \
120120
mkdir -p /out/usr/include/protoc-gen-openapiv2/options && \
121121
install -D $(find ./protoc-gen-openapiv2/options -name '*.proto') -t /out/usr/include/protoc-gen-openapiv2/options
122122

123+
COPY --from=src-gen-goshim . /src/gen-goshim
124+
RUN cd /src/gen-goshim && \
125+
go build -ldflags '-w -s' -o /out/usr/bin/protoc-gen-goshim
126+
123127
FROM alpine:3.18 as packer
124128
RUN apk add --no-cache curl
125129

protobuf/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Forked from <https://github.com/jaegertracing/docker-protobuf>.
1515
- <https://github.com/grpc/grpc>
1616
- <https://github.com/grpc/grpc-java>
1717
- <https://github.com/atoulme/protoc-gen-parquet>
18+
- [protobuf-gen-goshim](../protoc-gen-goshim/)
1819

1920
## Supported languages
2021

protoc-gen-goshim/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
protoc-gen-goshim*

protoc-gen-goshim/go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module github.com/open-telemetry/build-tools/protoc-gen-goshim
2+
3+
go 1.23
4+
5+
require google.golang.org/protobuf v1.36.1

protoc-gen-goshim/go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
2+
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
3+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
4+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
5+
google.golang.org/protobuf v1.36.1 h1:yBPeRvTftaleIgM3PZ/WBIZ7XM/eEYAaEyCwvyjq/gk=
6+
google.golang.org/protobuf v1.36.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=

protoc-gen-goshim/main.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
"strings"
7+
8+
"google.golang.org/protobuf/compiler/protogen"
9+
"google.golang.org/protobuf/types/pluginpb"
10+
)
11+
12+
func main() {
13+
var flags flag.FlagSet
14+
old := flags.String("old", "", "old path")
15+
new := flags.String("new", "", "new path")
16+
17+
protogen.Options{
18+
ParamFunc: flags.Set,
19+
}.Run(func(gen *protogen.Plugin) error {
20+
for _, f := range gen.Files {
21+
if f.Generate {
22+
if *old == "" || *new == "" {
23+
return fmt.Errorf("must specify old and new")
24+
}
25+
genShim(gen, f, *old, *new)
26+
}
27+
}
28+
gen.SupportedFeatures = uint64(pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL | pluginpb.CodeGeneratorResponse_FEATURE_SUPPORTS_EDITIONS)
29+
return nil
30+
})
31+
}
32+
33+
func genShim(gen *protogen.Plugin, file *protogen.File, old string, new string) {
34+
importPath := strings.ReplaceAll(string(file.GoImportPath), old, new)
35+
36+
filename := file.GeneratedFilenamePrefix + ".shim.pb.go"
37+
g := gen.NewGeneratedFile(filename, file.GoImportPath)
38+
g.P("// Code generated by protoc-gen-goshim. DO NOT EDIT.")
39+
g.P()
40+
g.P("package ", file.GoPackageName)
41+
g.P(`import slim "`, importPath, `"`)
42+
43+
g.P("type (")
44+
for _, m := range file.Messages {
45+
g.P(fmt.Sprintf("%[1]s = slim.%[1]s", m.GoIdent.GoName))
46+
}
47+
for _, m := range file.Enums {
48+
g.P(fmt.Sprintf("%[1]s = slim.%[1]s", m.GoIdent.GoName))
49+
}
50+
g.P(")")
51+
}

protoc-gen-goshim/readme.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# protobuf-gen-goshim
2+
3+
This tool replaces Go type definitions with imports.
4+
5+
## usage
6+
7+
Run the protoc compiler with the -goshim_out option, specifying the old and new package paths, and the output directory.
8+
9+
```shell
10+
protoc -goshim_out=old=go.opentelemetry.io/proto/otlp,new=go.opentelemetry.io/proto/slim/otlp:./dir file.proto
11+
```
12+
13+
### output
14+
15+
The generated Go code imports the new package and aliases the types, instead of redefining them.
16+
17+
```go
18+
// Code generated by protoc-gen-goshim. DO NOT EDIT.
19+
20+
package v1
21+
22+
import slim "go.opentelemetry.io/proto/slim/otlp/logs/v1"
23+
24+
type (
25+
LogsData = slim.LogsData
26+
)
27+
```
28+
29+
This feature is related to an issue in the OpenTelemetry Go project: <https://github.com/open-telemetry/opentelemetry-go/issues/2579>

0 commit comments

Comments
 (0)