Skip to content

Commit

Permalink
abi2: bugfix. validating required block fields
Browse files Browse the repository at this point in the history
  • Loading branch information
ryandotsmith committed Nov 2, 2023
1 parent f2fc9ae commit 5d5d845
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
15 changes: 11 additions & 4 deletions abi2/abi2.go
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,7 @@ func New(name string, ev Event, bd []BlockData, table Table) (Integration, error
return ig, fmt.Errorf("validating %s: %w", name, err)
}
for _, input := range ev.Selected() {
c, err := col(table, input.Column)
c, err := col(ig.Table, input.Column)
if err != nil {
return ig, err
}
Expand All @@ -614,8 +614,8 @@ func New(name string, ev Event, bd []BlockData, table Table) (Integration, error
})
ig.numSelected++
}
for _, data := range bd {
c, err := col(table, data.Column)
for _, data := range ig.Block {
c, err := col(ig.Table, data.Column)
if err != nil {
return ig, err
}
Expand Down Expand Up @@ -677,6 +677,7 @@ func (ig *Integration) validateCols() error {
}
ucols = map[string]struct{}{}
uinputs = map[string]struct{}{}
ubd = map[string]struct{}{}
)
for _, c := range ig.Table.Cols {
if _, ok := ucols[c.Name]; ok {
Expand All @@ -690,14 +691,20 @@ func (ig *Integration) validateCols() error {
}
uinputs[inp.Name] = struct{}{}
}
for _, bd := range ig.Block {
if _, ok := ubd[bd.Name]; ok {
return fmt.Errorf("duplicate block data field: %s", bd.Name)
}
ubd[bd.Name] = struct{}{}
}
for name, cfg := range required {
switch {
case len(cfg.i.Name) > 0:
if _, ok := uinputs[name]; !ok {
ig.Event.Inputs = append(ig.Event.Inputs, cfg.i)
}
case len(cfg.b.Name) > 0:
if _, ok := uinputs[name]; !ok {
if _, ok := ubd[name]; !ok {
ig.Block = append(ig.Block, cfg.b)
}
}
Expand Down
27 changes: 27 additions & 0 deletions abi2/abi2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,33 @@ func TestNumIndexed(t *testing.T) {
diff.Test(t, t.Errorf, 3, event.numIndexed())
}

func TestNew(t *testing.T) {
var (
table = Table{
Name: "foo",
Cols: []Column{
Column{Name: "block_num", Type: "numeric"},
Column{Name: "b", Type: "bytea"},
Column{Name: "c", Type: "bytea"},
},
}
block = []BlockData{
BlockData{Name: "block_num", Column: "block_num"},
}
event = Event{
Name: "bar",
Inputs: []Input{
Input{Indexed: true, Name: "a"},
Input{Indexed: true, Name: "b", Column: "b"},
Input{Indexed: true, Name: "c", Column: "c"},
},
}
)
ig, err := New("foo", event, block, table)
diff.Test(t, t.Errorf, nil, err)
diff.Test(t, t.Errorf, 5, len(ig.Columns))
}

func TestValidate_AddRequired(t *testing.T) {
ig := Integration{
name: "foo",
Expand Down

0 comments on commit 5d5d845

Please sign in to comment.