buildViewedResultType builds a viewed result type from the given result type and projected type.
(att, projected *expr.AttributeExpr, viewspkg string, scope, viewScope *codegen.NameScope)
| 1978 | // buildViewedResultType builds a viewed result type from the given result type |
| 1979 | // and projected type. |
| 1980 | func buildViewedResultType(att, projected *expr.AttributeExpr, viewspkg string, scope, viewScope *codegen.NameScope) *ViewedResultTypeData { |
| 1981 | // collect result type views |
| 1982 | rt := att.Type.(*expr.ResultTypeExpr) |
| 1983 | isarr := expr.IsArray(att.Type) |
| 1984 | var viewName string |
| 1985 | if !rt.HasMultipleViews() { |
| 1986 | viewName = expr.DefaultView |
| 1987 | } |
| 1988 | if v, ok := att.Meta.Last(expr.ViewMetaKey); ok { |
| 1989 | viewName = v |
| 1990 | } |
| 1991 | views := buildViews(rt, viewScope) |
| 1992 | |
| 1993 | // build validation data |
| 1994 | resvar := scope.GoTypeName(att) |
| 1995 | resref := scope.GoTypeRef(att) |
| 1996 | data := map[string]any{ |
| 1997 | "Projected": scope.GoTypeName(projected), |
| 1998 | "ArgVar": "result", |
| 1999 | "Source": "result", |
| 2000 | "Views": views, |
| 2001 | "IsViewed": true, |
| 2002 | } |
| 2003 | buf := &bytes.Buffer{} |
| 2004 | if err := validateTypeCodeTmpl.Execute(buf, data); err != nil { |
| 2005 | panic(err) // bug |
| 2006 | } |
| 2007 | name := "Validate" + resvar |
| 2008 | validate := &ValidateData{ |
| 2009 | Name: name, |
| 2010 | Description: fmt.Sprintf("%s runs the validations defined on the viewed result type %s.", name, resvar), |
| 2011 | Ref: resref, |
| 2012 | Validate: buf.String(), |
| 2013 | } |
| 2014 | |
| 2015 | // build constructor to initialize viewed result type from result type |
| 2016 | vresref := viewScope.GoFullTypeRef(att, viewspkg) |
| 2017 | data = map[string]any{ |
| 2018 | "ToViewed": true, |
| 2019 | "ArgVar": "res", |
| 2020 | "ReturnVar": "vres", |
| 2021 | "Views": views, |
| 2022 | "ReturnTypeRef": vresref, |
| 2023 | "IsCollection": isarr, |
| 2024 | "TargetType": scope.GoFullTypeName(att, viewspkg), |
| 2025 | "InitName": "new" + viewScope.GoTypeName(projected), |
| 2026 | } |
| 2027 | buf = &bytes.Buffer{} |
| 2028 | if err := initTypeCodeTmpl.Execute(buf, data); err != nil { |
| 2029 | panic(err) // bug |
| 2030 | } |
| 2031 | pkg := "" |
| 2032 | if loc := codegen.UserTypeLocation(att.Type); loc != nil { |
| 2033 | pkg = loc.PackageName() |
| 2034 | } |
| 2035 | name = "NewViewed" + resvar |
| 2036 | init := &InitData{ |
| 2037 | Name: name, |
no test coverage detected