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 #80 from suzuki-shunsuke/fix/convert-meta
Browse files Browse the repository at this point in the history
fix: convert meta's map[interface{}]interface{} to map[string]interface{}
  • Loading branch information
suzuki-shunsuke authored Oct 15, 2020
2 parents d1febd2 + eb22f82 commit 9d8f0fb
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 1 deletion.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,21 @@ ex.

We can test external Tengo scripts with [suzuki-shunsuke/tengo-tester](https://github.com/suzuki-shunsuke/tengo-tester).

## Restriction: map key should be string

* meta
* phase.meta
* task.meta
* task.items

NG

```yaml
---
meta:
true: foo # invalid key: key should be string
```

## Configuration Reference

```yaml
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require (
github.com/sirupsen/logrus v1.7.0
github.com/stretchr/testify v1.6.1
github.com/suzuki-shunsuke/go-ci-env v0.2.1
github.com/suzuki-shunsuke/go-convmap v0.1.0
github.com/suzuki-shunsuke/go-dataeq v1.0.1
github.com/suzuki-shunsuke/go-error-with-exit-code v1.0.0
github.com/suzuki-shunsuke/go-findconfig v0.1.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/suzuki-shunsuke/go-ci-env v0.2.1 h1:BXyeehL7uHtQ8cM+xwLzfzZ8fEhrvcIXJtYe0MEqN0o=
github.com/suzuki-shunsuke/go-ci-env v0.2.1/go.mod h1:kO9UgcQIAH4Pu4ESkUJHPuQEtesxHPKkpQqeJHJzzdk=
github.com/suzuki-shunsuke/go-convmap v0.1.0 h1:0qzSC9IAzYdylNJvgzFpxbD/KeWpuVM608d/S0VjYsU=
github.com/suzuki-shunsuke/go-convmap v0.1.0/go.mod h1:S0yDqBPFU0lmlHhoMKNSSHDRt87tUyI+KY/fBuXNn14=
github.com/suzuki-shunsuke/go-dataeq v1.0.1 h1:ruo7fZ2tT1g5wSWxNTXbDzbdKZRyycnUiAQrhkrOWxw=
github.com/suzuki-shunsuke/go-dataeq v1.0.1/go.mod h1:y9Jf/g370ehd4VBPajzO2Ir8kj+Y4OR+yRu+T5CiK88=
github.com/suzuki-shunsuke/go-error-with-exit-code v1.0.0 h1:oVXrrYNGBq4POyITQNWKzwsYz7B2nUcqtDbeX4BfeEc=
Expand Down
21 changes: 20 additions & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package config

import (
"fmt"
"os"

"github.com/suzuki-shunsuke/buildflow/pkg/expr"
"github.com/suzuki-shunsuke/go-ci-env/cienv"
"github.com/suzuki-shunsuke/go-convmap/convmap"
)

type Phase struct {
Expand Down Expand Up @@ -52,12 +54,29 @@ type Env struct {
CI bool
}

func convertMeta(meta map[string]interface{}) error {
for k, a := range meta {
b, err := convmap.Convert(a)
if err != nil {
return fmt.Errorf("parse meta: %w", err)
}
meta[k] = b
}
return nil
}

func Set(cfg Config) (Config, error) {
cfg = setDefault(setEnv(cfg))
if err := convertMeta(cfg.Meta); err != nil {
return cfg, fmt.Errorf(".meta is invalid: %w", err)
}
for i, phase := range cfg.Phases {
if err := convertMeta(phase.Meta); err != nil {
return cfg, fmt.Errorf("phase is invalid: %s: %w", phase.Name, err)
}
for j, task := range phase.Tasks {
if err := task.Set(); err != nil {
return cfg, err
return cfg, fmt.Errorf("task is invalid: %w", err)
}
phase.Tasks[j] = task
}
Expand Down
11 changes: 11 additions & 0 deletions pkg/config/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/suzuki-shunsuke/buildflow/pkg/constant"
"github.com/suzuki-shunsuke/buildflow/pkg/execute"
"github.com/suzuki-shunsuke/buildflow/pkg/expr"
"github.com/suzuki-shunsuke/go-convmap/convmap"
)

type Task struct {
Expand Down Expand Up @@ -49,6 +50,16 @@ func (task *Task) Set() error {
task.CompiledItems = Items{
Program: prog,
}
} else if task.Items != nil {
a, err := convmap.Convert(task.Items)
if err != nil {
return err
}
task.Items = a
}

if err := convertMeta(task.Meta); err != nil {
return err
}

return nil
Expand Down

0 comments on commit 9d8f0fb

Please sign in to comment.