Debian Codegraph @

pkg/tools/xmlgen/gen_model_build.go

raw symbols SCIP
1 package xmlgen
2
3 import (
4 "fmt"
5 "reflect"
6 )
7
8 func BuildModel(in *DocModel, out *Model) {
9 for _, v := range in.Agi {
10 out.AGICommands = append(out.AGICommands, in.AGIModel(v))
11 }
12 for _, v := range in.Manager {
13 out.Actions = append(out.Actions, in.ManagerModel(v))
14 }
15 for _, v := range in.ManagerEvent {
16 out.Events = append(out.Events, in.ManagerEventModel(v))
17 }
18 //
19 Dedup(&out.Actions, func(i int) string {
20 return out.Actions[i].Name
21 })
22 Dedup(&out.Events, func(i int) string {
23 return out.Events[i].Name
24 })
25 for _, v := range out.Events {
26 Dedup(&v.Syntax.Params, func(i int) string {
27 return v.Syntax.Params[i].Name
28 })
29 }
30
31 // add missing event, unify event
32 events := map[string]*ManagerEvent{}
33 for _, v := range out.Events {
34 events[v.Name] = v
35 }
36 for _, action := range out.Actions {
37 for k, v := range action.Responses {
38 if e, ok := events[v.Name]; ok {
39 action.Responses[k] = e
40 } else {
41 out.Events = append(out.Events, v)
42 fmt.Println("Append Response Event", v.Name)
43 }
44 }
45 }
46
47 // add missing ActionID
48 for _, v := range out.Actions {
49 found := false
50 for _, v := range v.Syntax.Params {
51 if found = v.Name == "ActionID"; found {
52 break
53 }
54 }
55 if !found {
56 o := []*Parameter{{
57 Name: "ActionID",
58 Type: "string",
59 Description: "ActionID for this transaction. Will be returned.",
60 }}
61 v.Syntax.Params = append(o, v.Syntax.Params...)
62 }
63 }
64 }
65
66 func validGoTypeName(s string) string {
67 s = invalid.ReplaceAllLiteralString(s, "")
68 return s
69 }
70 func Dedup(arr interface{}, cond func(int) string) {
71 m := map[string]bool{}
72 Filter(arr, func(i int) bool {
73 k := cond(i)
74 if _, ok := m[k]; ok {
75 return false
76 }
77 m[k] = true
78 return true
79 })
80 }
81
82 func Filter(arr interface{}, cond func(int) bool) {
83 rv := reflect.ValueOf(arr).Elem()
84 rt := rv.Type()
85
86 neo := reflect.MakeSlice(rt, 0, 0)
87 for i := 0; i < rv.Len(); i++ {
88 if v := rv.Index(i); cond(i) {
89 neo = reflect.Append(neo, v)
90 }
91 }
92 rv.Set(neo)
93 }