diff --git a/argv.go b/argv.go index fb1eedd..50ac4d9 100644 --- a/argv.go +++ b/argv.go @@ -9,11 +9,11 @@ package main var argv args type args struct { - LoBase string `arg:"--lobase" help:"lowercase basename"` - UpBase string `arg:"--upbase" help:"uppercase basename"` - Proto string `arg:"--proto" help:"the .proto filename"` + LoBase string `arg:"--lobase" help:"lowercase basename"` + UpBase string `arg:"--upbase" help:"uppercase basename"` + Proto string `arg:"--proto" help:"the .proto filename"` Sort []string `arg:"--sort" help:"how and what to sort on"` - DryRun bool `arg:"--dry-run" help:"show what would be run"` + DryRun bool `arg:"--dry-run" help:"show what would be run"` } func (a args) Description() string { diff --git a/blah_marshal b/blah_marshal new file mode 100644 index 0000000..738ae35 --- /dev/null +++ b/blah_marshal @@ -0,0 +1,42 @@ +package gitpb + +// todo: autogen this +// functions to import and export the protobuf + +import ( + "google.golang.org/protobuf/encoding/protojson" + "google.golang.org/protobuf/encoding/prototext" + "google.golang.org/protobuf/proto" +) + +// human readable JSON +func (r *Repo) FormatJSON() string { + return protojson.Format(r) +} + +// apparently this isn't supposed to be used? +// https://protobuf.dev/reference/go/faq/#unstable-text +// this is a shame because this is much nicer output than JSON Format() +func (r *Repo) FormatTEXT() string { + return prototext.Format(r) +} + +// marshal json +func (r *Repo) MarshalJSON() ([]byte, error) { + return protojson.Marshal(r) +} + +// unmarshal +func (r *Repo) UnmarshalJSON(data []byte) error { + return protojson.Unmarshal(data, r) +} + +// marshal to wire +func (r *Repo) Marshal() ([]byte, error) { + return proto.Marshal(r) +} + +// unmarshal from wire +func (r *Repo) Unmarshal(data []byte) error { + return proto.Unmarshal(data, r) +} diff --git a/main.go b/main.go index 1f0f6b9..312843b 100644 --- a/main.go +++ b/main.go @@ -26,7 +26,7 @@ func main() { if !shell.Exists(argv.Proto) { log.Info("protobuf", argv.Proto, "is missing") - if ! argv.DryRun { + if !argv.DryRun { os.Exit(-1) } } @@ -54,6 +54,7 @@ func main() { sortmap := make(map[string]string) sortmap["package"] = packageName + sortmap["protobase"] = protobase sortmap["base"] = argv.LoBase sortmap["lock"] = sortmap["base"] + "slock" sortmap["Base"] = argv.UpBase @@ -79,13 +80,19 @@ func main() { iterSort(f, sortmap) iterAppend(f, sortmap) iterEnd(f, sortmap) + + // make the foo.marshal.pb.go file + marshal(sortmap) } func header(w io.Writer, names map[string]string) { fmt.Fprintln(w, "package "+names["package"]) fmt.Fprintln(w, "") - fmt.Fprintln(w, "// this is becoming a standard format") - fmt.Fprintln(w, "// todo: autogenerate this from the .proto file?") + fmt.Fprintln(w, "// this file was autogenerated with autogenpb") + fmt.Fprintln(w, "//") + fmt.Fprintln(w, "// you might be able to use it on simple, strictly defined protobuf files") + fmt.Fprintln(w, "//") + fmt.Fprintln(w, "// go install go.wit.com/apps/autogenpb@latest") fmt.Fprintln(w, "") fmt.Fprintln(w, "import (") fmt.Fprintln(w, " \"fmt\"") diff --git a/marshal.go b/marshal.go new file mode 100644 index 0000000..462468c --- /dev/null +++ b/marshal.go @@ -0,0 +1,71 @@ +package main + +import ( + "fmt" + "io" + "os" + + "go.wit.com/log" +) + +func marshal(names map[string]string) { + + if argv.DryRun { + for k, v := range names { + log.Info(k, "=", v) + } + os.Exit(0) + } + + w, _ := os.OpenFile(names["protobase"]+".marshal.pb.go", os.O_WRONLY|os.O_CREATE, 0600) + + fmt.Fprintln(w, "package "+names["package"]) + fmt.Fprintln(w, "") + fmt.Fprintln(w, "// todo: autogen this") + fmt.Fprintln(w, "// functions to import and export the protobuf") + fmt.Fprintln(w, "") + fmt.Fprintln(w, "import (") + fmt.Fprintln(w, " \"google.golang.org/protobuf/encoding/protojson\"") + fmt.Fprintln(w, " \"google.golang.org/protobuf/encoding/prototext\"") + fmt.Fprintln(w, " \"google.golang.org/protobuf/proto\"") + fmt.Fprintln(w, ")") + fmt.Fprintln(w, "") + + marshalThing(w, names["Base"]) + marshalThing(w, names["Bases"]) +} + +func marshalThing(w io.Writer, thing string) { + fmt.Fprintln(w, "// human readable JSON") + fmt.Fprintln(w, "func (r *"+thing+") FormatJSON() string {") + fmt.Fprintln(w, " return protojson.Format(r)") + fmt.Fprintln(w, "}") + fmt.Fprintln(w, "") + fmt.Fprintln(w, "// apparently this isn't supposed to be used?") + fmt.Fprintln(w, "// https://protobuf.dev/reference/go/faq/#unstable-text") + fmt.Fprintln(w, "// this is a shame because this is much nicer output than JSON Format()") + 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, "func (r *"+thing+") MarshalJSON() ([]byte, error) {") + fmt.Fprintln(w, " return protojson.Marshal(r)") + fmt.Fprintln(w, "}") + fmt.Fprintln(w, "") + fmt.Fprintln(w, "// unmarshal") + fmt.Fprintln(w, "func (r *"+thing+") UnmarshalJSON(data []byte) error {") + fmt.Fprintln(w, " return protojson.Unmarshal(data, r)") + fmt.Fprintln(w, "}") + fmt.Fprintln(w, "") + fmt.Fprintln(w, "// marshal to wire") + fmt.Fprintln(w, "func (r *"+thing+") Marshal() ([]byte, error) {") + fmt.Fprintln(w, " return proto.Marshal(r)") + fmt.Fprintln(w, "}") + fmt.Fprintln(w, "") + fmt.Fprintln(w, "// unmarshal from wire") + fmt.Fprintln(w, "func (r *"+thing+") Unmarshal(data []byte) error {") + fmt.Fprintln(w, " return proto.Unmarshal(data, r)") + fmt.Fprintln(w, "}") + fmt.Fprintln(w, "") +}