/
rpc_plugin_template.go
96 lines (67 loc) · 1.96 KB
/
rpc_plugin_template.go
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
93
94
95
96
package factory
//nolint:lll
var rpcPluginTemplate = `package main
import (
"unsafe"
"sort"
"encoding/json"
"reflect"
"strings"
"github.com/hashicorp/go-plugin"
"github.com/Hyingerrr/mirco-esim/tool/factory"
"github.com/Hyingerrr/mirco-esim/pkg"
"github.com/Hyingerrr/mirco-esim/log"
"github.com/Hyingerrr/mirco-esim/pkg/file-dir"
)
type InitFieldsReturn struct{
Fields []string
SpecFields pkg.Fields
}
type Return struct{
Fields pkg.Fields
Size int
}
func (ModelImp) Sort() string {
{{.StructName | tolower}} := {{.StructName}}{}
originSize := unsafe.Sizeof({{.StructName | tolower}})
getType := reflect.TypeOf({{.StructName | tolower}})
var fields pkg.Fields
{{range $i, $field := .Fields}}
field{{$i}} := pkg.Field{}
field{{$i}}.Name = "{{$field.Name}}"
field{{$i}}.Field = "{{$field.Field}}"
field{{$i}}.Size = int(getType.Field({{$i}}).Type.Size())
fields = append(fields, field{{$i}})
{{end}}
sort.Sort(fields)
re := &Return{}
re.Fields = fields
re.Size = int(originSize)
by, _ := json.Marshal(re)
return string(by)
}
func (ModelImp) InitField() string {
{{.StructName | tolower}} := {{.StructName}}{}
initReturn := &InitFieldsReturn{}
fields := &pkg.Fields{}
getType := reflect.TypeOf({{.StructName | tolower}})
writer := filedir.NewEsimWriter()
rpcPlugin := factory.NewRPCPluginStructField(writer, log.NewLogger())
structFields := rpcPlugin.GenInitFieldStr(getType, "{{.StructName | snakeToCamelLower | shorten}}", "{{.StructName | snakeToCamelLower | shorten}}", fields)
initReturn.SpecFields = *fields
initReturn.Fields = structFields
j, _ := json.Marshal(initReturn)
return string(j)
}
type ModelImp struct{}
func main() {
plugin.Serve(&plugin.ServeConfig{
HandshakeConfig: factory.HandshakeConfig,
Plugins: map[string]plugin.Plugin{
"model": &factory.ModelPlugin{Impl: &ModelImp{}},
},
// A non-nil value here enables gRPC serving for this plugin...
GRPCServer: plugin.DefaultGRPCServer,
})
}
`