2024-11-29 11:56:57 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"go.wit.com/log"
|
|
|
|
)
|
|
|
|
|
2025-01-09 05:00:29 -06:00
|
|
|
// makes Marshal and Unmarshal functions for protoWIRE protoTEXT and protoJSON
|
|
|
|
func (pb *Files) marshal(f *File) {
|
2024-11-29 11:56:57 -06:00
|
|
|
if argv.DryRun {
|
2025-01-09 05:00:29 -06:00
|
|
|
return
|
2024-11-29 11:56:57 -06:00
|
|
|
}
|
|
|
|
|
2025-01-09 05:00:29 -06:00
|
|
|
w, _ := os.OpenFile(f.Filebase+".marshal.pb.go", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
|
2024-11-29 11:56:57 -06:00
|
|
|
|
2024-11-29 12:00:19 -06:00
|
|
|
headerComment(w)
|
2025-01-09 05:00:29 -06:00
|
|
|
fmt.Fprintf(w, "package %s\n", f.Package)
|
2024-11-29 11:56:57 -06:00
|
|
|
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, "")
|
|
|
|
|
2025-01-10 19:59:01 -06:00
|
|
|
if f.Bases.DoMarshal {
|
|
|
|
marshalThing(w, f.Bases.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if f.Base.DoMarshal {
|
|
|
|
marshalThing(w, f.Base.Name)
|
|
|
|
}
|
|
|
|
|
2025-01-09 05:00:29 -06:00
|
|
|
for _, msg := range f.MsgNames {
|
|
|
|
if msg.DoMarshal {
|
|
|
|
marshalThing(w, msg.Name)
|
|
|
|
} else {
|
|
|
|
log.Info("Skipping. DoMarshal = false for", msg.Name)
|
|
|
|
}
|
2024-11-29 12:35:12 -06:00
|
|
|
}
|
2024-11-29 11:56:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func marshalThing(w io.Writer, thing string) {
|
|
|
|
fmt.Fprintln(w, "// human readable JSON")
|
2024-11-29 12:49:27 -06:00
|
|
|
fmt.Fprintln(w, "func (v *"+thing+") FormatJSON() string {")
|
|
|
|
fmt.Fprintln(w, " return protojson.Format(v)")
|
2024-11-29 11:56:57 -06:00
|
|
|
fmt.Fprintln(w, "}")
|
|
|
|
fmt.Fprintln(w, "")
|
|
|
|
fmt.Fprintln(w, "// marshal json")
|
2024-11-29 12:49:27 -06:00
|
|
|
fmt.Fprintln(w, "func (v *"+thing+") MarshalJSON() ([]byte, error) {")
|
|
|
|
fmt.Fprintln(w, " return protojson.Marshal(v)")
|
2024-11-29 11:56:57 -06:00
|
|
|
fmt.Fprintln(w, "}")
|
|
|
|
fmt.Fprintln(w, "")
|
2024-11-29 12:35:12 -06:00
|
|
|
fmt.Fprintln(w, "// unmarshal json")
|
2024-11-29 12:49:27 -06:00
|
|
|
fmt.Fprintln(w, "func (v *"+thing+") UnmarshalJSON(data []byte) error {")
|
|
|
|
fmt.Fprintln(w, " return protojson.Unmarshal(data, v)")
|
2024-11-29 11:56:57 -06:00
|
|
|
fmt.Fprintln(w, "}")
|
|
|
|
fmt.Fprintln(w, "")
|
2024-11-29 12:35:12 -06:00
|
|
|
fmt.Fprintln(w, "// apparently this isn't stable, but it's awesomely better")
|
|
|
|
fmt.Fprintln(w, "// https://protobuf.dev/reference/go/faq/#unstable-text")
|
2025-01-09 15:03:05 -06:00
|
|
|
fmt.Fprintln(w, "// it's brilliant for config files!")
|
2024-11-29 12:49:27 -06:00
|
|
|
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)")
|
2024-11-29 12:35:12 -06:00
|
|
|
fmt.Fprintln(w, "}")
|
|
|
|
fmt.Fprintln(w, "")
|
|
|
|
fmt.Fprintln(w, "// marshal to wire. This is called winning.")
|
2024-11-29 12:49:27 -06:00
|
|
|
fmt.Fprintln(w, "func (v *"+thing+") Marshal() ([]byte, error) {")
|
|
|
|
fmt.Fprintln(w, " return proto.Marshal(v)")
|
2024-11-29 11:56:57 -06:00
|
|
|
fmt.Fprintln(w, "}")
|
|
|
|
fmt.Fprintln(w, "")
|
2024-11-29 12:35:12 -06:00
|
|
|
fmt.Fprintln(w, "// unmarshal from wire. You have won.")
|
2024-11-29 12:49:27 -06:00
|
|
|
fmt.Fprintln(w, "func (v *"+thing+") Unmarshal(data []byte) error {")
|
|
|
|
fmt.Fprintln(w, " return proto.Unmarshal(data, v)")
|
2024-11-29 11:56:57 -06:00
|
|
|
fmt.Fprintln(w, "}")
|
|
|
|
fmt.Fprintln(w, "")
|
|
|
|
}
|