-
Notifications
You must be signed in to change notification settings - Fork 4
/
sync-metadata.go
167 lines (117 loc) · 3.31 KB
/
sync-metadata.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package devtool
import (
"encoding/json"
"fmt"
"go/parser"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
"go/token"
"github.com/project-flogo/core/data"
"github.com/spf13/cobra"
)
var syncMetadata = &cobra.Command{
Use: "sync-metadata",
Short: "sync descriptor json and metadata",
Long: `This subcommand creates descriptor json from metadata`,
Run: func(cmd *cobra.Command, args []string) {
pwd, err := os.Getwd()
if err != nil {
fmt.Printf("Error in getting current dir %v\n", err)
os.Exit(1)
}
if _, err = os.Stat(filepath.Join(pwd, "metadata.go")); os.IsNotExist(err) {
fmt.Printf("Error in getting metadata file %v \n", err)
os.Exit(1)
}
err = createDescriptorJSON(filepath.Join(pwd, "metadata.go"))
if err != nil {
fmt.Printf("Error in getting descriptor file %v \n", err)
os.Exit(1)
}
},
}
func init() {
descCmd.AddCommand(syncMetadata)
}
func createDescriptorJSON(src string) error {
var b []byte
pwd, err := os.Getwd()
srcData, err := ioutil.ReadFile(src)
if err != nil {
return err
}
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, "", srcData, 0)
if err != nil {
return err
}
fieldDetailsMap := make(map[string][]*fieldDetails)
for key, _ := range f.Scope.Objects {
fieldDetailsMap[key] = getKeyStructs(string(srcData), key)
}
if _, ok := fieldDetailsMap["Settings"]; ok {
contribStruct := ContribStruct{}
contribStruct.Type, err = getType(pwd)
if err == nil {
contribStruct.Name = path.Base(pwd) + "-" + contribStruct.Type[6:]
}
contribStruct.Settings = fieldDetailsMap["Settings"]
contribStruct.Input = fieldDetailsMap["Input"]
contribStruct.Output = fieldDetailsMap["Output"]
contribStruct.Handler = make(map[string][]*fieldDetails)
contribStruct.Handler["settings"] = fieldDetailsMap["HandlerSettings"]
b, err = json.Marshal(contribStruct)
} else {
operationStruct := OperationStruct{}
operationStruct.Type, err = getType(pwd)
if err == nil {
operationStruct.Name = path.Base(pwd) + "-" + operationStruct.Type[6:]
}
operationStruct.Params = fieldDetailsMap["Params"]
operationStruct.Input = fieldDetailsMap["Input"]
operationStruct.Output = fieldDetailsMap["Output"]
b, err = json.Marshal(operationStruct)
}
err = ioutil.WriteFile("descriptor.json", []byte(jsonPrettyPrint(string(b))), os.ModePerm)
if err != nil {
return err
}
return nil
}
func getKeyStructs(src string, key string) []*fieldDetails {
var result []*fieldDetails
start := strings.Index(src, key+" struct {")
offset := start + len(key) + 9
end := strings.Index(src[offset:], "\n}")
for _, val := range strings.Split(src[offset:offset+end], "\n") {
var temp *fieldDetails
if val != "" {
fields := strings.Fields(val)
temp = getFieldDetailStruct(fields[2])
tempType, _ := data.ToTypeEnum(fields[1])
if tempType.String() == "unknown" {
temp.Type = "any"
} else {
temp.Type = tempType.String()
}
if temp != nil {
result = append(result, temp)
}
}
}
return result
}
func getFieldDetailStruct(md string) *fieldDetails {
components := deconstructTag(md[5:])
temp := &fieldDetails{}
if len(components) > 0 {
temp.Name = strings.TrimFunc(components[0], removeSpecialChars)
for i := 0; i < len(components); i++ {
applyTagComponent(temp, components[i])
}
}
return temp
}