-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
244 additions
and
212 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.