Repository navigation
Expand file tree
/
Copy pathspec_derive.go
More file actions
128 lines (116 loc) · 3.65 KB
/
Copy pathspec_derive.go
File metadata and controls
128 lines (116 loc) · 3.65 KB
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
package pluginsdk
import (
"fmt"
"strings"
)
// SpecOption transforms a config spec. Build them with Hide and Override
// and pass them to DeriveSpec.
type SpecOption func(spec []ConfigField) []ConfigField
// FieldOverride mutates a single ConfigField in place. Build them with
// Placeholder, Default, Required or Description and pass them to
// Override.
type FieldOverride func(field *ConfigField)
// DeriveSpec builds a config spec from a plugin config struct and
// applies the given options in order. It is the standard way to build
// an alias plugin's spec, e.g. a Confluent Cloud plugin that reuses
// the Kafka config struct but hides TLS and pins the SASL mechanism:
//
// spec := pluginsdk.DeriveSpec(kafka.Config{},
// pluginsdk.Hide("tls", "authentication.type"),
// pluginsdk.Override("bootstrap_servers",
// pluginsdk.Placeholder("pkc-xxxxx.confluent.cloud:9092"),
// ),
// )
//
// DeriveSpec panics if any option references a field that does not
// exist in the generated spec. Alias plugins configure this at startup,
// so a bad dotted path is programmer error and should surface loudly
// rather than silently drop the override.
func DeriveSpec(config interface{}, opts ...SpecOption) []ConfigField {
spec := GenerateConfigSpec(config)
for _, opt := range opts {
spec = opt(spec)
}
return spec
}
// Hide removes fields from the spec. Nested fields use dot notation
// (e.g. "authentication.type"). Hiding a field that does not exist
// panics.
func Hide(names ...string) SpecOption {
return func(spec []ConfigField) []ConfigField {
for _, name := range names {
if !fieldExists(spec, name, "") {
panic(fmt.Sprintf("pluginsdk.Hide: field %q does not exist in spec", name))
}
}
return removeFields(spec, names)
}
}
// Override applies one or more FieldOverrides to a single field.
// Nested fields use dot notation (e.g. "authentication.type").
// Overriding a field that does not exist panics.
func Override(name string, overrides ...FieldOverride) SpecOption {
return func(spec []ConfigField) []ConfigField {
if !applyToField(spec, name, "", overrides) {
panic(fmt.Sprintf("pluginsdk.Override: field %q does not exist in spec", name))
}
return spec
}
}
// Placeholder sets the placeholder text shown in the UI form.
func Placeholder(text string) FieldOverride {
return func(f *ConfigField) { f.Placeholder = text }
}
func removeFields(spec []ConfigField, names []string) []ConfigField {
topLevel := make(map[string]bool)
nested := make(map[string][]string)
for _, name := range names {
if idx := strings.IndexByte(name, '.'); idx != -1 {
parent := name[:idx]
child := name[idx+1:]
nested[parent] = append(nested[parent], child)
} else {
topLevel[name] = true
}
}
result := make([]ConfigField, 0, len(spec))
for _, f := range spec {
if topLevel[f.Name] {
continue
}
if children, ok := nested[f.Name]; ok && len(f.Fields) > 0 {
f.Fields = removeFields(f.Fields, children)
}
result = append(result, f)
}
return result
}
func applyToField(spec []ConfigField, path, prefix string, overrides []FieldOverride) bool {
for i := range spec {
key := prefix + spec[i].Name
if key == path {
for _, o := range overrides {
o(&spec[i])
}
return true
}
if len(spec[i].Fields) > 0 && strings.HasPrefix(path, key+".") {
if applyToField(spec[i].Fields, path, key+".", overrides) {
return true
}
}
}
return false
}
func fieldExists(spec []ConfigField, path, prefix string) bool {
for _, f := range spec {
key := prefix + f.Name
if key == path {
return true
}
if strings.HasPrefix(path, key+".") && fieldExists(f.Fields, path, key+".") {
return true
}
}
return false
}