autogenpb/generateHeader.go

80 lines
2.5 KiB
Go

package main
import (
"fmt"
"io"
"os"
"go.wit.com/log"
)
func pbHeaderComment(w io.Writer) {
// technically this should be the first line and in this exact format:
fmt.Fprintln(w, "// Code modified by go.wit.com/apps/autogenpb DO NOT EDIT.")
fmt.Fprintln(w, "//")
fmt.Fprintln(w, "// user defined Mutex locks were auto added")
fmt.Fprintln(w, "//")
fmt.Fprintln(w, "// autogenpb version & build time:", VERSION, BUILDTIME)
fmt.Fprintln(w, "// autogenpb auto generates Sort(), Unique() and Marshal() functions")
fmt.Fprintln(w, "// go install go.wit.com/apps/autogenpb@latest")
fmt.Fprintln(w, "")
}
func headerComment(w io.Writer) {
// technically this should be the first line and in this exact format:
fmt.Fprintln(w, "// Code generated by go.wit.com/apps/autogenpb DO NOT EDIT.")
fmt.Fprintln(w, "// This file was autogenerated with autogenpb", VERSION, BUILDTIME)
fmt.Fprintln(w, "// go install go.wit.com/apps/autogenpb@latest")
fmt.Fprintln(w, "//")
fmt.Fprintln(w, "// define which structs (messages) you want to use in the .proto file")
fmt.Fprintln(w, "// Then sort.pb.go and marshal.pb.go files are autogenerated")
fmt.Fprintln(w, "//")
fmt.Fprintln(w, "// autogenpb uses it and has an example .proto file with instructions")
fmt.Fprintln(w, "//")
fmt.Fprintln(w, "")
}
func header(w io.Writer, pf *File) {
// header must come first
headerComment(w)
fmt.Fprintf(w, "package %s\n", pf.Package)
fmt.Fprintln(w, "")
fmt.Fprintln(w, "import (")
// fmt.Fprintln(w, " \"fmt\"")
fmt.Fprintln(w, " \"sort\"")
fmt.Fprintln(w, " \"sync\"")
fmt.Fprintln(w, ")")
fmt.Fprintln(w, "")
}
func (pf *File) addNewFunc(w io.Writer) {
var STRUCT string = pf.Bases.Name
var UUID string = pf.Uuid
var VERSION string = pf.Version
if UUID == "" {
log.Info("error: UUID == ''")
os.Exit(-1)
}
if VERSION == "" {
log.Info("error: Version == ''")
os.Exit(-1)
}
fmt.Fprintln(w, "func (x *"+STRUCT+") fixUuid() {")
fmt.Fprintln(w, " if x == nil {")
fmt.Fprintln(w, " return")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " if x.Uuid == \""+UUID+"\" {")
fmt.Fprintln(w, " return")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " x.Uuid = \""+UUID+"\"")
fmt.Fprintln(w, " x.Version = \""+VERSION+" "+pf.GoPath+"\"")
fmt.Fprintln(w, "}")
fmt.Fprintln(w, "")
fmt.Fprintln(w, "func New"+STRUCT+"() *"+STRUCT+"{")
fmt.Fprintln(w, " x := new("+STRUCT+")")
fmt.Fprintln(w, " x.Uuid = \""+UUID+"\"")
fmt.Fprintln(w, " x.Version = \""+VERSION+" "+pf.GoPath+"\"")
fmt.Fprintln(w, " return x")
fmt.Fprintln(w, "}")
}