marshal argv options
Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
parent
45b343bee8
commit
7a1b960858
12
argv.go
12
argv.go
|
@ -9,11 +9,13 @@ package main
|
||||||
var argv args
|
var argv args
|
||||||
|
|
||||||
type args struct {
|
type args struct {
|
||||||
LoBase string `arg:"--lobase" help:"lowercase basename"`
|
LoBase string `arg:"--lobase" help:"lowercase basename"`
|
||||||
UpBase string `arg:"--upbase" help:"uppercase basename"`
|
UpBase string `arg:"--upbase" help:"uppercase basename"`
|
||||||
Proto string `arg:"--proto" help:"the .proto filename"`
|
Proto string `arg:"--proto" help:"the .proto filename"`
|
||||||
Sort []string `arg:"--sort" help:"how and what to sort on"`
|
Sort []string `arg:"--sort" help:"how and what to sort on"`
|
||||||
DryRun bool `arg:"--dry-run" help:"show what would be run"`
|
Marshal []string `arg:"--marshal" help:"what to marshal on"`
|
||||||
|
NoMarshal bool `arg:"--no-marshal" help:"do not make a marshal.pb.go file"`
|
||||||
|
DryRun bool `arg:"--dry-run" help:"show what would be run"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a args) Description() string {
|
func (a args) Description() string {
|
||||||
|
|
8
main.go
8
main.go
|
@ -81,8 +81,12 @@ func main() {
|
||||||
iterAppend(f, sortmap)
|
iterAppend(f, sortmap)
|
||||||
iterEnd(f, sortmap)
|
iterEnd(f, sortmap)
|
||||||
|
|
||||||
// make the foo.marshal.pb.go file
|
if argv.NoMarshal {
|
||||||
marshal(sortmap)
|
log.Info("not making marshal.pb.go file (--no-marshal == true)")
|
||||||
|
} else {
|
||||||
|
// make the foo.marshal.pb.go file
|
||||||
|
marshal(sortmap)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func headerComment(w io.Writer) {
|
func headerComment(w io.Writer) {
|
||||||
|
|
30
marshal.go
30
marshal.go
|
@ -28,8 +28,14 @@ func marshal(names map[string]string) {
|
||||||
fmt.Fprintln(w, ")")
|
fmt.Fprintln(w, ")")
|
||||||
fmt.Fprintln(w, "")
|
fmt.Fprintln(w, "")
|
||||||
|
|
||||||
marshalThing(w, names["Base"])
|
if len(argv.Marshal) == 0 {
|
||||||
marshalThing(w, names["Bases"])
|
marshalThing(w, names["Base"])
|
||||||
|
marshalThing(w, names["Bases"])
|
||||||
|
} else {
|
||||||
|
for _, v := range argv.Marshal {
|
||||||
|
marshalThing(w, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func marshalThing(w io.Writer, thing string) {
|
func marshalThing(w io.Writer, thing string) {
|
||||||
|
@ -38,29 +44,29 @@ func marshalThing(w io.Writer, thing string) {
|
||||||
fmt.Fprintln(w, " return protojson.Format(r)")
|
fmt.Fprintln(w, " return protojson.Format(r)")
|
||||||
fmt.Fprintln(w, "}")
|
fmt.Fprintln(w, "}")
|
||||||
fmt.Fprintln(w, "")
|
fmt.Fprintln(w, "")
|
||||||
fmt.Fprintln(w, "// apparently this isn't stable")
|
|
||||||
fmt.Fprintln(w, "// https://protobuf.dev/reference/go/faq/#unstable-text")
|
|
||||||
fmt.Fprintln(w, "// but it's so awesome I'm using it by default to try to fix the problems with it")
|
|
||||||
fmt.Fprintln(w, "func (r *"+thing+") FormatTEXT() string {")
|
|
||||||
fmt.Fprintln(w, " return prototext.Format(r)")
|
|
||||||
fmt.Fprintln(w, "}")
|
|
||||||
fmt.Fprintln(w, "")
|
|
||||||
fmt.Fprintln(w, "// marshal json")
|
fmt.Fprintln(w, "// marshal json")
|
||||||
fmt.Fprintln(w, "func (r *"+thing+") MarshalJSON() ([]byte, error) {")
|
fmt.Fprintln(w, "func (r *"+thing+") MarshalJSON() ([]byte, error) {")
|
||||||
fmt.Fprintln(w, " return protojson.Marshal(r)")
|
fmt.Fprintln(w, " return protojson.Marshal(r)")
|
||||||
fmt.Fprintln(w, "}")
|
fmt.Fprintln(w, "}")
|
||||||
fmt.Fprintln(w, "")
|
fmt.Fprintln(w, "")
|
||||||
fmt.Fprintln(w, "// unmarshal")
|
fmt.Fprintln(w, "// unmarshal json")
|
||||||
fmt.Fprintln(w, "func (r *"+thing+") UnmarshalJSON(data []byte) error {")
|
fmt.Fprintln(w, "func (r *"+thing+") UnmarshalJSON(data []byte) error {")
|
||||||
fmt.Fprintln(w, " return protojson.Unmarshal(data, r)")
|
fmt.Fprintln(w, " return protojson.Unmarshal(data, r)")
|
||||||
fmt.Fprintln(w, "}")
|
fmt.Fprintln(w, "}")
|
||||||
fmt.Fprintln(w, "")
|
fmt.Fprintln(w, "")
|
||||||
fmt.Fprintln(w, "// marshal to wire")
|
fmt.Fprintln(w, "// apparently this isn't stable, but it's awesomely better")
|
||||||
|
fmt.Fprintln(w, "// https://protobuf.dev/reference/go/faq/#unstable-text")
|
||||||
|
fmt.Fprintln(w, "// it's so great for config files, I'm using it by default to try to fix the problems with it")
|
||||||
|
fmt.Fprintln(w, "func (r *"+thing+") FormatTEXT() string {")
|
||||||
|
fmt.Fprintln(w, " return prototext.Format(r)")
|
||||||
|
fmt.Fprintln(w, "}")
|
||||||
|
fmt.Fprintln(w, "")
|
||||||
|
fmt.Fprintln(w, "// marshal to wire. This is called winning.")
|
||||||
fmt.Fprintln(w, "func (r *"+thing+") Marshal() ([]byte, error) {")
|
fmt.Fprintln(w, "func (r *"+thing+") Marshal() ([]byte, error) {")
|
||||||
fmt.Fprintln(w, " return proto.Marshal(r)")
|
fmt.Fprintln(w, " return proto.Marshal(r)")
|
||||||
fmt.Fprintln(w, "}")
|
fmt.Fprintln(w, "}")
|
||||||
fmt.Fprintln(w, "")
|
fmt.Fprintln(w, "")
|
||||||
fmt.Fprintln(w, "// unmarshal from wire")
|
fmt.Fprintln(w, "// unmarshal from wire. You have won.")
|
||||||
fmt.Fprintln(w, "func (r *"+thing+") Unmarshal(data []byte) error {")
|
fmt.Fprintln(w, "func (r *"+thing+") Unmarshal(data []byte) error {")
|
||||||
fmt.Fprintln(w, " return proto.Unmarshal(data, r)")
|
fmt.Fprintln(w, " return proto.Unmarshal(data, r)")
|
||||||
fmt.Fprintln(w, "}")
|
fmt.Fprintln(w, "}")
|
||||||
|
|
|
@ -6,7 +6,7 @@ test: vet
|
||||||
all: clean test.pb.go run vet
|
all: clean test.pb.go run vet
|
||||||
|
|
||||||
run:
|
run:
|
||||||
../autogenpb --proto test.proto --lobase gitTag --upbase GitTag --sort "ByPath,Refname"
|
../autogenpb --proto test.proto --lobase gitTag --upbase GitTag --sort "ByPath,Refname" --marshal GitTags
|
||||||
|
|
||||||
vet:
|
vet:
|
||||||
@GO111MODULE=off go vet
|
@GO111MODULE=off go vet
|
||||||
|
|
Loading…
Reference in New Issue