Skip to content

Commit

Permalink
added config option for ext_code (#83)
Browse files Browse the repository at this point in the history
Co-authored-by: Matthias Wilhelm <matthias.wilhelm@ppi-fs.de>
  • Loading branch information
Puddinghat and MaWiPPI authored Nov 23, 2022
1 parent c02475d commit dd39569
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 2 deletions.
34 changes: 33 additions & 1 deletion pkg/server/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Configuration struct {
ResolvePathsWithTanka bool
JPaths []string
ExtVars map[string]string
ExtCode map[string]string
FormattingOptions formatter.Options

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

case "ext_code":
newCode, err := s.parseExtCode(sv)
if err != nil {
return fmt.Errorf("%w: ext_code parsing failed: %v", jsonrpc2.ErrInvalidParams, err)
}
s.configuration.ExtCode = newCode

default:
return fmt.Errorf("%w: unsupported settings key: %q", jsonrpc2.ErrInvalidParams, sk)
}
Expand Down Expand Up @@ -133,11 +141,35 @@ func (s *Server) parseFormattingOpts(unparsed interface{}) (formatter.Options, e
return opts, nil
}

func resetExtVars(vm *jsonnet.VM, vars map[string]string) {
func (s *Server) parseExtCode(unparsed interface{}) (map[string]string, error) {
newVars, ok := unparsed.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("unsupported settings value for ext_code. expected json object. got: %T", unparsed)
}

vm := s.getVM(".")

extCode := make(map[string]string, len(newVars))
for varKey, varValue := range newVars {
vv, ok := varValue.(string)
if !ok {
return nil, fmt.Errorf("unsupported settings value for ext_code.%s. expected string. got: %T", varKey, varValue)
}
jsonResult, _ := vm.EvaluateAnonymousSnippet("ext-code", vv)
extCode[varKey] = jsonResult
}

return extCode, nil
}

func resetExtVars(vm *jsonnet.VM, vars map[string]string, code map[string]string) {
vm.ExtReset()
for vk, vv := range vars {
vm.ExtVar(vk, vv)
}
for vk, vv := range code {
vm.ExtCode(vk, vv)
}
}

func stringStyleDecodeFunc(from, to reflect.Type, unparsed interface{}) (interface{}, error) {
Expand Down
42 changes: 42 additions & 0 deletions pkg/server/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,42 @@ func TestConfiguration(t *testing.T) {
expectedFileOutput: `
{
"hello": "world"
}
`,
},
{
name: "ext_code config is not an object",
settings: map[string]interface{}{
"ext_code": []string{},
},
fileContent: `[]`,
expectedErr: errors.New("JSON RPC invalid params: ext_code parsing failed: unsupported settings value for ext_code. expected json object. got: []string"),
},
{
name: "ext_code config is empty",
settings: map[string]interface{}{
"ext_code": map[string]interface{}{},
},
fileContent: `[]`,
expectedFileOutput: `[]`,
},
{
name: "ext_code config is valid",
settings: map[string]interface{}{
"ext_code": map[string]interface{}{
"hello": "{\"world\": true,}",
},
},
fileContent: `
{
hello: std.extVar("hello"),
}
`,
expectedFileOutput: `
{
"hello": {
"world": true
}
}
`,
},
Expand Down Expand Up @@ -243,6 +279,9 @@ func TestConfiguration_Formatting(t *testing.T) {
"ext_vars": map[string]interface{}{
"hello": "world",
},
"ext_code": map[string]interface{}{
"hello": "{\"world\": true,}",
},
"resolve_paths_with_tanka": false,
"jpath": []interface{}{"blabla", "blabla2"},
"enable_eval_diagnostics": false,
Expand All @@ -268,6 +307,9 @@ func TestConfiguration_Formatting(t *testing.T) {
ExtVars: map[string]string{
"hello": "world",
},
ExtCode: map[string]string{
"hello": "{\n \"world\": true\n}\n",
},
ResolvePathsWithTanka: false,
JPaths: []string{"blabla", "blabla2"},
EnableEvalDiagnostics: false,
Expand Down
2 changes: 1 addition & 1 deletion pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (s *Server) getVM(path string) *jsonnet.VM {
vm.Importer(importer)
}

resetExtVars(vm, s.configuration.ExtVars)
resetExtVars(vm, s.configuration.ExtVars, s.configuration.ExtCode)
return vm
}

Expand Down

0 comments on commit dd39569

Please sign in to comment.