1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
package main
import (
"fmt"
"io"
"os"
)
// 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 {
if f.Uuid == "" {
marshalThing(w, f.Bases.Name, false)
} else {
// if the Uuid is set, run fixUuid() in Marshal()
marshalThing(w, f.Bases.Name, true)
}
}
if f.Base.DoMarshal {
marshalThing(w, f.Base.Name, false)
}
for _, msg := range f.MsgNames {
if msg.DoMarshal {
marshalThing(w, msg.Name, false)
} else {
// log.Info("Skipping. DoMarshal = false for", msg.Name)
}
}
}
func marshalThing(w io.Writer, thing string, fix bool) {
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 {")
if fix {
fmt.Fprintln(w, " v.fixUuid()")
}
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) {")
if fix {
fmt.Fprintln(w, " v.fixUuid()")
}
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, "")
}
|