2015-06-08 11:33:06 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"go/format"
|
|
|
|
"os"
|
|
|
|
"unicode"
|
|
|
|
"unicode/utf8"
|
|
|
|
)
|
|
|
|
|
|
|
|
type stringSet struct {
|
|
|
|
values map[string]struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s stringSet) String() string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s stringSet) Set(value string) error {
|
|
|
|
s.values[value] = struct{}{}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (s stringSet) GetValues() map[string]struct{} {
|
|
|
|
return s.values
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
typeName = flag.String("type", "", "interface type to generate plugin rpc proxy for")
|
|
|
|
rpcName = flag.String("name", *typeName, "RPC name, set if different from type")
|
|
|
|
inputFile = flag.String("i", "", "input file path")
|
|
|
|
outputFile = flag.String("o", *inputFile+"_proxy.go", "output file path")
|
|
|
|
|
|
|
|
skipFuncs map[string]struct{}
|
|
|
|
flSkipFuncs = stringSet{make(map[string]struct{})}
|
|
|
|
|
|
|
|
flBuildTags = stringSet{make(map[string]struct{})}
|
|
|
|
)
|
|
|
|
|
|
|
|
func errorOut(msg string, err error) {
|
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Fprintf(os.Stderr, "%s: %v\n", msg, err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkFlags() error {
|
|
|
|
if *outputFile == "" {
|
|
|
|
return fmt.Errorf("missing required flag `-o`")
|
|
|
|
}
|
|
|
|
if *inputFile == "" {
|
|
|
|
return fmt.Errorf("missing required flag `-i`")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Var(flSkipFuncs, "skip", "skip parsing for function")
|
|
|
|
flag.Var(flBuildTags, "tag", "build tags to add to generated files")
|
|
|
|
flag.Parse()
|
|
|
|
skipFuncs = flSkipFuncs.GetValues()
|
|
|
|
|
|
|
|
errorOut("error", checkFlags())
|
|
|
|
|
|
|
|
pkg, err := Parse(*inputFile, *typeName)
|
|
|
|
errorOut(fmt.Sprintf("error parsing requested type %s", *typeName), err)
|
|
|
|
|
|
|
|
var analysis = struct {
|
|
|
|
InterfaceType string
|
|
|
|
RPCName string
|
|
|
|
BuildTags map[string]struct{}
|
2015-09-04 08:55:56 -04:00
|
|
|
*ParsedPkg
|
2015-06-08 11:33:06 -04:00
|
|
|
}{toLower(*typeName), *rpcName, flBuildTags.GetValues(), pkg}
|
|
|
|
var buf bytes.Buffer
|
|
|
|
|
|
|
|
errorOut("parser error", generatedTempl.Execute(&buf, analysis))
|
|
|
|
src, err := format.Source(buf.Bytes())
|
2016-04-11 11:17:52 -04:00
|
|
|
errorOut("error formatting generated source:\n"+buf.String(), err)
|
2021-08-24 06:10:50 -04:00
|
|
|
errorOut("error writing file", os.WriteFile(*outputFile, src, 0644))
|
2015-06-08 11:33:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func toLower(s string) string {
|
|
|
|
if s == "" {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
r, n := utf8.DecodeRuneInString(s)
|
|
|
|
return string(unicode.ToLower(r)) + s[n:]
|
|
|
|
}
|