autogen the marshal file

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2024-11-29 11:56:57 -06:00
parent 07e556e379
commit dc640a6ccb
4 changed files with 127 additions and 7 deletions

View File

@ -9,11 +9,11 @@ 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"` DryRun bool `arg:"--dry-run" help:"show what would be run"`
} }
func (a args) Description() string { func (a args) Description() string {

42
blah_marshal Normal file
View File

@ -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)
}

13
main.go
View File

@ -26,7 +26,7 @@ func main() {
if !shell.Exists(argv.Proto) { if !shell.Exists(argv.Proto) {
log.Info("protobuf", argv.Proto, "is missing") log.Info("protobuf", argv.Proto, "is missing")
if ! argv.DryRun { if !argv.DryRun {
os.Exit(-1) os.Exit(-1)
} }
} }
@ -54,6 +54,7 @@ func main() {
sortmap := make(map[string]string) sortmap := make(map[string]string)
sortmap["package"] = packageName sortmap["package"] = packageName
sortmap["protobase"] = protobase
sortmap["base"] = argv.LoBase sortmap["base"] = argv.LoBase
sortmap["lock"] = sortmap["base"] + "slock" sortmap["lock"] = sortmap["base"] + "slock"
sortmap["Base"] = argv.UpBase sortmap["Base"] = argv.UpBase
@ -79,13 +80,19 @@ func main() {
iterSort(f, sortmap) iterSort(f, sortmap)
iterAppend(f, sortmap) iterAppend(f, sortmap)
iterEnd(f, sortmap) iterEnd(f, sortmap)
// make the foo.marshal.pb.go file
marshal(sortmap)
} }
func header(w io.Writer, names map[string]string) { func header(w io.Writer, names map[string]string) {
fmt.Fprintln(w, "package "+names["package"]) fmt.Fprintln(w, "package "+names["package"])
fmt.Fprintln(w, "") fmt.Fprintln(w, "")
fmt.Fprintln(w, "// this is becoming a standard format") fmt.Fprintln(w, "// this file was autogenerated with autogenpb")
fmt.Fprintln(w, "// todo: autogenerate this from the .proto file?") 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, "")
fmt.Fprintln(w, "import (") fmt.Fprintln(w, "import (")
fmt.Fprintln(w, " \"fmt\"") fmt.Fprintln(w, " \"fmt\"")

71
marshal.go Normal file
View File

@ -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, "")
}