11 Star 11 Fork 0

Gitee 极速下载/goa

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/goadesign/goa
克隆/下载
typedef.go 3.18 KB
一键复制 编辑 原始数据 按行查看 历史
package codegen
import (
"fmt"
"strings"
"goa.design/goa/codegen"
"goa.design/goa/expr"
)
// goTypeDef returns the Go code that defines the struct corresponding to ma.
// It differs from the function defined in the codegen package in the following
// ways:
//
// - It defines marshaler tags on each fields using the HTTP element names.
//
// - It produced fields with pointers even if the corresponding attribute is
// required when ptr is true so that the generated code may validate
// explicitly.
//
// useDefault directs whether fields holding primitive types with default values
// should hold pointers when ptr is false. If it is true then the fields are
// values even when not required (to account for the fact that they have a
// default value so cannot be nil) otherwise the fields are values only when
// required.
func goTypeDef(scope *codegen.NameScope, att *expr.AttributeExpr, ptr, useDefault bool) string {
switch actual := att.Type.(type) {
case expr.Primitive:
if t, _ := codegen.GetMetaType(att); t != "" {
return t
}
return codegen.GoNativeTypeName(actual)
case *expr.Array:
d := goTypeDef(scope, actual.ElemType, ptr, useDefault)
if expr.IsObject(actual.ElemType.Type) {
d = "*" + d
}
return "[]" + d
case *expr.Map:
keyDef := goTypeDef(scope, actual.KeyType, ptr, useDefault)
if expr.IsObject(actual.KeyType.Type) {
keyDef = "*" + keyDef
}
elemDef := goTypeDef(scope, actual.ElemType, ptr, useDefault)
if expr.IsObject(actual.ElemType.Type) {
elemDef = "*" + elemDef
}
return fmt.Sprintf("map[%s]%s", keyDef, elemDef)
case *expr.Object:
var ss []string
ss = append(ss, "struct {")
ma := expr.NewMappedAttributeExpr(att)
mat := ma.Attribute()
codegen.WalkMappedAttr(ma, func(name, elem string, required bool, at *expr.AttributeExpr) error {
var (
fn string
tdef string
desc string
tags string
)
{
fn = codegen.GoifyAtt(at, name, true)
tdef = goTypeDef(scope, at, ptr, useDefault)
if expr.IsPrimitive(at.Type) {
if (ptr || mat.IsPrimitivePointer(name, useDefault)) && at.Type != expr.Bytes && at.Type != expr.Any {
tdef = "*" + tdef
}
} else if expr.IsObject(at.Type) {
tdef = "*" + tdef
}
if at.Description != "" {
desc = codegen.Comment(at.Description) + "\n\t"
}
var optional bool
{
switch {
case ptr:
optional = true
case useDefault:
optional = !ma.IsRequired(name) && !ma.HasDefaultValue(name)
default:
optional = !ma.IsRequired(name)
}
}
tags = attributeTags(mat, at, elem, optional)
}
ss = append(ss, fmt.Sprintf("\t%s%s %s%s", desc, fn, tdef, tags))
return nil
})
ss = append(ss, "}")
return strings.Join(ss, "\n")
case expr.UserType:
return scope.GoTypeName(att)
default:
panic(fmt.Sprintf("unknown data type %T", actual)) // bug
}
}
// attributeTags computes the struct field tags.
func attributeTags(parent, att *expr.AttributeExpr, t string, optional bool) string {
if tags := codegen.AttributeTags(parent, att); tags != "" {
return tags
}
var o string
if optional {
o = ",omitempty"
}
return fmt.Sprintf(" `form:\"%s%s\" json:\"%s%s\" xml:\"%s%s\"`", t, o, t, o, t, o)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mirrors/goa.git
git@gitee.com:mirrors/goa.git
mirrors
goa
goa
v2.2.4

搜索帮助