MCPcopy Create free account
hub / github.com/goadesign/goa / recurseValidationCode

Function recurseValidationCode

codegen/validation.go:94–261  ·  view source on GitHub ↗
(att *expr.AttributeExpr, put expr.UserType, attCtx *AttributeContext, req, alias, view bool, target, context string, seen map[string]*bytes.Buffer)

Source from the content-addressed store, hash-verified

92}
93
94func recurseValidationCode(att *expr.AttributeExpr, put expr.UserType, attCtx *AttributeContext, req, alias, view bool, target, context string, seen map[string]*bytes.Buffer) *bytes.Buffer {
95 if seen == nil {
96 seen = make(map[string]*bytes.Buffer)
97 }
98 var (
99 buf = new(bytes.Buffer)
100 first = true
101 ut, isUT = att.Type.(expr.UserType)
102 )
103
104 // Break infinite recursions
105 // Note: when alias=true, we're validating the underlying base type,
106 // so alias types shouldn't use the recursion guard. Only non-alias user
107 // types need cycle protection.
108 if isUT && !alias {
109 if buf, ok := seen[ut.ID()]; ok {
110 return buf
111 }
112 seen[ut.ID()] = buf
113 }
114
115 flattenValidations(att, make(map[string]struct{}))
116
117 newline := func() {
118 if !first {
119 buf.WriteByte('\n')
120 } else {
121 first = false
122 }
123 }
124
125 // Write validations on attribute if any.
126 validation := validationCode(att, attCtx, req, alias, target, context)
127 if validation != "" {
128 buf.WriteString(validation)
129 first = false
130 }
131
132 // Recurse down depending on attribute type.
133 switch {
134 case expr.IsObject(att.Type):
135 if isUT {
136 put = ut
137 }
138 for _, nat := range *(expr.AsObject(att.Type)) {
139 tgt := fmt.Sprintf("%s.%s", target, attCtx.Scope.Field(nat.Attribute, nat.Name, true))
140 ctx := fmt.Sprintf("%s.%s", context, nat.Name)
141 val := validateAttribute(attCtx, nat.Attribute, put, tgt, ctx, att.IsRequired(nat.Name), view, seen)
142 if val != "" {
143 newline()
144 buf.WriteString(val)
145 }
146 }
147 case expr.IsArray(att.Type):
148 arr := expr.AsArray(att.Type)
149 elem := arr.ElemType
150 ctx := attCtx
151 if ctx.Pointer && expr.IsPrimitive(elem.Type) {

Callers 3

AttributeValidationCodeFunction · 0.85
ValidationCodeFunction · 0.85
validateAttributeFunction · 0.85

Calls 15

IsObjectFunction · 0.92
AsObjectFunction · 0.92
IsArrayFunction · 0.92
AsArrayFunction · 0.92
IsPrimitiveFunction · 0.92
IsMapFunction · 0.92
AsMapFunction · 0.92
IsUnionFunction · 0.92
AsUnionFunction · 0.92
flattenValidationsFunction · 0.85
validationCodeFunction · 0.85
validateAttributeFunction · 0.85

Tested by

no test coverage detected