buildView builds a view expression given an attribute and a corresponding result type. The attribute must be an object listing the child attributes that make up the view.
(name string, mt *expr.ResultTypeExpr, at *expr.AttributeExpr)
| 512 | // result type. The attribute must be an object listing the child attributes |
| 513 | // that make up the view. |
| 514 | func buildView(name string, mt *expr.ResultTypeExpr, at *expr.AttributeExpr) (*expr.ViewExpr, error) { |
| 515 | if at.Type == nil { |
| 516 | return nil, fmt.Errorf("invalid view DSL") |
| 517 | } |
| 518 | o := expr.AsObject(at.Type) |
| 519 | if o == nil { |
| 520 | return nil, fmt.Errorf("invalid view DSL") |
| 521 | } |
| 522 | for _, nat := range *o { |
| 523 | n := nat.Name |
| 524 | cat := nat.Attribute |
| 525 | if existing := mt.Find(n); existing != nil { |
| 526 | dup := expr.DupAtt(existing) |
| 527 | if v, ok := cat.Meta.Last(expr.ViewMetaKey); ok { |
| 528 | dup.AddMeta("view", v) |
| 529 | } |
| 530 | o.Set(n, dup) |
| 531 | } else if n != "links" { |
| 532 | return nil, fmt.Errorf("unknown attribute %#v", n) |
| 533 | } |
| 534 | } |
| 535 | return &expr.ViewExpr{ |
| 536 | AttributeExpr: at, |
| 537 | Name: name, |
| 538 | Parent: mt, |
| 539 | }, nil |
| 540 | } |