IsCompatible returns an error if a and b are not both objects, both arrays, both maps, both unions or one union and one object. actx and bctx are used to build the error message if any.
(a, b expr.DataType, actx, bctx string)
| 118 | // both maps, both unions or one union and one object. actx and bctx are used |
| 119 | // to build the error message if any. |
| 120 | func IsCompatible(a, b expr.DataType, actx, bctx string) error { |
| 121 | switch { |
| 122 | case expr.IsObject(a): |
| 123 | if !expr.IsObject(b) && !expr.IsUnion(b) { |
| 124 | return fmt.Errorf("%s is an object but %s type is %s", actx, bctx, b.Name()) |
| 125 | } |
| 126 | case expr.IsArray(a): |
| 127 | if !expr.IsArray(b) { |
| 128 | return fmt.Errorf("%s is an array but %s type is %s", actx, bctx, b.Name()) |
| 129 | } |
| 130 | case expr.IsMap(a): |
| 131 | if !expr.IsMap(b) { |
| 132 | return fmt.Errorf("%s is a hash but %s type is %s", actx, bctx, b.Name()) |
| 133 | } |
| 134 | case expr.IsUnion(a): |
| 135 | if !expr.IsUnion(b) && !expr.IsObject(b) { |
| 136 | return fmt.Errorf("%s is a union but %s type is %s", actx, bctx, b.Name()) |
| 137 | } |
| 138 | default: |
| 139 | aUT, isAUT := a.(expr.UserType) |
| 140 | bUT, isBUT := b.(expr.UserType) |
| 141 | switch { |
| 142 | case isAUT && isBUT: |
| 143 | return IsCompatible(aUT.Attribute().Type, bUT.Attribute().Type, actx, bctx) |
| 144 | case isAUT: |
| 145 | return IsCompatible(aUT.Attribute().Type, b, actx, bctx) |
| 146 | case isBUT: |
| 147 | return IsCompatible(a, bUT.Attribute().Type, actx, bctx) |
| 148 | case a.Kind() != b.Kind(): |
| 149 | return fmt.Errorf("%s is a %s but %s type is %s", actx, a.Name(), bctx, b.Name()) |
| 150 | } |
| 151 | } |
| 152 | return nil |
| 153 | } |
| 154 | |
| 155 | // AppendHelpers takes care of only appending helper functions from newH that |
| 156 | // are not already in oldH. |
no test coverage detected