lang: core: Simplify list and map lookup default functions

This commit is contained in:
James Shubin
2025-02-26 19:35:07 -05:00
parent d579787bcd
commit d56896cb0d
4 changed files with 77 additions and 381 deletions

View File

@@ -108,16 +108,30 @@ func (obj *LookupDefaultFunc) Build(typ *types.Type) (*types.Type, error) {
return nil, fmt.Errorf("first arg must have a type")
}
name := ""
if tListOrMap.Kind == types.KindList {
obj.fn = &ListLookupDefaultFunc{} // set it
return obj.fn.Build(typ)
name = ListLookupDefaultFuncName
}
if tListOrMap.Kind == types.KindMap {
obj.fn = &MapLookupDefaultFunc{} // set it
return obj.fn.Build(typ)
name = MapLookupDefaultFuncName
}
if name == "" {
return nil, fmt.Errorf("we must lookup from either a list or a map")
}
return nil, fmt.Errorf("we must lookup from either a list or a map")
f, err := funcs.Lookup(name)
if err != nil {
// programming error
return nil, err
}
bf, ok := f.(interfaces.BuildableFunc)
if !ok {
// programming error
return nil, fmt.Errorf("not a BuildableFunc")
}
obj.fn = bf
return obj.fn.Build(typ)
}
// Validate tells us if the input struct takes a valid form.