當前位置: 首頁>>代碼示例>>Golang>>正文


Golang AttributeDefinition.Description方法代碼示例

本文整理匯總了Golang中github.com/raphael/goa/design.AttributeDefinition.Description方法的典型用法代碼示例。如果您正苦於以下問題:Golang AttributeDefinition.Description方法的具體用法?Golang AttributeDefinition.Description怎麽用?Golang AttributeDefinition.Description使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在github.com/raphael/goa/design.AttributeDefinition的用法示例。


在下文中一共展示了AttributeDefinition.Description方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: Attribute

// Attribute implements the attribute definition DSL. An attribute describes a data structure
// recursively. Attributes are used for describing request headers, parameters and payloads -
// response bodies and headers - media types and types. An attribute definition is recursive:
// attributes may include other attributes. At the basic level an attribute has a name,
// a type and optionally a default value and validation rules. The type of an attribute can be one of:
//
// * The primitive types Boolean, Integer, Number or String.
//
// * A type defined via the Type function.
//
// * A media type defined via the MediaType function.
//
// * An object described recursively with child attributes.
//
// * An array defined using the ArrayOf function.
//
// * An hashmap defined using the HashOf function.
//
// Attributes can be defined using the Attribute, Param, Member or Header functions depending
// on where the definition appears. The syntax for all these DSL is the same.
// Here are some examples:
//
//	Attribute("name")					// Defines an attribute of type String
//
//	Attribute("name", func() {
//		Pattern("^foo")					// Adds a validation rule to the attribute
//	})
//
//	Attribute("name", Integer)				// Defines an attribute of type Integer
//
//	Attribute("name", Integer, func() {
//		Default(42)					// With a default value
//	})
//
//	Attribute("name", Integer, "description")		// Specifies a description
//
//	Attribute("name", Integer, "description", func() {
//		Enum(1, 2)					// And validation rules
//	})
//
// Nested attributes:
//
//	Attribute("nested", func() {
//		Description("description")
//		Attribute("child")
//		Attribute("child2", func() {
//			// ....
//		})
//		Required("child")
//	})
//
// Here are all the valid usage of the Attribute function:
//
//	Attribute(name string, dataType DataType, description string, dsl func())
//
//	Attribute(name string, dataType DataType, description string)
//
//	Attribute(name string, dataType DataType, dsl func())
//
//	Attribute(name string, dataType DataType)
//
//	Attribute(name string, dsl func())	// dataType is String or Object (if DSL defines child attributes)
//
//	Attribute(name string)			// dataType is String
func Attribute(name string, args ...interface{}) {
	var parent *design.AttributeDefinition
	if at, ok := attributeDefinition(false); ok {
		parent = at
	} else if mt, ok := mediaTypeDefinition(true); ok {
		parent = mt.AttributeDefinition
	}

	if parent != nil {
		if parent.Type == nil {
			parent.Type = design.Object{}
		}
		if _, ok := parent.Type.(design.Object); !ok {
			ReportError("can't define child attributes on attribute of type %s", parent.Type.Name())
			return
		}

		var baseAttr *design.AttributeDefinition
		if parent.Reference != nil {
			if att, ok := parent.Reference.ToObject()[name]; ok {
				baseAttr = att.Dup()
			}
		}

		dataType, description, dsl := parseAttributeArgs(baseAttr, args...)
		if baseAttr != nil {
			if description != "" {
				baseAttr.Description = description
			}
			if dataType != nil {
				baseAttr.Type = dataType
			}
		} else {
			baseAttr = &design.AttributeDefinition{
				Type:        dataType,
				Description: description,
//.........這裏部分代碼省略.........
開發者ID:minloved,項目名稱:goa,代碼行數:101,代碼來源:attribute.go

示例2: Attribute


//.........這裏部分代碼省略.........
//	 Attribute(name string, dataType DataType, description string, dsl func())
//
//	 Attribute(name string, dataType DataType, description string)
//
//	 Attribute(name string, dataType DataType, dsl func())
//
//	 Attribute(name string, dataType DataType)
//
//	 Attribute(name string, dsl func()) /* dataType is String or Object (if DSL defines child attributes) */
//
//	 Attribute(name string) /* dataType is String */
func Attribute(name string, args ...interface{}) {
	var parent *design.AttributeDefinition
	if at, ok := attributeDefinition(false); ok {
		parent = at
	} else if mt, ok := mediaTypeDefinition(true); ok {
		parent = mt.AttributeDefinition
	}
	if parent != nil {
		if parent.Type == nil {
			parent.Type = design.Object{}
		} else if _, ok := parent.Type.(design.Object); !ok {
			ReportError("can't define child attributes on attribute of type %s", parent.Type.Name())
			return
		}
		var baseAttr *design.AttributeDefinition
		if parent.Reference != nil {
			for n, att := range parent.Reference.ToObject() {
				if n == name {
					baseAttr = att.Dup()
					break
				}
			}
		}
		var dataType design.DataType
		var description string
		var dsl func()
		var ok bool
		if len(args) == 0 {
			if baseAttr != nil {
				dataType = baseAttr.Type
			} else {
				dataType = design.String
			}
		} else if len(args) == 1 {
			if dsl, ok = args[0].(func()); !ok {
				if dataType, ok = args[0].(design.DataType); !ok {
					invalidArgError("DataType or func()", args[0])
				}
			} else if baseAttr != nil {
				dataType = baseAttr.Type
			}
		} else if len(args) == 2 {
			if dataType, ok = args[0].(design.DataType); !ok {
				invalidArgError("DataType", args[0])
			}
			if dsl, ok = args[1].(func()); !ok {
				if description, ok = args[1].(string); !ok {
					invalidArgError("string or func()", args[1])
				}
			}
		} else if len(args) == 3 {
			if dataType, ok = args[0].(design.DataType); !ok {
				invalidArgError("DataType", args[0])
			}
			if description, ok = args[1].(string); !ok {
				invalidArgError("string", args[1])
			}
			if dsl, ok = args[2].(func()); !ok {
				invalidArgError("func()", args[2])
			}
		} else {
			ReportError("too many arguments in call to Attribute")
		}
		var att *design.AttributeDefinition
		if baseAttr != nil {
			att = baseAttr
			if description != "" {
				att.Description = description
			}
			if dataType != nil {
				att.Type = dataType
			}
		} else {
			att = &design.AttributeDefinition{
				Type:        dataType,
				Description: description,
			}
		}
		att.Reference = parent.Reference
		if dsl != nil {
			executeDSL(dsl, att)
		}
		if att.Type == nil {
			// DSL did not contain an "Attribute" declaration
			att.Type = design.String
		}
		parent.Type.(design.Object)[name] = att
	}
}
開發者ID:harboe,項目名稱:goa,代碼行數:101,代碼來源:attribute.go


注:本文中的github.com/raphael/goa/design.AttributeDefinition.Description方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。