Skip to content

Commit

Permalink
Always extract source information and check for undefined blocks/attrs
Browse files Browse the repository at this point in the history
  • Loading branch information
qbart committed Oct 4, 2022
1 parent 879e8e4 commit 737c31b
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 6 deletions.
12 changes: 6 additions & 6 deletions krab/type_ddl_create_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ type DDLCreateTable struct {
krabhcl.Source

Name string
Unlogged bool `hcl:"unlogged,optional"`
Columns []*DDLColumn `hcl:"column,block"`
PrimaryKeys []*DDLPrimaryKey `hcl:"primary_key,block"`
ForeignKeys []*DDLForeignKey `hcl:"foreign_key,block"`
Uniques []*DDLUnique `hcl:"unique,block"`
Checks []*DDLCheck `hcl:"check,block"`
Unlogged bool
Columns []*DDLColumn
PrimaryKeys []*DDLPrimaryKey
ForeignKeys []*DDLForeignKey
Uniques []*DDLUnique
Checks []*DDLCheck
}

var schemaCreateTable = &hcl.BodySchema{
Expand Down
8 changes: 8 additions & 0 deletions krab/type_ddl_drop_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ func (d *DDLDropIndex) DecodeHCL(ctx *hcl.EvalContext, block *hcl.Block) error {
return fmt.Errorf("failed to decode `%s` block: %s", block.Type, 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 "concurrently":
Expand Down
4 changes: 4 additions & 0 deletions krab/type_ddl_generated_column.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (

// DDLGeneratedColumn DSL.
type DDLGeneratedColumn struct {
krabhcl.Source

As string
}

Expand All @@ -22,6 +24,8 @@ var schemaGeneratedColumn = &hcl.BodySchema{

// DecodeHCL parses HCL into struct.
func (d *DDLGeneratedColumn) DecodeHCL(ctx *hcl.EvalContext, block *hcl.Block) error {
d.Source.Extract(block)

content, diags := block.Body.Content(schemaGeneratedColumn)
if diags.HasErrors() {
return fmt.Errorf("failed to decode `%s` block: %s", block.Type, diags.Error())
Expand Down
4 changes: 4 additions & 0 deletions krab/type_ddl_identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import (
"io"

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

// DDLIdentity DSL.
type DDLIdentity struct {
krabhcl.Source
}

var schemaIdentity = &hcl.BodySchema{
Expand All @@ -18,6 +20,8 @@ var schemaIdentity = &hcl.BodySchema{

// DecodeHCL parses HCL into struct.
func (d *DDLIdentity) DecodeHCL(ctx *hcl.EvalContext, block *hcl.Block) error {
d.Source.Extract(block)

content, diags := block.Body.Content(schemaColumn)
if diags.HasErrors() {
return fmt.Errorf("failed to decode `%s` block: %s", block.Type, diags.Error())
Expand Down
4 changes: 4 additions & 0 deletions krab/type_ddl_primary_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (

// DDLPrimaryKey constraint DSL for table DDL.
type DDLPrimaryKey struct {
krabhcl.Source

Columns []string
Include []string
}
Expand All @@ -26,6 +28,8 @@ var schemaPrimaryKey = &hcl.BodySchema{

// DecodeHCL parses HCL into struct.
func (d *DDLPrimaryKey) DecodeHCL(ctx *hcl.EvalContext, block *hcl.Block) error {
d.Source.Extract(block)

d.Columns = []string{}
d.Include = []string{}

Expand Down
4 changes: 4 additions & 0 deletions krab/type_ddl_references.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (

// DDLReferences DSL for ForeignKey.
type DDLReferences struct {
krabhcl.Source

Table string
Columns []string
OnDelete string
Expand All @@ -29,6 +31,8 @@ var schemaForeignKeyReferences = &hcl.BodySchema{

// DecodeHCL parses HCL into struct.
func (d *DDLReferences) DecodeHCL(ctx *hcl.EvalContext, block *hcl.Block) error {
d.Source.Extract(block)

d.Columns = []string{}
d.Table = block.Labels[0]

Expand Down
4 changes: 4 additions & 0 deletions krab/type_ddl_unique.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (

// DDLUnique constraint DSL for table DDL.
type DDLUnique struct {
krabhcl.Source

Columns []string
Include []string
}
Expand All @@ -26,6 +28,8 @@ var schemaUnique = &hcl.BodySchema{

// DecodeHCL parses HCL into struct.
func (d *DDLUnique) DecodeHCL(ctx *hcl.EvalContext, block *hcl.Block) error {
d.Source.Extract(block)

d.Columns = []string{}
d.Include = []string{}

Expand Down

0 comments on commit 737c31b

Please sign in to comment.