View has two usages: - when used inside a ResultType DSL function it defines a view for the result type. A view lists a subset of the result type attributes that are used when marshalling responses. - when used inside a Result DSL function it defines the view used to marshal the result type return
(name string, adsl ...func())
| 199 | // }) |
| 200 | // }) |
| 201 | func View(name string, adsl ...func()) { |
| 202 | if adsl == nil { |
| 203 | switch e := eval.Current().(type) { |
| 204 | case *expr.ResultTypeExpr: |
| 205 | e.AddMeta(expr.ViewMetaKey, name) |
| 206 | case *expr.AttributeExpr: |
| 207 | e.AddMeta(expr.ViewMetaKey, name) |
| 208 | default: |
| 209 | eval.IncompatibleDSL() |
| 210 | } |
| 211 | return |
| 212 | } |
| 213 | rt, ok := eval.Current().(*expr.ResultTypeExpr) |
| 214 | if !ok { |
| 215 | eval.IncompatibleDSL() |
| 216 | return |
| 217 | } |
| 218 | if rt.View(name) != nil { |
| 219 | eval.ReportError("view %q is defined multiple times in result type %q", name, rt.TypeName) |
| 220 | return |
| 221 | } |
| 222 | at := &expr.AttributeExpr{} |
| 223 | ok = false |
| 224 | if len(adsl) > 0 { |
| 225 | ok = eval.Execute(adsl[0], at) |
| 226 | } else if a, ok := rt.Type.(*expr.Array); ok { |
| 227 | // inherit view from collection element if present |
| 228 | if elem := a.ElemType; elem != nil { |
| 229 | if pa, ok2 := elem.Type.(*expr.ResultTypeExpr); ok2 { |
| 230 | if v := pa.View(name); v != nil { |
| 231 | at = v.AttributeExpr |
| 232 | rt = pa |
| 233 | } else { |
| 234 | eval.ReportError("unknown view %#v", name) |
| 235 | return |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | if ok { |
| 241 | view, err := buildView(name, rt, at) |
| 242 | if err != nil { |
| 243 | eval.ReportError(err.Error()) |
| 244 | return |
| 245 | } |
| 246 | rt.Views = append(rt.Views, view) |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | // CollectionOf creates a collection result type from its element result type. A |
| 251 | // collection result type represents the content of responses that return a |