(union Union, visitorType reflect.Type)
| 95 | } |
| 96 | |
| 97 | func cleanupValues(union Union, visitorType reflect.Type) error { |
| 98 | unionValue := reflect.ValueOf(union) |
| 99 | |
| 100 | if union.discriminator() == nil { |
| 101 | return errors.New("Discriminator should not be 'nil' in union: " + unionValue.Type().Name()) |
| 102 | } |
| 103 | |
| 104 | if *union.discriminator() == "" { |
| 105 | // Nothing to do |
| 106 | return errors.New("Values cannot be cleaned up without a discriminator in union: " + unionValue.Type().Name()) |
| 107 | } |
| 108 | |
| 109 | for i := 0; i < visitorType.NumField(); i++ { |
| 110 | unionMemberToRead := visitorType.Field(i).Name |
| 111 | unionMember := unionValue.Elem().FieldByName(unionMemberToRead) |
| 112 | if !unionMember.IsZero() { |
| 113 | if unionMemberToRead != *union.discriminator() { |
| 114 | unionMember.Set(reflect.Zero(unionMember.Type())) |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | return nil |
| 119 | } |
no test coverage detected