本文整理汇总了Golang中github.com/goadesign/goa/design.UserTypeDefinition类的典型用法代码示例。如果您正苦于以下问题:Golang UserTypeDefinition类的具体用法?Golang UserTypeDefinition怎么用?Golang UserTypeDefinition使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了UserTypeDefinition类的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: fieldAssignmentTypeToModel
func fieldAssignmentTypeToModel(model *RelationalModelDefinition, ut *design.UserTypeDefinition, utype, mtype string) string {
// Get a sortable slice of field names
var keys []string
for k := range model.RelationalFields {
keys = append(keys, k)
}
sort.Strings(keys)
var fieldAssignments []string
for _, fname := range keys {
field := model.RelationalFields[fname]
var mpointer, upointer bool
mpointer = field.Nullable
obj := ut.ToObject()
definition := ut.Definition()
if field.Datatype == "" {
continue
}
for key := range obj {
gfield := obj[key]
if field.Underscore() == key || field.DatabaseFieldName == key {
// this is our field
if gfield.Type.IsObject() || definition.IsPrimitivePointer(key) {
upointer = true
} else {
// set it explicitly because we're reusing the same bool
upointer = false
}
var prefix string
if upointer != mpointer {
prefix = "*"
}
fa := fmt.Sprintf("\t%s.%s = %s%s.%s", mtype, fname, prefix, utype, codegen.Goify(key, true))
fieldAssignments = append(fieldAssignments, fa)
}
}
}
return strings.Join(fieldAssignments, "\n")
}
开发者ID:goadesign,项目名称:gorma,代码行数:43,代码来源:writers.go
示例2: Type
// Type implements the type definition dsl. A type definition describes a data structure consisting
// of attributes. Each attribute has a type which can also refer to a type definition (or use a
// primitive type or nested attibutes). The dsl syntax for define a type definition is the
// Attribute dsl, see Attribute.
//
// On top of specifying any attribute type, type definitions can also be used to describe the data
// structure of a request payload. They can also be used by media type definitions as reference, see
// Reference. Here is an example:
//
// Type("createPayload", func() {
// Description("Type of create and upload action payloads")
// APIVersion("1.0")
// Attribute("name", String, "name of bottle")
// Attribute("origin", Origin, "Details on wine origin") // See Origin definition below
// Required("name")
// })
//
// var Origin = Type("origin", func() {
// Description("Origin of bottle")
// Attribute("Country")
// })
//
// This function returns the newly defined type so the value can be used throughout the dsl.
func Type(name string, dsl func()) *design.UserTypeDefinition {
if design.Design.Types == nil {
design.Design.Types = make(map[string]*design.UserTypeDefinition)
} else if _, ok := design.Design.Types[name]; ok {
dslengine.ReportError("type %#v defined twice", name)
return nil
}
var t *design.UserTypeDefinition
if dslengine.TopLevelDefinition(true) {
t = &design.UserTypeDefinition{
TypeName: name,
AttributeDefinition: &design.AttributeDefinition{DSLFunc: dsl},
}
if dsl == nil {
t.Type = design.String
}
design.Design.Types[name] = t
}
return t
}
开发者ID:intfrr,项目名称:goa,代码行数:43,代码来源:type.go
示例3: GoTypeTransform
// GoTypeTransform produces Go code that initializes the data structure defined by target from an
// instance of the data structure described by source. The algorithm matches object fields by name
// or using the value of the "transform:key" attribute metadata when present.
// The function returns an error if target is not compatible with source (different type, fields of
// different type etc). It ignores fields in target that don't have a match in source.
func GoTypeTransform(source, target *design.UserTypeDefinition, targetPkg, funcName string) (string, error) {
var impl string
var err error
switch {
case source.IsObject():
if !target.IsObject() {
return "", fmt.Errorf("source is an object but target type is %s", target.Type.Name())
}
impl, err = transformObject(source.ToObject(), target.ToObject(), targetPkg, target.TypeName, "source", "target", 1)
case source.IsArray():
if !target.IsArray() {
return "", fmt.Errorf("source is an array but target type is %s", target.Type.Name())
}
impl, err = transformArray(source.ToArray(), target.ToArray(), targetPkg, "source", "target", 1)
case source.IsHash():
if !target.IsHash() {
return "", fmt.Errorf("source is a hash but target type is %s", target.Type.Name())
}
impl, err = transformHash(source.ToHash(), target.ToHash(), targetPkg, "source", "target", 1)
default:
panic("cannot transform primitive types") // bug
}
if err != nil {
return "", err
}
t := GoTypeRef(target, nil, 0)
if strings.HasPrefix(t, "*") && len(targetPkg) > 0 {
t = fmt.Sprintf("*%s.%s", targetPkg, t[1:])
}
data := map[string]interface{}{
"Name": funcName,
"Source": source,
"Target": target,
"TargetRef": t,
"TargetPkg": targetPkg,
"Impl": impl,
}
return RunTemplate(transformT, data), nil
}
开发者ID:RouGang,项目名称:goa,代码行数:45,代码来源:types.go
示例4: userTypeMarshalerFuncName
// userTypeMarshalerFuncName returns the name for the given media type marshaler function.
func userTypeMarshalerFuncName(u *design.UserTypeDefinition) string {
return fmt.Sprintf("Marshal%s", GoTypeName(u, u.AllRequired(), 0))
}
开发者ID:is00hcw,项目名称:goa,代码行数:4,代码来源:types.go
示例5: UserTypeUnmarshalerImpl
// UserTypeUnmarshalerImpl returns the code implementing the user type unmarshaler function.
func UserTypeUnmarshalerImpl(u *design.UserTypeDefinition, versioned bool, defaultPkg, context string) string {
validation := RecursiveChecker(u.AttributeDefinition, false, false, "source", context, 1)
var impl string
switch {
case u.IsObject():
impl = objectUnmarshalerR(u, u.AllRequired(), u.AllNonZero(), versioned, defaultPkg, context, "source", "target", 1)
case u.IsArray():
impl = arrayUnmarshalerR(u.ToArray(), versioned, defaultPkg, context, "source", "target", 1)
case u.IsHash():
impl = hashUnmarshalerR(u.ToHash(), versioned, defaultPkg, context, "source", "target", 1)
default:
return "" // No function for primitive types - they just get casted
}
data := map[string]interface{}{
"Name": userTypeUnmarshalerFuncName(u),
"Type": u,
"Impl": impl,
"MustValidate": strings.TrimSpace(validation) != "",
}
return RunTemplate(unmUserImplT, data)
}
开发者ID:jianjunliu,项目名称:goa,代码行数:22,代码来源:types.go
注:本文中的github.com/goadesign/goa/design.UserTypeDefinition类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论