diff options
| author | Jeff Carr <[email protected]> | 2025-01-11 04:03:41 -0600 |
|---|---|---|
| committer | Jeff Carr <[email protected]> | 2025-01-11 04:03:41 -0600 |
| commit | 1f9e4a682d3acf36aa7ee1279f1f9cc928bcd5fd (patch) | |
| tree | eac1d0677459b6cc55fc4eb292c6545d42315cbc /generateMarshal.go | |
| parent | d8464bf21ffada0be45116e7a6c4573bcadbce1b (diff) | |
rename files
Diffstat (limited to 'generateMarshal.go')
| -rw-r--r-- | generateMarshal.go | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/generateMarshal.go b/generateMarshal.go new file mode 100644 index 0000000..9cb1a4e --- /dev/null +++ b/generateMarshal.go @@ -0,0 +1,83 @@ +package main + +import ( + "fmt" + "io" + "os" + + "go.wit.com/log" +) + +// makes Marshal and Unmarshal functions for protoWIRE protoTEXT and protoJSON +func (pb *Files) marshal(f *File) { + if argv.DryRun { + return + } + + w, _ := os.OpenFile(f.Filebase+".marshal.pb.go", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) + + headerComment(w) + fmt.Fprintf(w, "package %s\n", f.Package) + 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, "") + + if f.Bases.DoMarshal { + marshalThing(w, f.Bases.Name) + } + + if f.Base.DoMarshal { + marshalThing(w, f.Base.Name) + } + + for _, msg := range f.MsgNames { + if msg.DoMarshal { + marshalThing(w, msg.Name) + } else { + log.Info("Skipping. DoMarshal = false for", msg.Name) + } + } +} + +func marshalThing(w io.Writer, thing string) { + fmt.Fprintln(w, "// human readable JSON") + fmt.Fprintln(w, "func (v *"+thing+") FormatJSON() string {") + fmt.Fprintln(w, " return protojson.Format(v)") + fmt.Fprintln(w, "}") + fmt.Fprintln(w, "") + fmt.Fprintln(w, "// marshal json") + fmt.Fprintln(w, "func (v *"+thing+") MarshalJSON() ([]byte, error) {") + fmt.Fprintln(w, " return protojson.Marshal(v)") + fmt.Fprintln(w, "}") + fmt.Fprintln(w, "") + fmt.Fprintln(w, "// unmarshal json") + fmt.Fprintln(w, "func (v *"+thing+") UnmarshalJSON(data []byte) error {") + fmt.Fprintln(w, " return protojson.Unmarshal(data, v)") + fmt.Fprintln(w, "}") + fmt.Fprintln(w, "") + 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 brilliant for config files!") + fmt.Fprintln(w, "func (v *"+thing+") FormatTEXT() string {") + fmt.Fprintln(w, " return prototext.Format(v)") + fmt.Fprintln(w, "}") + fmt.Fprintln(w, "") + fmt.Fprintln(w, "// unmarshalTEXT. This reads the .text config file back in after the user edits it") + fmt.Fprintln(w, "func (v *"+thing+") UnmarshalTEXT(data []byte) error {") + fmt.Fprintln(w, " return prototext.Unmarshal(data, v)") + fmt.Fprintln(w, "}") + fmt.Fprintln(w, "") + fmt.Fprintln(w, "// marshal to wire. This is called winning.") + fmt.Fprintln(w, "func (v *"+thing+") Marshal() ([]byte, error) {") + fmt.Fprintln(w, " return proto.Marshal(v)") + fmt.Fprintln(w, "}") + fmt.Fprintln(w, "") + fmt.Fprintln(w, "// unmarshal from wire. You have won.") + fmt.Fprintln(w, "func (v *"+thing+") Unmarshal(data []byte) error {") + fmt.Fprintln(w, " return proto.Unmarshal(data, v)") + fmt.Fprintln(w, "}") + fmt.Fprintln(w, "") +} |
