MCPcopy Create free account
hub / github.com/devfile/api / updateDiscriminator

Function updateDiscriminator

pkg/apis/workspaces/v1alpha1/union_implementation.go:70–95  ·  view source on GitHub ↗
(union Union, visitorType reflect.Type)

Source from the content-addressed store, hash-verified

68}
69
70func updateDiscriminator(union Union, visitorType reflect.Type) error {
71 unionValue := reflect.ValueOf(union)
72
73 if union.discriminator() == nil {
74 return errors.New("Discriminator should not be 'nil' in union: " + unionValue.Type().Name())
75 }
76
77 if *union.discriminator() != "" {
78 // Nothing to do
79 return nil
80 }
81
82 oneMemberPresent := false
83 for i := 0; i < visitorType.NumField(); i++ {
84 unionMemberToRead := visitorType.Field(i).Name
85 unionMember := unionValue.Elem().FieldByName(unionMemberToRead)
86 if !unionMember.IsZero() {
87 if oneMemberPresent {
88 return errors.New("Discriminator cannot be deduced from 2 values in union: " + unionValue.Type().Name())
89 }
90 oneMemberPresent = true
91 *(union.discriminator()) = unionMemberToRead
92 }
93 }
94 return nil
95}
96
97func cleanupValues(union Union, visitorType reflect.Type) error {
98 unionValue := reflect.ValueOf(union)

Callers 1

normalizeUnionFunction · 0.70

Calls 2

NameMethod · 0.80
discriminatorMethod · 0.65

Tested by

no test coverage detected