ArrayOf creates an array type from its element type. ArrayOf may be used wherever types can. The first argument of ArrayOf is the type of the array elements specified by name or by reference. The second argument of ArrayOf is an optional function that defines validations for the array elements. Ex
(v any, fn ...func())
| 139 | // CollectionOf if the argument is a result type and ArrayOf if it is a user |
| 140 | // type. |
| 141 | func ArrayOf(v any, fn ...func()) *expr.Array { |
| 142 | var t expr.DataType |
| 143 | var ok bool |
| 144 | t, ok = v.(expr.DataType) |
| 145 | if !ok { |
| 146 | if name, ok := v.(string); ok { |
| 147 | t = expr.Root.UserType(name) |
| 148 | } |
| 149 | } |
| 150 | // never return nil to avoid panics, errors are reported after DSL execution |
| 151 | if t == nil { |
| 152 | eval.ReportError("invalid ArrayOf argument: not a type and not a known user type name") |
| 153 | return &expr.Array{ElemType: &expr.AttributeExpr{Type: expr.String}} |
| 154 | } |
| 155 | if len(fn) > 1 { |
| 156 | eval.TooManyArgError() |
| 157 | return &expr.Array{ElemType: &expr.AttributeExpr{Type: expr.String}} |
| 158 | } |
| 159 | at := expr.AttributeExpr{Type: t} |
| 160 | if len(fn) == 1 { |
| 161 | eval.Execute(fn[0], &at) |
| 162 | } |
| 163 | return &expr.Array{ElemType: &at} |
| 164 | } |
| 165 | |
| 166 | // ArrayOfRequired creates an array type from its element type where elements |
| 167 | // cannot be null. |