autogen the marshal file
Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
parent
07e556e379
commit
dc640a6ccb
|
@ -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)
|
||||
}
|
11
main.go
11
main.go
|
@ -54,6 +54,7 @@ func main() {
|
|||
|
||||
sortmap := make(map[string]string)
|
||||
sortmap["package"] = packageName
|
||||
sortmap["protobase"] = protobase
|
||||
sortmap["base"] = argv.LoBase
|
||||
sortmap["lock"] = sortmap["base"] + "slock"
|
||||
sortmap["Base"] = argv.UpBase
|
||||
|
@ -79,13 +80,19 @@ func main() {
|
|||
iterSort(f, sortmap)
|
||||
iterAppend(f, sortmap)
|
||||
iterEnd(f, sortmap)
|
||||
|
||||
// make the foo.marshal.pb.go file
|
||||
marshal(sortmap)
|
||||
}
|
||||
|
||||
func header(w io.Writer, names map[string]string) {
|
||||
fmt.Fprintln(w, "package "+names["package"])
|
||||
fmt.Fprintln(w, "")
|
||||
fmt.Fprintln(w, "// this is becoming a standard format")
|
||||
fmt.Fprintln(w, "// todo: autogenerate this from the .proto file?")
|
||||
fmt.Fprintln(w, "// this file was autogenerated with autogenpb")
|
||||
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, "import (")
|
||||
fmt.Fprintln(w, " \"fmt\"")
|
||||
|
|
|
@ -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, "")
|
||||
}
|
Loading…
Reference in New Issue