Skip to content
This repository has been archived by the owner on Sep 29, 2021. It is now read-only.

Commit

Permalink
Merge pull request #7 from suzuki-shunsuke/fix/convert-and-validate-m…
Browse files Browse the repository at this point in the history
…ap-key

fix: convert and validate map key
  • Loading branch information
suzuki-shunsuke authored Oct 12, 2020
2 parents b087b86 + e47bc4a commit 4abf65a
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,36 @@ If the confgiuration file path isn't specified, the file named `.tengo-tester.ym
There are some configuration which are file paths.
If a file path is a relative path, the base directory of the relative path is the directory where the configuration file exists.

## The type of map key should be string in entry.params and test.equal

`entry.params` and `test.equal` is a variable of a Tengo script.
The type of map key of Tengo script should be string.

https://github.com/d5/tengo/blob/master/docs/tutorial.md#values-and-value-types

So the test fails if the map key whose type isn't string is found.

ex.

```yaml
---
entries:
- name: main
script_file: foo.tengo
params:
foo:
true: 1 # the key is boolean
tests:
- name: test
var_name: result
equal: hello
```

```
$ tengo-tester run
FATA[0000] format config: convert entry.params: entry_name: main: key: foo: the map key should be string : true
```
## LICENSE
[MIT](LICENSE)
56 changes: 56 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package config

import (
"fmt"
)

type Config struct {
Entries []Entry
LogLevel string `yaml:"log_level"`
Expand All @@ -21,3 +25,55 @@ type Test struct {
IsNil bool `yaml:"is_nil"`
IsNotNil bool `yaml:"is_not_nil"`
}

func ConvertConfig(cfg Config) error {
for i, entry := range cfg.Entries {
for k, v := range entry.Params {
p, err := ConvertMapKey(v)
if err != nil {
return fmt.Errorf("convert entry.params: entry_name: %s: key: %s: %w", entry.Name, k, err)
}
entry.Params[k] = p
}
for j, test := range entry.Tests {
p, err := ConvertMapKey(test.Equal)
if err != nil {
return fmt.Errorf("convert test.Equal: entry_name: %s: test_name: %s: %w", entry.Name, test.Name, err)
}
test.Equal = p
entry.Tests[j] = test
}
cfg.Entries[i] = entry
}
return nil
}

func ConvertMapKey(data interface{}) (interface{}, error) {
switch t := data.(type) {
case map[interface{}]interface{}:
m := make(map[string]interface{}, len(t))
for k, v := range t {
s, ok := k.(string)
if !ok {
return nil, fmt.Errorf("the map key should be string: %+v", k)
}
val, err := ConvertMapKey(v)
if err != nil {
return nil, fmt.Errorf("key: %s: %w", s, err)
}
m[s] = val
}
return m, nil
case []interface{}:
for i, v := range t {
val, err := ConvertMapKey(v)
if err != nil {
return nil, fmt.Errorf("index: %d: %w", i, err)
}
t[i] = val
}
return t, nil
default:
return data, nil
}
}
3 changes: 3 additions & 0 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ func (ctrl Controller) Run(ctx context.Context, wd string) error {
if err != nil {
return err
}
if err := config.ConvertConfig(ctrl.Config); err != nil {
return fmt.Errorf("format config: %w", err)
}
for _, entry := range ctrl.Config.Entries {
err := ctrl.testEntry(wd, entry, testErrorTpl)
if err != nil {
Expand Down

0 comments on commit 4abf65a

Please sign in to comment.