Skip to content

Commit dd39569

Browse files
PuddinghatMaWiPPI
andauthored
added config option for ext_code (#83)
Co-authored-by: Matthias Wilhelm <[email protected]>
1 parent c02475d commit dd39569

File tree

3 files changed

+76
-2
lines changed

3 files changed

+76
-2
lines changed

pkg/server/configuration.go

+33-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ type Configuration struct {
1717
ResolvePathsWithTanka bool
1818
JPaths []string
1919
ExtVars map[string]string
20+
ExtCode map[string]string
2021
FormattingOptions formatter.Options
2122

2223
EnableEvalDiagnostics bool
@@ -82,6 +83,13 @@ func (s *Server) DidChangeConfiguration(ctx context.Context, params *protocol.Di
8283
}
8384
s.configuration.FormattingOptions = newFmtOpts
8485

86+
case "ext_code":
87+
newCode, err := s.parseExtCode(sv)
88+
if err != nil {
89+
return fmt.Errorf("%w: ext_code parsing failed: %v", jsonrpc2.ErrInvalidParams, err)
90+
}
91+
s.configuration.ExtCode = newCode
92+
8593
default:
8694
return fmt.Errorf("%w: unsupported settings key: %q", jsonrpc2.ErrInvalidParams, sk)
8795
}
@@ -133,11 +141,35 @@ func (s *Server) parseFormattingOpts(unparsed interface{}) (formatter.Options, e
133141
return opts, nil
134142
}
135143

136-
func resetExtVars(vm *jsonnet.VM, vars map[string]string) {
144+
func (s *Server) parseExtCode(unparsed interface{}) (map[string]string, error) {
145+
newVars, ok := unparsed.(map[string]interface{})
146+
if !ok {
147+
return nil, fmt.Errorf("unsupported settings value for ext_code. expected json object. got: %T", unparsed)
148+
}
149+
150+
vm := s.getVM(".")
151+
152+
extCode := make(map[string]string, len(newVars))
153+
for varKey, varValue := range newVars {
154+
vv, ok := varValue.(string)
155+
if !ok {
156+
return nil, fmt.Errorf("unsupported settings value for ext_code.%s. expected string. got: %T", varKey, varValue)
157+
}
158+
jsonResult, _ := vm.EvaluateAnonymousSnippet("ext-code", vv)
159+
extCode[varKey] = jsonResult
160+
}
161+
162+
return extCode, nil
163+
}
164+
165+
func resetExtVars(vm *jsonnet.VM, vars map[string]string, code map[string]string) {
137166
vm.ExtReset()
138167
for vk, vv := range vars {
139168
vm.ExtVar(vk, vv)
140169
}
170+
for vk, vv := range code {
171+
vm.ExtCode(vk, vv)
172+
}
141173
}
142174

143175
func stringStyleDecodeFunc(from, to reflect.Type, unparsed interface{}) (interface{}, error) {

pkg/server/configuration_test.go

+42
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,42 @@ func TestConfiguration(t *testing.T) {
8282
expectedFileOutput: `
8383
{
8484
"hello": "world"
85+
}
86+
`,
87+
},
88+
{
89+
name: "ext_code config is not an object",
90+
settings: map[string]interface{}{
91+
"ext_code": []string{},
92+
},
93+
fileContent: `[]`,
94+
expectedErr: errors.New("JSON RPC invalid params: ext_code parsing failed: unsupported settings value for ext_code. expected json object. got: []string"),
95+
},
96+
{
97+
name: "ext_code config is empty",
98+
settings: map[string]interface{}{
99+
"ext_code": map[string]interface{}{},
100+
},
101+
fileContent: `[]`,
102+
expectedFileOutput: `[]`,
103+
},
104+
{
105+
name: "ext_code config is valid",
106+
settings: map[string]interface{}{
107+
"ext_code": map[string]interface{}{
108+
"hello": "{\"world\": true,}",
109+
},
110+
},
111+
fileContent: `
112+
{
113+
hello: std.extVar("hello"),
114+
}
115+
`,
116+
expectedFileOutput: `
117+
{
118+
"hello": {
119+
"world": true
120+
}
85121
}
86122
`,
87123
},
@@ -243,6 +279,9 @@ func TestConfiguration_Formatting(t *testing.T) {
243279
"ext_vars": map[string]interface{}{
244280
"hello": "world",
245281
},
282+
"ext_code": map[string]interface{}{
283+
"hello": "{\"world\": true,}",
284+
},
246285
"resolve_paths_with_tanka": false,
247286
"jpath": []interface{}{"blabla", "blabla2"},
248287
"enable_eval_diagnostics": false,
@@ -268,6 +307,9 @@ func TestConfiguration_Formatting(t *testing.T) {
268307
ExtVars: map[string]string{
269308
"hello": "world",
270309
},
310+
ExtCode: map[string]string{
311+
"hello": "{\n \"world\": true\n}\n",
312+
},
271313
ResolvePathsWithTanka: false,
272314
JPaths: []string{"blabla", "blabla2"},
273315
EnableEvalDiagnostics: false,

pkg/server/server.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func (s *Server) getVM(path string) *jsonnet.VM {
6565
vm.Importer(importer)
6666
}
6767

68-
resetExtVars(vm, s.configuration.ExtVars)
68+
resetExtVars(vm, s.configuration.ExtVars, s.configuration.ExtCode)
6969
return vm
7070
}
7171

0 commit comments

Comments
 (0)