Skip to content

Commit

Permalink
Extract subtypes to separate files
Browse files Browse the repository at this point in the history
  • Loading branch information
qbart committed Mar 5, 2023
1 parent 053c3fe commit 0274639
Show file tree
Hide file tree
Showing 5 changed files with 244 additions and 212 deletions.
212 changes: 0 additions & 212 deletions krab/type_test_example.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,215 +81,3 @@ func (t *TestExample) DecodeHCL(ctx *hcl.EvalContext, block *hcl.Block) error {

return nil
}

// TestExampleIt represents one use case for test example that contain queries and assertions.
type TestExampleIt struct {
krabhcl.Source

Name string
Queries []*TestQuery
}

var schemaTestExampleIt = &hcl.BodySchema{
Blocks: []hcl.BlockHeaderSchema{
{
Type: "query",
LabelNames: []string{"sql"},
},
},
Attributes: []hcl.AttributeSchema{},
}

func (it *TestExampleIt) DecodeHCL(ctx *hcl.EvalContext, block *hcl.Block) error {
it.Source.Extract(block)

it.Name = block.Labels[0]

content, diags := block.Body.Content(schemaTestExampleIt)
if diags.HasErrors() {
return fmt.Errorf("failed to decode `test` block: %s", diags.Error())
}

for _, b := range content.Blocks {
switch b.Type {
case "query":
q := new(TestQuery)
err := q.DecodeHCL(ctx, b)
if err != nil {
return err
}
it.Queries = append(it.Queries, q)

default:
return fmt.Errorf("Unknown block `%s` for `%s` block", b.Type, block.Type)
}
}

for k, _ := range content.Attributes {
switch k {

default:
return fmt.Errorf("Unknown attribute `%s` for `migration` block", k)
}
}

return nil
}

type TestQuery struct {
krabhcl.Source

Query string
Rows []*TestQueryRow
}

var schemaTestQuery = &hcl.BodySchema{
Blocks: []hcl.BlockHeaderSchema{
{
Type: "row",
LabelNames: []string{"scope"},
},
},
Attributes: []hcl.AttributeSchema{},
}

func (q *TestQuery) DecodeHCL(ctx *hcl.EvalContext, block *hcl.Block) error {
q.Source.Extract(block)
q.Query = block.Labels[0]

content, diags := block.Body.Content(schemaTestQuery)
if diags.HasErrors() {
return fmt.Errorf("failed to decode `test` block: %s", diags.Error())
}

for _, b := range content.Blocks {
switch b.Type {
case "row":
row := new(TestQueryRow)
err := row.DecodeHCL(ctx, b)
if err != nil {
return err
}
q.Rows = append(q.Rows, row)

default:
return fmt.Errorf("Unknown block `%s` for `%s` block", b.Type, block.Type)
}
}

for k, _ := range content.Attributes {
switch k {

default:
return fmt.Errorf("Unknown attribute `%s` for `migration` block", k)
}
}

return nil
}

type TestQueryRow struct {
krabhcl.Source

Scope string
Cols []*TestQueryCol
}

var schemaTestQueryRow = &hcl.BodySchema{
Blocks: []hcl.BlockHeaderSchema{
{
Type: "col",
LabelNames: []string{"message"},
},
},
Attributes: []hcl.AttributeSchema{},
}

func (row *TestQueryRow) DecodeHCL(ctx *hcl.EvalContext, block *hcl.Block) error {
row.Source.Extract(block)
row.Scope = block.Labels[0]

content, diags := block.Body.Content(schemaTestQueryRow)
if diags.HasErrors() {
return fmt.Errorf("failed to decode `test` block: %s", diags.Error())
}

for _, b := range content.Blocks {
switch b.Type {
case "col":
col := new(TestQueryCol)
err := col.DecodeHCL(ctx, b)
if err != nil {
return err
}
row.Cols = append(row.Cols, col)

default:
return fmt.Errorf("Unknown block `%s` for `%s` block", b.Type, block.Type)
}
}

for k, _ := range content.Attributes {
switch k {

default:
return fmt.Errorf("Unknown attribute `%s` for `migration` block", k)
}
}

return nil
}

