4
ast.go
4
ast.go
@@ -10,7 +10,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/fatih/structtag"
|
||||
tag_options "go.unistack.org/micro-proto/v4/tag"
|
||||
tag_options "go.unistack.org/micro-proto/v5/tag"
|
||||
"google.golang.org/protobuf/compiler/protogen"
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
@@ -98,7 +98,7 @@ func (g *Generator) astGenerate(plugin *protogen.Plugin) error {
|
||||
return err
|
||||
}
|
||||
|
||||
fp, err := os.OpenFile(file, os.O_WRONLY|os.O_TRUNC, os.FileMode(0644))
|
||||
fp, err := os.OpenFile(file, os.O_WRONLY|os.O_TRUNC, os.FileMode(0o644))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
syntax = "proto3";
|
||||
|
||||
package example;
|
||||
|
||||
option go_package = "go.unistack.org/protoc-gen-go-micro/v4/example;examplepb";
|
||||
|
||||
message PaddingWaste {
|
||||
bool flag_a = 1;
|
||||
int64 value = 2;
|
||||
bool flag_b = 3;
|
||||
}
|
||||
@@ -2,7 +2,7 @@ syntax = "proto3";
|
||||
|
||||
package example;
|
||||
|
||||
option go_package = "go.unistack.org/protoc-gen-go-micro/v4/example;examplepb";
|
||||
option go_package = "go.unistack.org/protoc-gen-go-micro/v5/example;examplepb";
|
||||
|
||||
import "tag/tag.proto";
|
||||
import "api/annotations.proto";
|
||||
|
||||
@@ -2,7 +2,7 @@ syntax = "proto3";
|
||||
|
||||
package example;
|
||||
|
||||
option go_package = "go.unistack.org/protoc-gen-go-micro/v4/example/out/go;examplepb";
|
||||
option go_package = "go.unistack.org/protoc-gen-go-micro/v5/example/out/go;examplepb";
|
||||
|
||||
service StreamExample {
|
||||
// server-side streaming
|
||||
|
||||
115
fiealaligment.go
115
fiealaligment.go
@@ -1,115 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"go/token"
|
||||
"go/types"
|
||||
"os"
|
||||
"sort"
|
||||
|
||||
"golang.org/x/tools/go/analysis"
|
||||
"golang.org/x/tools/go/analysis/passes/fieldalignment"
|
||||
"golang.org/x/tools/go/analysis/passes/inspect"
|
||||
"golang.org/x/tools/go/packages"
|
||||
"google.golang.org/protobuf/compiler/protogen"
|
||||
)
|
||||
|
||||
func (g *Generator) fieldAlign(plugin *protogen.Plugin) error {
|
||||
if !g.fieldaligment {
|
||||
return nil
|
||||
}
|
||||
|
||||
fset := token.NewFileSet()
|
||||
cfg := &packages.Config{
|
||||
Mode: packages.NeedName | packages.NeedFiles | packages.NeedSyntax |
|
||||
packages.NeedTypes | packages.NeedTypesInfo | packages.NeedTypesSizes,
|
||||
Fset: fset,
|
||||
Dir: g.tagPath,
|
||||
}
|
||||
|
||||
pkgs, err := packages.Load(cfg, ".")
|
||||
if err != nil {
|
||||
return fmt.Errorf("fieldalignment load: %w", err)
|
||||
}
|
||||
|
||||
for _, pkg := range pkgs {
|
||||
if len(pkg.Errors) > 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
makePass := func(a *analysis.Analyzer, resultOf map[*analysis.Analyzer]interface{}) *analysis.Pass {
|
||||
return &analysis.Pass{
|
||||
Analyzer: a,
|
||||
Fset: fset,
|
||||
Files: pkg.Syntax,
|
||||
Pkg: pkg.Types,
|
||||
TypesInfo: pkg.TypesInfo,
|
||||
TypesSizes: pkg.TypesSizes,
|
||||
ResultOf: resultOf,
|
||||
Report: func(d analysis.Diagnostic) {},
|
||||
ImportObjectFact: func(obj types.Object, fact analysis.Fact) bool { return false },
|
||||
ExportObjectFact: func(obj types.Object, fact analysis.Fact) {},
|
||||
ImportPackageFact: func(pkg *types.Package, fact analysis.Fact) bool { return false },
|
||||
ExportPackageFact: func(fact analysis.Fact) {},
|
||||
AllObjectFacts: func() []analysis.ObjectFact { return nil },
|
||||
AllPackageFacts: func() []analysis.PackageFact { return nil },
|
||||
}
|
||||
}
|
||||
|
||||
inspectResult, err := inspect.Analyzer.Run(makePass(inspect.Analyzer, nil))
|
||||
if err != nil {
|
||||
return fmt.Errorf("inspect run: %w", err)
|
||||
}
|
||||
|
||||
var fixes []analysis.SuggestedFix
|
||||
alignPass := makePass(fieldalignment.Analyzer, map[*analysis.Analyzer]interface{}{
|
||||
inspect.Analyzer: inspectResult,
|
||||
})
|
||||
alignPass.Report = func(d analysis.Diagnostic) {
|
||||
fixes = append(fixes, d.SuggestedFixes...)
|
||||
}
|
||||
|
||||
if _, err := fieldalignment.Analyzer.Run(alignPass); err != nil {
|
||||
return fmt.Errorf("fieldalignment run: %w", err)
|
||||
}
|
||||
|
||||
if err := applyTextEdits(fset, fixes); err != nil {
|
||||
return fmt.Errorf("fieldalignment apply: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func applyTextEdits(fset *token.FileSet, fixes []analysis.SuggestedFix) error {
|
||||
byFile := make(map[string][]analysis.TextEdit)
|
||||
for _, fix := range fixes {
|
||||
for _, edit := range fix.TextEdits {
|
||||
fname := fset.Position(edit.Pos).Filename
|
||||
byFile[fname] = append(byFile[fname], edit)
|
||||
}
|
||||
}
|
||||
|
||||
for fname, edits := range byFile {
|
||||
content, err := os.ReadFile(fname)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
sort.Slice(edits, func(i, j int) bool {
|
||||
return edits[i].Pos > edits[j].Pos
|
||||
})
|
||||
|
||||
for _, edit := range edits {
|
||||
start := fset.Position(edit.Pos).Offset
|
||||
end := fset.Position(edit.End).Offset
|
||||
content = append(content[:start], append(edit.NewText, content[end:]...)...)
|
||||
}
|
||||
|
||||
if err := os.WriteFile(fname, content, 0o644); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
10
generate.go
10
generate.go
@@ -1,14 +1,10 @@
|
||||
package main
|
||||
|
||||
//go:generate sh -xc "protoc -I./example -I. -I$(go list -f '{{ .Dir }}' -m go.unistack.org/micro-proto/v4) --go-micro_out=components=graphqls,graphql_file=./schema.graphql:./example example/example.proto"
|
||||
//go:generate sh -xc "protoc -I./example -I. -I$(go list -f '{{ .Dir }}' -m go.unistack.org/micro-proto/v5) --go-micro_out=components=graphqls,graphql_file=./schema.graphql:./example example/example.proto"
|
||||
|
||||
//go:generate go build -o $GOPATH/bin/protoc-gen-go-micro .
|
||||
//go:generate mkdir -p ./example/out/go/grpc ./example/out/align
|
||||
|
||||
// stream_test: micro + grpc
|
||||
//go:generate sh -xc "protoc -I./example -I$(go list -f '{{ .Dir }}' -m go.unistack.org/micro-proto/v4) --go_out=paths=source_relative:./example/out/go --go-micro_out=components=micro,standalone=false,paths=source_relative:./example/out/go ./example/stream_test.proto"
|
||||
//go:generate sh -xc "protoc -I./example -I$(go list -f '{{ .Dir }}' -m go.unistack.org/micro-proto/v4) --go-micro_out=components=grpc,standalone=true,paths=source_relative:./example/out/go/grpc ./example/stream_test.proto"
|
||||
|
||||
// align_test: fieldalignment
|
||||
//go:generate sh -xc "protoc -I./example -I$(go list -f '{{ .Dir }}' -m go.unistack.org/micro-proto/v4) --go_out=paths=source_relative:./example/out/align ./example/align_test.proto"
|
||||
//go:generate sh -xc "protoc -I./example -I$(go list -f '{{ .Dir }}' -m go.unistack.org/micro-proto/v4) --go-micro_out=components=micro,standalone=false,fieldaligment=true,tag_path=./example/out/align,paths=source_relative:./example/out/align ./example/align_test.proto"
|
||||
//go:generate sh -xc "protoc -I./example -I$(go list -f '{{ .Dir }}' -m go.unistack.org/micro-proto/v5) --go_out=paths=source_relative:./example/out/go --go-micro_out=components=micro,standalone=false,paths=source_relative:./example/out/go ./example/stream_test.proto"
|
||||
//go:generate sh -xc "protoc -I./example -I$(go list -f '{{ .Dir }}' -m go.unistack.org/micro-proto/v5) --go-micro_out=components=grpc,standalone=true,paths=source_relative:./example/out/go/grpc ./example/stream_test.proto"
|
||||
|
||||
14
go.mod
14
go.mod
@@ -1,12 +1,12 @@
|
||||
module go.unistack.org/protoc-gen-go-micro/v4
|
||||
module go.unistack.org/protoc-gen-go-micro/v5
|
||||
|
||||
go 1.24.0
|
||||
go 1.24.6
|
||||
|
||||
require (
|
||||
github.com/fatih/structtag v1.2.0
|
||||
github.com/jhump/protoreflect v1.18.0
|
||||
github.com/vektah/gqlparser/v2 v2.5.27
|
||||
go.unistack.org/micro-proto/v4 v4.1.0
|
||||
github.com/vektah/gqlparser/v2 v2.5.33
|
||||
go.unistack.org/micro-proto/v4 v4.1.2
|
||||
golang.org/x/tools v0.42.0
|
||||
google.golang.org/protobuf v1.36.11
|
||||
)
|
||||
@@ -14,9 +14,9 @@ require (
|
||||
require (
|
||||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
|
||||
github.com/golang/protobuf v1.5.4 // indirect
|
||||
github.com/google/gnostic v0.7.0 // indirect
|
||||
github.com/google/gnostic-models v0.6.9 // indirect
|
||||
github.com/jhump/protoreflect/v2 v2.0.0-beta.1 // indirect
|
||||
github.com/google/gnostic v0.7.1 // indirect
|
||||
github.com/google/gnostic-models v0.7.1 // indirect
|
||||
github.com/jhump/protoreflect/v2 v2.0.0-beta.2 // indirect
|
||||
github.com/kr/pretty v0.3.1 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
|
||||
github.com/rogpeppe/go-internal v1.14.1 // indirect
|
||||
|
||||
10
go.sum
10
go.sum
@@ -719,9 +719,13 @@ github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ
|
||||
github.com/google/flatbuffers v2.0.8+incompatible/go.mod h1:1AeVuKshWv4vARoZatz6mlQ0JxURH0Kv5+zNeJKJCa8=
|
||||
github.com/google/gnostic v0.7.0 h1:d7EpuFp8vVdML+y0JJJYiKeOLjKTdH/GvVkLOBWqJpw=
|
||||
github.com/google/gnostic v0.7.0/go.mod h1:IAcUyMl6vtC95f60EZ8oXyqTsOersP6HbwjeG7EyDPM=
|
||||
github.com/google/gnostic v0.7.1 h1:t5Kc7j/8kYr8t2u11rykRrPPovlEMG4+xdc/SpekATs=
|
||||
github.com/google/gnostic v0.7.1/go.mod h1:KSw6sxnxEBFM8jLPfJd46xZP+yQcfE8XkiqfZx5zR28=
|
||||
github.com/google/gnostic-models v0.6.9-0.20230804172637-c7be7c783f49/go.mod h1:BkkQ4L1KS1xMt2aWSPStnn55ChGC0DPOn2FQYj+f25M=
|
||||
github.com/google/gnostic-models v0.6.9 h1:MU/8wDLif2qCXZmzncUQ/BOfxWfthHi63KqpoNbWqVw=
|
||||
github.com/google/gnostic-models v0.6.9/go.mod h1:CiWsm0s6BSQd1hRn8/QmxqB6BesYcbSZxsz9b0KuDBw=
|
||||
github.com/google/gnostic-models v0.7.1 h1:SisTfuFKJSKM5CPZkffwi6coztzzeYUhc3v4yxLWH8c=
|
||||
github.com/google/gnostic-models v0.7.1/go.mod h1:whL5G0m6dmc5cPxKc5bdKdEN3UjI7OUGxBlw57miDrQ=
|
||||
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
|
||||
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
||||
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
||||
@@ -792,6 +796,8 @@ github.com/jhump/protoreflect v1.18.0 h1:TOz0MSR/0JOZ5kECB/0ufGnC2jdsgZ123Rd/k4Z
|
||||
github.com/jhump/protoreflect v1.18.0/go.mod h1:ezWcltJIVF4zYdIFM+D/sHV4Oh5LNU08ORzCGfwvTz8=
|
||||
github.com/jhump/protoreflect/v2 v2.0.0-beta.1 h1:Dw1rslK/VotaUGYsv53XVWITr+5RCPXfvvlGrM/+B6w=
|
||||
github.com/jhump/protoreflect/v2 v2.0.0-beta.1/go.mod h1:D9LBEowZyv8/iSu97FU2zmXG3JxVTmNw21mu63niFzU=
|
||||
github.com/jhump/protoreflect/v2 v2.0.0-beta.2 h1:qZU+rEZUOYTz1Bnhi3xbwn+VxdXkLVeEpAeZzVXLY88=
|
||||
github.com/jhump/protoreflect/v2 v2.0.0-beta.2/go.mod h1:4tnOYkB/mq7QTyS3YKtVtNrJv4Psqout8HA1U+hZtgM=
|
||||
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
|
||||
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
|
||||
github.com/jung-kurt/gofpdf v1.0.0/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+r32jIOes=
|
||||
@@ -866,6 +872,8 @@ github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu
|
||||
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
|
||||
github.com/vektah/gqlparser/v2 v2.5.27 h1:RHPD3JOplpk5mP5JGX8RKZkt2/Vwj/PZv0HxTdwFp0s=
|
||||
github.com/vektah/gqlparser/v2 v2.5.27/go.mod h1:D1/VCZtV3LPnQrcPBeR/q5jkSQIPti0uYCP/RI0gIeo=
|
||||
github.com/vektah/gqlparser/v2 v2.5.33 h1:lRp8aIeNUNbimf/axZd7ETg24q06hBtPaas+TcvI/7E=
|
||||
github.com/vektah/gqlparser/v2 v2.5.33/go.mod h1:c1I28gSOVNzlfc4WuDlqU7voQnsqI6OG2amkBAFmgts=
|
||||
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
|
||||
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
|
||||
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
|
||||
@@ -890,6 +898,8 @@ go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqe
|
||||
go.opentelemetry.io/proto/otlp v0.15.0/go.mod h1:H7XAot3MsfNsj7EXtrA2q5xSNQ10UqI405h3+duxN4U=
|
||||
go.unistack.org/micro-proto/v4 v4.1.0 h1:qPwL2n/oqh9RE3RTTDgt28XK3QzV597VugQPaw9lKUk=
|
||||
go.unistack.org/micro-proto/v4 v4.1.0/go.mod h1:ArmK7o+uFvxSY3dbJhKBBX4Pm1rhWdLEFf3LxBrMtec=
|
||||
go.unistack.org/micro-proto/v4 v4.1.2 h1:gZbW5rPArfmlh53Kg3hFCKQix4jtiVhPospUzgIEl+Q=
|
||||
go.unistack.org/micro-proto/v4 v4.1.2/go.mod h1:NL30Cc3uYP1hwu8cGgYWKCvwJriBmx5l94QIMG2rZy0=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
|
||||
@@ -5,7 +5,7 @@ import (
|
||||
"os"
|
||||
|
||||
"github.com/vektah/gqlparser/v2/formatter"
|
||||
generator "go.unistack.org/protoc-gen-go-micro/v4/graphql"
|
||||
generator "go.unistack.org/protoc-gen-go-micro/v5/graphql"
|
||||
"google.golang.org/protobuf/compiler/protogen"
|
||||
"google.golang.org/protobuf/proto"
|
||||
"google.golang.org/protobuf/types/pluginpb"
|
||||
|
||||
6
main.go
6
main.go
@@ -24,7 +24,6 @@ var (
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
if *flagHelp {
|
||||
flagSet.PrintDefaults()
|
||||
return
|
||||
@@ -127,10 +126,5 @@ func (g *Generator) Generate(plugin *protogen.Plugin) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = g.fieldAlign(plugin); err != nil {
|
||||
plugin.Error(err)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -24,8 +24,8 @@ import (
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"go.unistack.org/micro-proto/v4/api"
|
||||
v3 "go.unistack.org/micro-proto/v4/openapiv3"
|
||||
"go.unistack.org/micro-proto/v5/api"
|
||||
v3 "go.unistack.org/micro-proto/v5/openapiv3"
|
||||
"google.golang.org/protobuf/compiler/protogen"
|
||||
"google.golang.org/protobuf/proto"
|
||||
"google.golang.org/protobuf/reflect/protoreflect"
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package main
|
||||
|
||||
import v3 "go.unistack.org/micro-proto/v4/openapiv3"
|
||||
import v3 "go.unistack.org/micro-proto/v5/openapiv3"
|
||||
|
||||
func getMediaType(eopt interface{}) string {
|
||||
ctype := "application/json"
|
||||
|
||||
2
tools.go
2
tools.go
@@ -3,5 +3,5 @@
|
||||
package tools
|
||||
|
||||
import (
|
||||
_ "go.unistack.org/micro-proto/v4/graphql"
|
||||
_ "go.unistack.org/micro-proto/v5/graphql"
|
||||
)
|
||||
|
||||
6
util.go
6
util.go
@@ -8,9 +8,9 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
apioptions "go.unistack.org/micro-proto/v4/api"
|
||||
v2 "go.unistack.org/micro-proto/v4/openapiv2"
|
||||
v3 "go.unistack.org/micro-proto/v4/openapiv3"
|
||||
apioptions "go.unistack.org/micro-proto/v5/api"
|
||||
v2 "go.unistack.org/micro-proto/v5/openapiv2"
|
||||
v3 "go.unistack.org/micro-proto/v5/openapiv3"
|
||||
|
||||
"google.golang.org/protobuf/compiler/protogen"
|
||||
"google.golang.org/protobuf/proto"
|
||||
|
||||
Reference in New Issue
Block a user