Finalize makes sure the error type is a user type since it has to generate a Go error. Note: this may produce a user type with an attribute that is not an object!
()
| 131 | // Go error. |
| 132 | // Note: this may produce a user type with an attribute that is not an object! |
| 133 | func (e *ErrorExpr) Finalize() { |
| 134 | att := e.AttributeExpr |
| 135 | switch dt := att.Type.(type) { |
| 136 | case UserType: |
| 137 | if dt != ErrorResult { |
| 138 | // If this type contains an attribute with "struct:error:name" meta |
| 139 | // then no need to do anything. |
| 140 | if IsObject(dt) { |
| 141 | for _, nat := range *AsObject(dt) { |
| 142 | if _, ok := nat.Attribute.Meta["struct:error:name"]; ok { |
| 143 | return |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | // This type does not have an attribute with "struct:error:name" meta. |
| 149 | // It means the type is used by at most one error (otherwise validations |
| 150 | // would have failed). |
| 151 | dt.Attribute().AddMeta("struct:error:name", e.Name) |
| 152 | } |
| 153 | default: |
| 154 | ut := &UserTypeExpr{ |
| 155 | AttributeExpr: att, |
| 156 | TypeName: e.Name, |
| 157 | } |
| 158 | e.AttributeExpr = &AttributeExpr{Type: ut} |
| 159 | } |
| 160 | } |