-
Notifications
You must be signed in to change notification settings - Fork 1
/
converter.go
123 lines (122 loc) · 4.56 KB
/
converter.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package main
//
//import (
// "strings"
//
// "github.com/anz-bank/protoc-gen-sysl/sysloption"
// "github.com/anz-bank/protoc-gen-sysl/syslpopulate"
// "google.golang.org/protobuf/proto"
// "google.golang.org/protobuf/types/descriptorpb"
//
// "github.com/anz-bank/sysl/pkg/sysl"
// pgs "github.com/lyft/protoc-gen-star"
//)
//
//func syslPackageName(m pgs.Entity) string {
// return strings.ReplaceAll(strings.ReplaceAll(m.Package().ProtoName().String(), ".", " "), " ", "")
//}
//
//// fieldToString converts a field type to a string and returns name and type respectively
//func (p *PrinterModule) fieldToSysl(e pgs.Field) (string, *sysl.Type) {
// var fieldName, fieldType string
// application := syslPackageName(e)
// fieldName = e.Name().String()
// if t := e.Descriptor(); t != nil && t.TypeName != nil {
// if arr := NoEmptyStrings(strings.Split(*t.TypeName, ".")); len(arr) > 1 {
//
// // This is some wack logic to process messages and enums that are defined in other messages
// fieldType = arr[len(arr)-1]
// remove := strings.ReplaceAll(e.Message().FullyQualifiedName(), e.Message().Parent().FullyQualifiedName(), "")
// remove = strings.ReplaceAll(remove, ".", "")
// application = strings.ReplaceAll(strings.Join(arr[:len(arr)-1], ""), remove, "")
// if enum := e.Type().Enum(); enum != nil {
// remove = strings.ReplaceAll(enum.FullyQualifiedName(), e.Message().Parent().FullyQualifiedName(), "")
// remove = strings.ReplaceAll(remove, fieldType, "")
// remove = strings.ReplaceAll(remove, ".", "")
//
// application = strings.ReplaceAll(application, remove, "")
//
// }
// } else {
// fieldType = strings.ReplaceAll(*t.TypeName, e.Package().ProtoName().String(), "")
// fieldType = strings.ReplaceAll(fieldType, ".", "")
// }
// } else {
// fieldType = e.Type().ProtoType().String()
// }
// fieldType = syslpopulate.SanitiseTypeName(fieldType)
// if *e.Descriptor().Label == descriptorpb.FieldDescriptorProto_LABEL_REPEATED {
// return fieldName, &sysl.Type{
// Type: &sysl.Type_Sequence{
// Sequence: syslpopulate.NewType(fieldType, application),
// },
// }
// }
// return fieldName, syslpopulate.NewType(fieldType, application)
//}
//
//// messageToSysl converts a message to a sysl type
//func messageToSysl(e pgs.Message) string {
// var fieldType string
// if t := e.Descriptor(); t != nil && t.Name != nil {
// fieldType = strings.ReplaceAll(*t.Name, syslPackageName(e), "")
// fieldType = strings.ReplaceAll(fieldType, ".", "")
// fieldType = syslpopulate.SanitiseTypeName(fieldType)
// }
// return fieldType
//}
//
//// customOption converts a pgs method to a slice of the sysl CallRule
//func customOption(meth pgs.Method) []*sysloption.CallRule {
// var call []*sysloption.CallRule
// if proto.HasExtension(meth.Descriptor().Options, sysloption.E_Calls) {
// this := proto.GetExtension(meth.Descriptor().Options, sysloption.E_Calls)
// call = this.([]*sysloption.CallRule)
// }
// return call
//}
//
//// EndpointFromMethod converts a pgs Method to a sysl endpoint and fills in call and return statments
//func endpointFromMethod(method pgs.Method) (*sysl.Endpoint, map[string]string) {
// callOption := customOption(method)
// syslCalls := []*sysl.Statement{}
// stringCalls := make(map[string]string)
// application := syslPackageName(method)
// for _, calls := range callOption {
// for _, call := range calls.Call {
// syslCallSplit := strings.Split(call, ".")
// syslCalls = append(syslCalls, syslpopulate.NewCall(syslCallSplit[0], syslCallSplit[1]))
// stringCalls[syslCallSplit[0]] = syslCallSplit[1]
// }
// }
// endpoint := syslpopulate.NewEndpoint(method.Name().String())
// endpoint.Param = []*sysl.Param{syslpopulate.NewParameter(messageToSysl(method.Input()), application)}
// endpoint.Stmt = append(syslCalls, syslpopulate.NewReturn(syslPackageName(method.Output()), method.Output().Name().String()))
// return endpoint, stringCalls
//}
//
//// syslFilename converts replaces a .proto filename to .sysl, removing any paths
////func syslFilename(s string) string {
//// return strings.Replace(regexp.MustCompile(`(?m)\w*\.proto`).FindString(s), ".proto", "", -1)
////}
//
//// enumToSysl converts an Enum to a sysl enum
//func enumToSysl(e pgs.Enum) map[string]int64 {
// values := make(map[string]int64)
// if t := e.Descriptor(); t != nil && t.Name != nil {
// for _, val := range t.Value {
// values[*val.Name] = int64(*val.Number)
// }
// }
// return values
//}
//
//func NoEmptyStrings(in []string) []string {
// out := make([]string, 0, len(in))
// for _, element := range in {
// if element != "" {
// out = append(out, element)
// }
// }
// return out
//}