diff options
| author | Jeff Carr <[email protected]> | 2024-11-29 12:49:27 -0600 |
|---|---|---|
| committer | Jeff Carr <[email protected]> | 2024-11-29 12:49:27 -0600 |
| commit | bdd91505e06af763437a4d6a53a0796f73556e38 (patch) | |
| tree | fa83b22bb3a2336aff3f551e8c3ec054989454a2 | |
| parent | 7a1b9608581ad4f2169198be1cbd30093beaa5b3 (diff) | |
add UnmarshalTEXT()
Signed-off-by: Jeff Carr <[email protected]>
| -rw-r--r-- | marshal.go | 29 |
1 files changed, 17 insertions, 12 deletions
@@ -40,35 +40,40 @@ func marshal(names map[string]string) { 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, "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 (r *"+thing+") MarshalJSON() ([]byte, error) {") - fmt.Fprintln(w, " return protojson.Marshal(r)") + 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 (r *"+thing+") UnmarshalJSON(data []byte) error {") - fmt.Fprintln(w, " return protojson.Unmarshal(data, r)") + 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 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, "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 (r *"+thing+") Marshal() ([]byte, error) {") - fmt.Fprintln(w, " return proto.Marshal(r)") + 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 (r *"+thing+") Unmarshal(data []byte) error {") - fmt.Fprintln(w, " return proto.Unmarshal(data, r)") + fmt.Fprintln(w, "func (v *"+thing+") Unmarshal(data []byte) error {") + fmt.Fprintln(w, " return proto.Unmarshal(data, v)") fmt.Fprintln(w, "}") fmt.Fprintln(w, "") } |
