lang: ast: Add more validation on struct fields

Catch more errors early, just in case.
This commit is contained in:
James Shubin
2024-01-12 15:52:20 -05:00
parent acae7eccc4
commit dff9c9ad22

View File

@@ -151,6 +151,10 @@ func (obj *StmtBind) Apply(fn func(interfaces.Node) error) error {
// Init initializes this branch of the AST, and returns an error if it fails to // Init initializes this branch of the AST, and returns an error if it fails to
// validate. // validate.
func (obj *StmtBind) Init(data *interfaces.Data) error { func (obj *StmtBind) Init(data *interfaces.Data) error {
if obj.Ident == "" {
return fmt.Errorf("bind ident is empty")
}
return obj.Value.Init(data) return obj.Value.Init(data)
} }
@@ -307,6 +311,10 @@ func (obj *StmtRes) Apply(fn func(interfaces.Node) error) error {
// Init initializes this branch of the AST, and returns an error if it fails to // Init initializes this branch of the AST, and returns an error if it fails to
// validate. // validate.
func (obj *StmtRes) Init(data *interfaces.Data) error { func (obj *StmtRes) Init(data *interfaces.Data) error {
if obj.Kind == "" {
return fmt.Errorf("res kind is empty")
}
if strings.Contains(obj.Kind, "_") && obj.Kind != interfaces.PanicResKind { if strings.Contains(obj.Kind, "_") && obj.Kind != interfaces.PanicResKind {
return fmt.Errorf("kind must not contain underscores") return fmt.Errorf("kind must not contain underscores")
} }
@@ -1138,6 +1146,10 @@ func (obj *StmtResField) Apply(fn func(interfaces.Node) error) error {
// Init initializes this branch of the AST, and returns an error if it fails to // Init initializes this branch of the AST, and returns an error if it fails to
// validate. // validate.
func (obj *StmtResField) Init(data *interfaces.Data) error { func (obj *StmtResField) Init(data *interfaces.Data) error {
if obj.Field == "" {
return fmt.Errorf("res field name is empty")
}
if obj.Condition != nil { if obj.Condition != nil {
if err := obj.Condition.Init(data); err != nil { if err := obj.Condition.Init(data); err != nil {
return err return err
@@ -1416,7 +1428,7 @@ func (obj *StmtResEdge) Apply(fn func(interfaces.Node) error) error {
// validate. // validate.
func (obj *StmtResEdge) Init(data *interfaces.Data) error { func (obj *StmtResEdge) Init(data *interfaces.Data) error {
if obj.Property == "" { if obj.Property == "" {
return fmt.Errorf("empty property") return fmt.Errorf("res edge property is empty")
} }
if obj.Property != EdgeNotify && obj.Property != EdgeBefore && obj.Property != EdgeListen && obj.Property != EdgeDepend { if obj.Property != EdgeNotify && obj.Property != EdgeBefore && obj.Property != EdgeListen && obj.Property != EdgeDepend {
return fmt.Errorf("invalid property: `%s`", obj.Property) return fmt.Errorf("invalid property: `%s`", obj.Property)
@@ -1656,7 +1668,7 @@ func (obj *StmtResMeta) Apply(fn func(interfaces.Node) error) error {
// validate. // validate.
func (obj *StmtResMeta) Init(data *interfaces.Data) error { func (obj *StmtResMeta) Init(data *interfaces.Data) error {
if obj.Property == "" { if obj.Property == "" {
return fmt.Errorf("empty property") return fmt.Errorf("res meta property is empty")
} }
switch p := strings.ToLower(obj.Property); p { switch p := strings.ToLower(obj.Property); p {
@@ -2329,6 +2341,9 @@ func (obj *StmtEdgeHalf) Apply(fn func(interfaces.Node) error) error {
// Init initializes this branch of the AST, and returns an error if it fails to // Init initializes this branch of the AST, and returns an error if it fails to
// validate. // validate.
func (obj *StmtEdgeHalf) Init(data *interfaces.Data) error { func (obj *StmtEdgeHalf) Init(data *interfaces.Data) error {
if obj.Kind == "" {
return fmt.Errorf("edge half kind is empty")
}
if strings.Contains(obj.Kind, "_") { if strings.Contains(obj.Kind, "_") {
return fmt.Errorf("kind must not contain underscores") return fmt.Errorf("kind must not contain underscores")
} }
@@ -3968,6 +3983,10 @@ func (obj *StmtFunc) Apply(fn func(interfaces.Node) error) error {
// Init initializes this branch of the AST, and returns an error if it fails to // Init initializes this branch of the AST, and returns an error if it fails to
// validate. // validate.
func (obj *StmtFunc) Init(data *interfaces.Data) error { func (obj *StmtFunc) Init(data *interfaces.Data) error {
if obj.Name == "" {
return fmt.Errorf("func name is empty")
}
//obj.data = data // TODO: ??? //obj.data = data // TODO: ???
if err := obj.Func.Init(data); err != nil { if err := obj.Func.Init(data); err != nil {
return err return err
@@ -4126,6 +4145,10 @@ func (obj *StmtClass) Apply(fn func(interfaces.Node) error) error {
// Init initializes this branch of the AST, and returns an error if it fails to // Init initializes this branch of the AST, and returns an error if it fails to
// validate. // validate.
func (obj *StmtClass) Init(data *interfaces.Data) error { func (obj *StmtClass) Init(data *interfaces.Data) error {
if obj.Name == "" {
return fmt.Errorf("class name is empty")
}
return obj.Body.Init(data) return obj.Body.Init(data)
} }
@@ -4310,6 +4333,10 @@ func (obj *StmtInclude) Apply(fn func(interfaces.Node) error) error {
// Init initializes this branch of the AST, and returns an error if it fails to // Init initializes this branch of the AST, and returns an error if it fails to
// validate. // validate.
func (obj *StmtInclude) Init(data *interfaces.Data) error { func (obj *StmtInclude) Init(data *interfaces.Data) error {
if obj.Name == "" {
return fmt.Errorf("include name is empty")
}
if obj.Args != nil { if obj.Args != nil {
for _, x := range obj.Args { for _, x := range obj.Args {
if err := x.Init(data); err != nil { if err := x.Init(data); err != nil {
@@ -4643,7 +4670,12 @@ func (obj *StmtImport) Apply(fn func(interfaces.Node) error) error { return fn(o
// Init initializes this branch of the AST, and returns an error if it fails to // Init initializes this branch of the AST, and returns an error if it fails to
// validate. // validate.
func (obj *StmtImport) Init(*interfaces.Data) error { return nil } func (obj *StmtImport) Init(*interfaces.Data) error {
if obj.Name == "" {
return fmt.Errorf("import name is empty")
}
return nil
}
// Interpolate returns a new node (aka a copy) once it has been expanded. This // Interpolate returns a new node (aka a copy) once it has been expanded. This
// generally increases the size of the AST when it is used. It calls Interpolate // generally increases the size of the AST when it is used. It calls Interpolate