Skip to content

Commit

Permalink
Parse action arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
qbart committed Oct 10, 2021
1 parent 08cd0ae commit 666b7bb
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 0 deletions.
5 changes: 5 additions & 0 deletions krab/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ func NewConfig(files []*File) (*Config, error) {
}
}

// defaults
for _, defaultable := range c.MigrationSets {
defaultable.InitDefaults()
}

// validate
for _, validatable := range c.MigrationSets {
if err := validatable.Validate(); err != nil {
Expand Down
5 changes: 5 additions & 0 deletions krab/defaultable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package krab

type Defaultable interface {
InitDefaults()
}
46 changes: 46 additions & 0 deletions krab/parser_arguments_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package krab

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestParserArguments(t *testing.T) {
assert := assert.New(t)

p := mockParser(
"src/public.krab.hcl",
`
migration_set "public" {
arguments {
arg "str" {
type = "string"
}
arg "num" {
type = "int"
}
arg "none" {}
}
migrations = []
}
`)
c, err := p.LoadConfigDir("src")
assert.NoError(err, "Arguments should be parsed")

set := c.MigrationSets["public"]
assert.NotNil(set)
assert.Equal(3, len(set.Arguments.Args))

assert.Equal("str", set.Arguments.Args[0].Name)
assert.Equal("string", set.Arguments.Args[0].Type)

assert.Equal("num", set.Arguments.Args[1].Name)
assert.Equal("int", set.Arguments.Args[1].Type)

assert.Equal("none", set.Arguments.Args[2].Name)
assert.Equal("string", set.Arguments.Args[2].Type)
}
38 changes: 38 additions & 0 deletions krab/type_arguments.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package krab

type Argument struct {
Name string `hcl:"name,label"`
Type string `hcl:"type,optional"`
}

// Arguments represents command line arguments or params that you can pass to action.
//
type Arguments struct {
Args []*Argument `hcl:"arg,block"`
}

func (a *Arguments) Validate() error {
for _, a := range a.Args {
if err := a.Validate(); err != nil {
return err
}
}

return nil
}

func (a *Arguments) InitDefaults() {
for _, a := range a.Args {
a.InitDefaults()
}
}

func (a *Argument) Validate() error {
return nil
}

func (a *Argument) InitDefaults() {
if a.Type == "" {
a.Type = "string"
}
}
8 changes: 8 additions & 0 deletions krab/type_migration_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,19 @@ import (
type MigrationSet struct {
RefName string `hcl:"ref_name,label"`

Arguments *Arguments `hcl:"arguments,block"`

// SchemaMigrationsTable string `hcl:"schema_migrations_table"`
MigrationsExpr hcl.Expression `hcl:"migrations"`
Migrations []*Migration // populated from refs in expression
}

func (ms *MigrationSet) InitDefaults() {
if ms.Arguments != nil {
ms.Arguments.InitDefaults()
}
}

func (ms *MigrationSet) Validate() error {
return ErrorCoalesce(
ValidateRefName(ms.RefName),
Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/simple/migrations.krab.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,14 @@ migration "add_users" {
}

migration_set "tenant" {
arguments {
arg "schema" {
type = "string"
}
}

migrations = [
migration.create_assets
]
}

Expand Down

0 comments on commit 666b7bb

Please sign in to comment.