Skip to content

Commit

Permalink
don't expose external library type directly
Browse files Browse the repository at this point in the history
  • Loading branch information
RangelReale committed Oct 8, 2023
1 parent e7bb5f2 commit 9d9982c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
8 changes: 5 additions & 3 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,19 @@ var (
ResolveCallbackError = errors.New("resolve callback error")
)

type TokenPosition = token.Position

type ParseError struct {
ErrorMessage string
Path string
Token *token.Token
Position *TokenPosition
}

func NewParseError(msg string, path string, token *token.Token) ParseError {
func NewParseError(msg string, path string, position *TokenPosition) ParseError {
return ParseError{
ErrorMessage: msg,
Path: path,
Token: token,
Position: position,
}
}

Expand Down
24 changes: 12 additions & 12 deletions loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (l *loader) loadTables(node ast.Node, tags []string, parent parentRowInfo)
}
default:
return NewParseError(fmt.Sprintf("invalid table node '%s'", n.Type().String()),
n.GetPath(), n.GetToken())
n.GetPath(), n.GetToken().Position)
}

return nil
Expand All @@ -117,27 +117,27 @@ func (l *loader) loadTable(tableID string, node ast.Node, tags []string, parent
values = []*ast.MappingValueNode{n}
default:
return NewParseError(fmt.Sprintf("unknown table node type '%s'", node.Type().String()),
node.GetPath(), node.GetToken())
node.GetPath(), node.GetToken().Position)
}

for _, value := range values {
key, err := getStringNode(value.Key)
if err != nil {
return NewParseError(fmt.Sprintf("error getting table info for '%s': %s", tableID, err),
value.GetPath(), value.GetToken())
value.GetPath(), value.GetToken().Position)
}
switch key {
case "config":
var cfg TableConfig
err := yaml.NodeToValue(value.Value, &cfg)
if err != nil {
return NewParseError(fmt.Sprintf("error reading table config for '%s': %s", tableID, err),
value.GetPath(), value.GetToken())
value.GetPath(), value.GetToken().Position)
}
err = table.Config.Merge(&cfg)
if err != nil {
return NewParseError(fmt.Sprintf("error merge table config for '%s': %s", tableID, err),
value.GetPath(), value.GetToken())
value.GetPath(), value.GetToken().Position)
}
case "rows":
err := l.loadTableRows(value.Value, table, tags, parent)
Expand All @@ -146,7 +146,7 @@ func (l *loader) loadTable(tableID string, node ast.Node, tags []string, parent
}
default:
return NewParseError(fmt.Sprintf("unknown key in table row data: '%s' for '%s'", key, tableID),
value.GetPath(), value.GetToken())
value.GetPath(), value.GetToken().Position)
}
}
return nil
Expand All @@ -163,7 +163,7 @@ func (l *loader) loadTableRows(node ast.Node, table *Table, tags []string, paren
}
default:
return NewParseError(fmt.Sprintf("invalid table rows node '%s' (expected Sequence)", n.Type().String()),
n.GetPath(), n.GetToken())
n.GetPath(), n.GetToken().Position)
}
return nil
}
Expand All @@ -177,7 +177,7 @@ func (l *loader) loadTableRow(node ast.Node, table *Table, tags []string, parent
values = []*ast.MappingValueNode{n}
default:
return NewParseError(fmt.Sprintf("unknown table row node type '%s' (expected Mapping)", node.Type().String()),
node.GetPath(), node.GetToken())
node.GetPath(), node.GetToken().Position)
}

row := Row{
Expand All @@ -195,7 +195,7 @@ func (l *loader) loadTableRow(node ast.Node, table *Table, tags []string, parent
err := yaml.NodeToValue(field.Value, &row.Config)
if err != nil {
return NewParseError(fmt.Sprintf("error reading row config: %s", err),
field.GetPath(), field.GetToken())
field.GetPath(), field.GetToken().Position)
}
case "_dbfdeps":
err := l.loadTables(field.Value, tags, &defaultParentRowInfo{
Expand All @@ -204,11 +204,11 @@ func (l *loader) loadTableRow(node ast.Node, table *Table, tags []string, parent
})
if err != nil {
return NewParseError(fmt.Sprintf("error reading row deps: %s", err),
field.GetPath(), field.GetToken())
field.GetPath(), field.GetToken().Position)
}
default:
return NewParseError(fmt.Sprintf("invalid table row field: %s", key),
field.GetPath(), field.GetToken())
field.GetPath(), field.GetToken().Position)
}
} else {
fieldValue, err := l.loadFieldValue(field.Value, parent)
Expand Down Expand Up @@ -243,7 +243,7 @@ func (l *loader) loadFieldValue(node ast.Node, parent parentRowInfo) (any, error
return parseValue(tvalue, parent)
default:
return nil, NewParseError(fmt.Sprintf("unknown value tag: %s", n.Start.Value),
n.GetPath(), n.GetToken())
n.GetPath(), n.GetToken().Position)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion util.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ func getStringNode(node ast.Node) (string, error) {
case *ast.StringNode:
return n.Value, nil
default:
return "", NewParseError("node is not string", node.GetPath(), node.GetToken())
return "", NewParseError("node is not string", node.GetPath(), node.GetToken().Position)
}
}

0 comments on commit 9d9982c

Please sign in to comment.