type TestQueryCol struct {
krabhcl.Source

Message string
Assert string
}

var schemaTestQueryCol = &hcl.BodySchema{
Blocks: []hcl.BlockHeaderSchema{
},
Attributes: []hcl.AttributeSchema{
{
Name: "assert",
Required: true,
},
},
}

func (col *TestQueryCol) DecodeHCL(ctx *hcl.EvalContext, block *hcl.Block) error {
col.Source.Extract(block)
col.Message = block.Labels[0]

content, diags := block.Body.Content(schemaTestQueryCol)
if diags.HasErrors() {
return fmt.Errorf("failed to decode `test` block: %s", diags.Error())
}

for _, b := range content.Blocks {
switch b.Type {

default:
return fmt.Errorf("Unknown block `%s` for `%s` block", b.Type, block.Type)
}
}

for k, v := range content.Attributes {
switch k {

case "assert":
expr := krabhcl.Expression{Expr: v.Expr, EvalContext: ctx}
val, err := expr.String()
if err != nil {
return err
}
col.Assert = val

default:
return fmt.Errorf("Unknown attribute `%s` for `migration` block", k)
}
}

return nil
}
62 changes: 62 additions & 0 deletions krab/type_test_example_it.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package krab

import (
"fmt"

"github.com/hashicorp/hcl/v2"
"github.com/ohkrab/krab/krabhcl"
)

// TestExampleIt represents one use case for test example that contain queries and assertions.
type TestExampleIt struct {
krabhcl.Source

Name string
Queries []*TestQuery
}

var schemaTestExampleIt = &hcl.BodySchema{
Blocks: []hcl.BlockHeaderSchema{
{
Type: "query",
LabelNames: []string{"sql"},
},
},
Attributes: []hcl.AttributeSchema{},
}

func (it *TestExampleIt) DecodeHCL(ctx *hcl.EvalContext, block *hcl.Block) error {
it.Source.Extract(block)

it.Name = block.Labels[0]

content, diags := block.Body.Content(schemaTestExampleIt)
if diags.HasErrors() {
return fmt.Errorf("failed to decode `test` block: %s", diags.Error())
}

for _, b := range content.Blocks {
switch b.Type {
case "query":
q := new(TestQuery)
err := q.DecodeHCL(ctx, b)
if err != nil {
return err
}
it.Queries = append(it.Queries, q)

default:
return fmt.Errorf("Unknown block `%s` for `%s` block", b.Type, block.Type)
}
}

for k := range content.Attributes {
switch k {

default:
return fmt.Errorf("Unknown attribute `%s` for `migration` block", k)
}
}

return nil
}
60 changes: 60 additions & 0 deletions krab/type_test_query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package krab

import (
"fmt"

"github.com/hashicorp/hcl/v2"
"github.com/ohkrab/krab/krabhcl"
)

type TestQuery struct {
krabhcl.Source

Query string
Rows []*TestQueryRow
}

var schemaTestQuery = &hcl.BodySchema{
Blocks: []hcl.BlockHeaderSchema{
{
Type: "row",
LabelNames: []string{"scope"},
},
},
Attributes: []hcl.AttributeSchema{},
}

func (q *TestQuery) DecodeHCL(ctx *hcl.EvalContext, block *hcl.Block) error {
q.Source.Extract(block)
q.Query = block.Labels[0]

content, diags := block.Body.Content(schemaTestQuery)
if diags.HasErrors() {
return fmt.Errorf("failed to decode `test` block: %s", diags.Error())
}

for _, b := range content.Blocks {
switch b.Type {
case "row":
row := new(TestQueryRow)
err := row.DecodeHCL(ctx, b)
if err != nil {
return err
}
q.Rows = append(q.Rows, row)

default:
return fmt.Errorf("Unknown block `%s` for `%s` block", b.Type, block.Type)
}
}

for k, _ := range content.Attributes {
switch k {

default:
return fmt.Errorf("Unknown attribute `%s` for `migration` block", k)
}
}

return nil
}
Loading

0 comments on commit 0274639

Please sign in to comment.