/
spec.go
163 lines (144 loc) Β· 3.49 KB
/
spec.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
package main
import (
"fmt"
"os"
"strings"
)
type SourceSpec struct {
Name string
Registry string
Path string
Version string
Destinations []string
Spec PluginSpec
}
type PluginSpec struct {
Auth AuthSpec
Lists []ListConf
ContentTypes []ContentTypeConf
MMD []MMDConf
Profiles bool
Search []SearchConf
}
type AuthSpec struct {
Strategy string
Creds [][]string
}
func (s *SourceSpec) Marshal() []byte {
spec := `
kind: source
spec:
name: ` + s.Name + `
registry: ` + s.Registry + `
path: ` + s.Path + `
version: ` + s.Version + `
destinations: ["` + strings.Join(s.Destinations, `", "`) + `"]
tables: ["*"]
spec:
`
if len(s.Spec.Auth.Creds) > 0 {
spec += marshalAuth(s.Spec.Auth)
}
if len(s.Spec.Lists) > 0 {
spec += marshalLists(s.Spec.Lists)
}
if len(s.Spec.ContentTypes) > 0 {
spec += marshalContentTypes(s.Spec.ContentTypes)
}
if len(s.Spec.MMD) > 0 {
spec += marshalMMD(s.Spec.MMD)
}
if s.Spec.Profiles {
spec += marshalProfiles()
}
if len(s.Spec.Search) > 0 {
spec += marshalSearch(s.Spec.Search)
}
return []byte(strings.TrimSpace(spec))
}
func (s *SourceSpec) Save(filename string) error {
return os.WriteFile(filename, s.Marshal(), 0644)
}
func marshalAuth(authSpec AuthSpec) string {
res := " auth:\n"
res += " strategy: " + authSpec.Strategy + "\n"
res += " creds:\n"
for _, c := range authSpec.Creds {
res += " " + c[0] + ": " + c[1] + "\n"
}
return res
}
func marshalLists(listsSpec []ListConf) string {
res := " lists:\n"
for _, list := range listsSpec {
res += " " + list.ID + ":\n"
res += " select:\n"
for _, field := range list.Spec.Select {
res += " - " + field + "\n"
}
if len(list.Spec.Expand) > 0 {
res += " expand:\n"
for _, field := range list.Spec.Expand {
res += " - " + field + "\n"
}
}
if len(list.Spec.Filter) > 0 {
res += " filter: \"" + list.Spec.Filter + "\"\n"
}
if len(list.Spec.Alias) > 0 {
res += " alias: \"" + list.Spec.Alias + "\"\n"
}
}
return res
}
func marshalContentTypes(ctSpec []ContentTypeConf) string {
res := " content_types:\n"
for _, ct := range ctSpec {
res += " " + ct.ID + ":\n"
res += " select:\n"
for _, field := range ct.Spec.Select {
res += " - " + field + "\n"
}
if len(ct.Spec.Expand) > 0 {
res += " expand:\n"
for _, field := range ct.Spec.Expand {
res += " - " + field + "\n"
}
}
if len(ct.Spec.Alias) > 0 {
res += " alias: \"" + ct.Spec.Alias + "\"\n"
}
}
return res
}
func marshalMMD(mmdSpec []MMDConf) string {
res := " mmd:\n"
for _, mmd := range mmdSpec {
res += " " + mmd.ID + ":\n"
res += " alias: \"" + mmd.Spec.Alias + "\"\n"
}
return res
}
func marshalProfiles() string {
res := " profiles:\n"
res += " enabled: true\n"
return res
}
func marshalSearch(searchSpec []SearchConf) string {
res := " search:\n"
for _, search := range searchSpec {
res += " " + search.ID + ":\n"
res += " query_text: \"" + search.QueryText + "\"\n"
if len(search.SourceID) > 0 {
res += " source_id: \"" + search.SourceID + "\"\n"
}
res += fmt.Sprintf(" trim_duplicates: %t\n", search.TrimDuplicates)
if len(search.SelectProperties) > 0 {
res += " select_properties:\n"
for _, prop := range search.SelectProperties {
res += " - " + prop + "\n"
}
}
}
return res
}