Skip to content
Merged
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
13 changes: 12 additions & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
uses: golangci/golangci-lint-action@v9.3.0
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
version: v2.2.1
version: latest

- name: Install Buf
uses: bufbuild/buf-action@v1
Expand All @@ -40,3 +40,14 @@ jobs:
git diff
exit 1
fi

- name: Go Generate and Check
run: |
go generate ./...
if ! git diff --quiet; then
echo "Error: 'go generate ./...' produced uncommitted changes."
echo "Run 'go generate ./...' locally and commit the result."
git status --short
git diff
exit 1
fi
3 changes: 2 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ jobs:
- name: Install Go
uses: actions/setup-go@v6
with:
go-version: "1.24.x"
go-version-file: go.mod
cache-dependency-path: go.sum

- name: Download dependencies
run: go mod download
Expand Down
10 changes: 5 additions & 5 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@ jobs:
test:
strategy:
matrix:
go-version: [1.24.x]
os: [ubuntu-latest, windows-latest]
targetplatform: [x86, x64]

runs-on: ${{ matrix.os }}

steps:
- name: Checkout Code
uses: actions/checkout@v7

- name: Install Go
uses: actions/setup-go@v6
with:
go-version: ${{ matrix.go-version }}

- name: Checkout Code
uses: actions/checkout@v7
go-version-file: go.mod
cache-dependency-path: go.sum

- name: Vet
run: go vet ./...
Expand Down
2 changes: 1 addition & 1 deletion internal/protogen/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ func (x *sheetExporter) findMDFromGeneratedProtos(name string) protoreflect.Mess
return nil
}
fullName := protoreflect.FullName(x.be.ProtoPackage).Append(protoreflect.Name(name))
descriptor, err := x.be.gen.ProtoRegistryFilesWithGenerated.FindDescriptorByName(fullName)
descriptor, err := x.be.gen.getProtoRegistryFilesWithGenerated().FindDescriptorByName(fullName)
if err != nil {
return nil
}
Expand Down
10 changes: 5 additions & 5 deletions internal/protogen/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,7 @@ func Test_sheetExporter_exportStruct(t *testing.T) {
OutputOpt: &options.ProtoOutputOption{
PreserveFieldNumbers: true,
},
ProtoRegistryFilesWithGenerated: protoregistry.GlobalFiles,
protoRegistryFilesWithGenerated: protoregistry.GlobalFiles,
},
},
typeInfos: &xproto.TypeInfos{},
Expand Down Expand Up @@ -665,7 +665,7 @@ func Test_sheetExporter_exportStruct(t *testing.T) {
OutputOpt: &options.ProtoOutputOption{
PreserveFieldNumbers: true,
},
ProtoRegistryFilesWithGenerated: protoregistry.GlobalFiles,
protoRegistryFilesWithGenerated: protoregistry.GlobalFiles,
},
},
typeInfos: &xproto.TypeInfos{},
Expand Down Expand Up @@ -793,7 +793,7 @@ func Test_sheetExporter_exportUnion(t *testing.T) {
OutputOpt: &options.ProtoOutputOption{
PreserveFieldNumbers: true,
},
ProtoRegistryFilesWithGenerated: protoregistry.GlobalFiles,
protoRegistryFilesWithGenerated: protoregistry.GlobalFiles,
},
},
typeInfos: &xproto.TypeInfos{},
Expand Down Expand Up @@ -1015,7 +1015,7 @@ func Test_sheetExporter_exportMessager(t *testing.T) {
OutputOpt: &options.ProtoOutputOption{
PreserveFieldNumbers: true,
},
ProtoRegistryFilesWithGenerated: protoregistry.GlobalFiles,
protoRegistryFilesWithGenerated: protoregistry.GlobalFiles,
},
},
typeInfos: &xproto.TypeInfos{},
Expand Down Expand Up @@ -1098,7 +1098,7 @@ func Test_sheetExporter_exportMessager(t *testing.T) {
OutputOpt: &options.ProtoOutputOption{
PreserveFieldNumbers: true,
},
ProtoRegistryFilesWithGenerated: protoregistry.GlobalFiles,
protoRegistryFilesWithGenerated: protoregistry.GlobalFiles,
},
},
typeInfos: &xproto.TypeInfos{},
Expand Down
38 changes: 29 additions & 9 deletions internal/protogen/protogen.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,17 @@ type Generator struct {
InputOpt *options.ProtoInputOption
OutputOpt *options.ProtoOutputOption

ProtoRegistryFiles, ProtoRegistryFilesWithGenerated *protoregistry.Files
ProtoRegistryTypes *dynamicpb.Types
ProtoRegistryFiles *protoregistry.Files
ProtoRegistryTypes *dynamicpb.Types

// internal
typeInfos *xproto.TypeInfos // predefined type infos
collector *xerrors.Collector // concurrent error collector shared across the generator.

// used in advanced mode or when preserveFieldNumbers is set to true
registryWithGeneratedOnce sync.Once
protoRegistryFilesWithGenerated *protoregistry.Files

cacheMu sync.RWMutex // guard fields below
cachedImporters map[string]importer.Importer // absolute file path -> importer
}
Expand Down Expand Up @@ -86,15 +90,29 @@ func NewGeneratorWithOptions(protoPackage, indir, outdir string, opts *options.O
panic(err)
}
gen.ProtoRegistryFiles = registryFiles
registryFiles, err = gen.parseProtoRegistryFiles(true)
if err != nil {
panic(err)
}
gen.ProtoRegistryFilesWithGenerated = registryFiles
gen.ProtoRegistryTypes = dynamicpb.NewTypes(registryFiles)
// NOTE: protoRegistryFilesWithGenerated is lazily computed by
// getProtoRegistryFilesWithGenerated() on first use.
return gen
}

// getProtoRegistryFilesWithGenerated returns a registry including both the
// imported and previously generated protos, computing and caching it on
// first use.
func (gen *Generator) getProtoRegistryFilesWithGenerated() *protoregistry.Files {
if gen.protoRegistryFilesWithGenerated != nil {
return gen.protoRegistryFilesWithGenerated
}
gen.registryWithGeneratedOnce.Do(func() {
files, err := gen.parseProtoRegistryFiles(true)
if err != nil {
panic(err)
}
gen.protoRegistryFilesWithGenerated = files
})
return gen.protoRegistryFilesWithGenerated
}

func (gen *Generator) parseProtoRegistryFiles(useGeneratedProtos bool) (*protoregistry.Files, error) {
outdir := filepath.Join(gen.OutputDir, gen.OutputOpt.Subdir)
var protoFiles []string
Expand All @@ -116,8 +134,10 @@ func (gen *Generator) preprocess(useGeneratedProtos, delExisted bool) error {
outdir := filepath.Join(gen.OutputDir, gen.OutputOpt.Subdir)
// parse custom imported proto files
protoRegistryFiles := gen.ProtoRegistryFiles
if useGeneratedProtos {
protoRegistryFiles = gen.ProtoRegistryFilesWithGenerated
// preserveFieldNumbers also needs generated protos parsed before they
// are deleted below.
if useGeneratedProtos || gen.OutputOpt.PreserveFieldNumbers {
protoRegistryFiles = gen.getProtoRegistryFilesWithGenerated()
}
gen.typeInfos = xproto.GetAllTypeInfo(protoRegistryFiles, gen.ProtoPackage)
return prepareOutdir(outdir, gen.InputOpt.ProtoFiles, delExisted)
Expand Down