本文整理汇总了Golang中github.com/goadesign/goa/dslengine.ReportError函数的典型用法代码示例。如果您正苦于以下问题:Golang ReportError函数的具体用法?Golang ReportError怎么用?Golang ReportError使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ReportError函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Store
// Store represents a database. Gorma lets you specify
// a database type, but it's currently not used for any generation
// logic.
func Store(name string, storeType gorma.RelationalStorageType, dsl func()) {
if name == "" || len(name) == 0 {
dslengine.ReportError("Relational Store requires a name.")
return
}
if len(storeType) == 0 {
dslengine.ReportError("Relational Store requires a RelationalStoreType.")
return
}
if dsl == nil {
dslengine.ReportError("Relational Store requires a dsl.")
return
}
if s, ok := storageGroupDefinition(true); ok {
if s.RelationalStores == nil {
s.RelationalStores = make(map[string]*gorma.RelationalStoreDefinition)
}
store, ok := s.RelationalStores[name]
if !ok {
store = &gorma.RelationalStoreDefinition{
Name: name,
DefinitionDSL: dsl,
Parent: s,
Type: storeType,
RelationalModels: make(map[string]*gorma.RelationalModelDefinition),
}
} else {
dslengine.ReportError("Relational Store %s can only be declared once.", name)
}
s.RelationalStores[name] = store
}
}
示例2: executeResponseDSL
func executeResponseDSL(name string, paramsAndDSL ...interface{}) *design.ResponseDefinition {
var params []string
var dsl func()
var ok bool
var dt design.DataType
if len(paramsAndDSL) > 0 {
d := paramsAndDSL[len(paramsAndDSL)-1]
if dsl, ok = d.(func()); ok {
paramsAndDSL = paramsAndDSL[:len(paramsAndDSL)-1]
}
if len(paramsAndDSL) > 0 {
t := paramsAndDSL[0]
if dt, ok = t.(design.DataType); ok {
paramsAndDSL = paramsAndDSL[1:]
}
}
params = make([]string, len(paramsAndDSL))
for i, p := range paramsAndDSL {
params[i], ok = p.(string)
if !ok {
dslengine.ReportError("invalid response template parameter %#v, must be a string", p)
return nil
}
}
}
var resp *design.ResponseDefinition
if len(params) > 0 {
if tmpl, ok := design.Design.ResponseTemplates[name]; ok {
resp = tmpl.Template(params...)
} else if tmpl, ok := design.Design.DefaultResponseTemplates[name]; ok {
resp = tmpl.Template(params...)
} else {
dslengine.ReportError("no response template named %#v", name)
return nil
}
} else {
if ar, ok := design.Design.Responses[name]; ok {
resp = ar.Dup()
} else if ar, ok := design.Design.DefaultResponses[name]; ok {
resp = ar.Dup()
resp.Standard = true
} else {
resp = &design.ResponseDefinition{Name: name}
}
}
if dsl != nil {
if !dslengine.Execute(dsl, resp) {
return nil
}
resp.Standard = false
}
if dt != nil {
if mt, ok := dt.(*design.MediaTypeDefinition); ok {
resp.MediaType = mt.Identifier
}
resp.Type = dt
resp.Standard = false
}
return resp
}
示例3: Trait
// Trait defines an API trait. A trait encapsulates arbitrary DSL that gets executed wherever the
// trait is called via the UseTrait function.
func Trait(name string, val ...func()) {
var ver *design.APIVersionDefinition
if a, ok := apiDefinition(false); ok {
ver = a.APIVersionDefinition
} else if v, ok := versionDefinition(true); ok {
ver = v
}
if len(val) < 1 {
dslengine.ReportError("missing trait DSL for %s", name)
return
} else if len(val) > 1 {
dslengine.ReportError("too many arguments given to Trait")
return
}
if ver == nil {
return
}
if _, ok := ver.Traits[name]; ok {
dslengine.ReportError("multiple definitions for trait %s%s", name, ver.Context())
return
}
trait := &dslengine.TraitDefinition{Name: name, DSLFunc: val[0]}
if ver.Traits == nil {
ver.Traits = make(map[string]*dslengine.TraitDefinition)
}
ver.Traits[name] = trait
}
示例4: Response
// Response implements the response definition DSL. Response takes the name of the response as
// first parameter. goa defines all the standard HTTP status name as global variables so they can be
// readily used as response names. Response also accepts optional arguments that correspond to the
// arguments defined by the corresponding response template (the response template with the same
// name) if there is one, see ResponseTemplate.
//
// A response may also optionally use an anonymous function as last argument to specify the response
// status code, media type and headers overriding what the default response or response template
// specifies:
//
// Response(OK, "vnd.goa.bottle", func() { // OK response template accepts one argument: the media type identifier
// Headers(func() { // Headers list the response HTTP headers, see Headers
// Header("X-Request-Id")
// })
// })
//
// Response(NotFound, func() {
// Status(404) // Not necessary as defined by default NotFound response.
// Media("application/json") // Override NotFound response default of "text/plain"
// })
//
// Response(Created, func() {
// Media(BottleMedia)
// })
//
// goa defines a default response for all the HTTP status code. The default response simply sets
// the status code. So if an action can return NotFound for example all it has to do is specify
// Response(NotFound) - there is no need to specify the status code as the default response already
// does it, in other words:
//
// Response(NotFound)
//
// is equivalent to:
//
// Response(NotFound, func() {
// Status(404)
// })
//
// goa also defines a default response template for the OK response which takes a single argument:
// the identifier of the media type used to render the response. The API DSL can define additional
// response templates or override the default OK response template using ResponseTemplate.
//
// The media type identifier specified in a response definition via the Media function can be
// "generic" such as "text/plain" or "application/json" or can correspond to the identifier of a
// media type defined in the API DSL. In this latter case goa uses the media type definition to
// generate helper response methods. These methods know how to render the views defined on the media
// type and run the validations defined in the media type during rendering.
func Response(name string, paramsAndDSL ...interface{}) {
if a, ok := actionDefinition(false); ok {
if a.Responses == nil {
a.Responses = make(map[string]*design.ResponseDefinition)
}
if _, ok := a.Responses[name]; ok {
dslengine.ReportError("response %s is defined twice", name)
return
}
if resp := executeResponseDSL(name, paramsAndDSL...); resp != nil {
if resp.Status == 200 && resp.MediaType == "" {
resp.MediaType = a.Parent.MediaType
}
resp.Parent = a
a.Responses[name] = resp
}
} else if r, ok := resourceDefinition(true); ok {
if r.Responses == nil {
r.Responses = make(map[string]*design.ResponseDefinition)
}
if _, ok := r.Responses[name]; ok {
dslengine.ReportError("response %s is defined twice", name)
return
}
if resp := executeResponseDSL(name, paramsAndDSL...); resp != nil {
if resp.Status == 200 && resp.MediaType == "" {
resp.MediaType = r.MediaType
}
resp.Parent = r
r.Responses[name] = resp
}
}
}
示例5: buildEncodingDefinition
// buildEncodingDefinition builds up an encoding definition.
func buildEncodingDefinition(encoding bool, args ...interface{}) *design.EncodingDefinition {
var dsl func()
var ok bool
funcName := "Consumes"
if encoding {
funcName = "Produces"
}
if len(args) == 0 {
dslengine.ReportError("missing argument in call to %s", funcName)
return nil
}
if _, ok = args[0].(string); !ok {
dslengine.ReportError("first argument to %s must be a string (MIME type)", funcName)
return nil
}
last := len(args)
if dsl, ok = args[len(args)-1].(func()); ok {
last = len(args) - 1
}
mimeTypes := make([]string, last)
for i := 0; i < last; i++ {
var mimeType string
if mimeType, ok = args[i].(string); !ok {
dslengine.ReportError("argument #%d of %s must be a string (MIME type)", i, funcName)
return nil
}
mimeTypes[i] = mimeType
}
d := &design.EncodingDefinition{MIMETypes: mimeTypes, Encoder: encoding}
if dsl != nil {
dslengine.Execute(dsl, d)
}
return d
}
示例6: ResponseTemplate
// ResponseTemplate defines a response template that action definitions can use to describe their
// responses. The template may specify the HTTP response status, header specification and body media
// type. The template consists of a name and an anonymous function. The function is called when an
// action uses the template to define a response. Response template functions accept string
// parameters they can use to define the response fields. Here is an example of a response template
// definition that uses a function with one argument corresponding to the name of the response body
// media type:
//
// ResponseTemplate(OK, func(mt string) {
// Status(200) // OK response uses status code 200
// Media(mt) // Media type name set by action definition
// Headers(func() {
// Header("X-Request-Id", func() { // X-Request-Id header contains a string
// Pattern("[0-9A-F]+") // Regexp used to validate the response header content
// })
// Required("X-Request-Id") // Header is mandatory
// })
// })
//
// This template can the be used by actions to define the OK response as follows:
//
// Response(OK, "vnd.goa.example")
//
// goa comes with a set of predefined response templates (one per standard HTTP status code). The
// OK template is the only one that accepts an argument. It is used as shown in the example above to
// set the response media type. Other predefined templates do not use arguments. ResponseTemplate
// makes it possible to define additional response templates specific to the API.
func ResponseTemplate(name string, p interface{}) {
var v *design.APIVersionDefinition
if a, ok := apiDefinition(false); ok {
v = a.APIVersionDefinition
} else if ver, ok := versionDefinition(true); ok {
v = ver
}
if v == nil {
return
}
if v.Responses == nil {
v.Responses = make(map[string]*design.ResponseDefinition)
}
if v.ResponseTemplates == nil {
v.ResponseTemplates = make(map[string]*design.ResponseTemplateDefinition)
}
if _, ok := v.Responses[name]; ok {
dslengine.ReportError("multiple definitions for response template %s", name)
return
}
if _, ok := v.ResponseTemplates[name]; ok {
dslengine.ReportError("multiple definitions for response template %s", name)
return
}
setupResponseTemplate(v, name, p)
}
示例7: Headers
// Headers implements the DSL for describing HTTP headers. The DSL syntax is identical to the one
// of Attribute. Here is an example defining a couple of headers with validations:
//
// Headers(func() {
// Header("Authorization")
// Header("X-Account", Integer, func() {
// Minimum(1)
// })
// Required("Authorization")
// })
//
// Headers can be used inside Action to define the action request headers, Response to define the
// response headers or Resource to define common request headers to all the resource actions.
func Headers(dsl func()) {
if a, ok := actionDefinition(false); ok {
headers := newAttribute(a.Parent.MediaType)
if dslengine.Execute(dsl, headers) {
a.Headers = headers
}
} else if r, ok := resourceDefinition(false); ok {
headers := newAttribute(r.MediaType)
if dslengine.Execute(dsl, headers) {
r.Headers = headers
}
} else if r, ok := responseDefinition(true); ok {
if r.Headers != nil {
dslengine.ReportError("headers already defined")
return
}
var h *design.AttributeDefinition
switch actual := r.Parent.(type) {
case *design.ResourceDefinition:
h = newAttribute(actual.MediaType)
case *design.ActionDefinition:
h = newAttribute(actual.Parent.MediaType)
case nil: // API ResponseTemplate
h = &design.AttributeDefinition{}
default:
dslengine.ReportError("invalid use of Response or ResponseTemplate")
}
if dslengine.Execute(dsl, h) {
r.Headers = h
}
}
}
示例8: View
// View adds a new view to a media type. A view has a name and lists attributes that are
// rendered when the view is used to produce a response. The attribute names must appear in the
// media type definition. If an attribute is itself a media type then the view may specify which
// view to use when rendering the attribute using the View function in the View dsl. If not
// specified then the view named "default" is used. Examples:
//
// View("default", func() {
// Attribute("id") // "id" and "name" must be media type attributes
// Attribute("name")
// })
//
// View("extended", func() {
// Attribute("id")
// Attribute("name")
// Attribute("origin", func() {
// View("extended") // Use view "extended" to render attribute "origin"
// })
// })
func View(name string, dsl ...func()) {
if mt, ok := mediaTypeDefinition(false); ok {
if !mt.Type.IsObject() && !mt.Type.IsArray() {
dslengine.ReportError("cannot define view on non object and non collection media types")
return
}
if mt.Views == nil {
mt.Views = make(map[string]*design.ViewDefinition)
} else {
if _, ok = mt.Views[name]; ok {
dslengine.ReportError("multiple definitions for view %#v in media type %#v", name, mt.TypeName)
return
}
}
at := &design.AttributeDefinition{}
ok := false
if len(dsl) > 0 {
ok = dslengine.Execute(dsl[0], at)
} else if mt.Type.IsArray() {
// inherit view from collection element if present
elem := mt.Type.ToArray().ElemType
if elem != nil {
if pa, ok2 := elem.Type.(*design.MediaTypeDefinition); ok2 {
if v, ok2 := pa.Views[name]; ok2 {
at = v.AttributeDefinition
ok = true
} else {
dslengine.ReportError("unknown view %#v", name)
return
}
}
}
}
if ok {
o := at.Type.ToObject()
if o != nil {
mto := mt.Type.ToObject()
if mto == nil {
mto = mt.Type.ToArray().ElemType.Type.ToObject()
}
for n, cat := range o {
if existing, ok := mto[n]; ok {
dup := existing.Dup()
dup.View = cat.View
o[n] = dup
} else if n != "links" {
dslengine.ReportError("unknown attribute %#v", n)
}
}
}
mt.Views[name] = &design.ViewDefinition{
AttributeDefinition: at,
Name: name,
Parent: mt,
}
}
} else if a, ok := attributeDefinition(true); ok {
a.View = name
}
}
示例9: ArrayOf
// ArrayOf creates an array type from its element type. The result can be used anywhere a type can.
// Examples:
//
// var Bottle = Type("bottle", func() {
// Attribute("name")
// })
//
// var Bottles = ArrayOf(Bottle)
//
// Action("update", func() {
// Params(func() {
// Param("ids", ArrayOf(Integer))
// })
// Payload(ArrayOf(Bottle)) // Equivalent to Payload(Bottles)
// })
//
// ArrayOf accepts an optional DSL as second argument which allows providing validations for the
// elements of the array:
//
// var Names = ArrayOf(String, func() {
// Pattern("[a-zA-Z]+")
// })
//
// If you are looking to return a collection of elements in a Response clause, refer to
// CollectionOf. ArrayOf creates a type, where CollectionOf creates a media type.
func ArrayOf(v interface{}, dsl ...func()) *design.Array {
var t design.DataType
var ok bool
t, ok = v.(design.DataType)
if !ok {
if name, ok := v.(string); ok {
t = design.Design.Types[name]
}
}
// never return nil to avoid panics, errors are reported after DSL execution
res := &design.Array{ElemType: &design.AttributeDefinition{Type: design.String}}
if t == nil {
dslengine.ReportError("invalid ArrayOf argument: not a type and not a known user type name")
return res
}
if len(dsl) > 1 {
dslengine.ReportError("ArrayOf: too many arguments")
return res
}
at := design.AttributeDefinition{Type: t}
if len(dsl) == 1 {
dslengine.Execute(dsl[0], &at)
}
return &design.Array{ElemType: &at}
}
示例10: Scope
// Scope defines an authorization scope. Used within SecurityScheme, a description may be provided
// explaining what the scope means. Within a Security block, only a scope is needed.
func Scope(name string, desc ...string) {
switch current := dslengine.CurrentDefinition().(type) {
case *design.SecurityDefinition:
if len(desc) >= 1 {
dslengine.ReportError("too many arguments")
return
}
current.Scopes = append(current.Scopes, name)
case *design.SecuritySchemeDefinition:
if len(desc) > 1 {
dslengine.ReportError("too many arguments")
return
}
if current.Scopes == nil {
current.Scopes = make(map[string]string)
}
d := "no description"
if len(desc) == 1 {
d = desc[0]
}
current.Scopes[name] = d
default:
dslengine.IncompatibleDSL()
}
}
示例11: Maximum
// Maximum adds a "maximum" validation to the attribute.
// See http://json-schema.org/latest/json-schema-validation.html#anchor17.
func Maximum(val interface{}) {
if a, ok := attributeDefinition(); ok {
if a.Type != nil && a.Type.Kind() != design.IntegerKind && a.Type.Kind() != design.NumberKind {
incompatibleAttributeType("maximum", a.Type.Name(), "an integer or a number")
} else {
var f float64
switch v := val.(type) {
case float32, float64, int, int8, int16, int32, int64, uint8, uint16, uint32, uint64:
f = reflect.ValueOf(v).Convert(reflect.TypeOf(float64(0.0))).Float()
case string:
var err error
f, err = strconv.ParseFloat(v, 64)
if err != nil {
dslengine.ReportError("invalid number value %#v", v)
return
}
default:
dslengine.ReportError("invalid number value %#v", v)
return
}
if a.Validation == nil {
a.Validation = &dslengine.ValidationDefinition{}
}
a.Validation.Maximum = &f
}
}
}
示例12: Headers
// Headers implements the DSL for describing HTTP headers. The DSL syntax is identical to the one
// of Attribute. Here is an example defining a couple of headers with validations:
//
// Headers(func() {
// Header("Authorization")
// Header("X-Account", Integer, func() {
// Minimum(1)
// })
// Required("Authorization")
// })
//
// Headers can be used inside Action to define the action request headers, Response to define the
// response headers or Resource to define common request headers to all the resource actions.
func Headers(params ...interface{}) {
if len(params) == 0 {
dslengine.ReportError("missing parameter")
return
}
dsl, ok := params[0].(func())
if ok {
switch def := dslengine.CurrentDefinition().(type) {
case *design.ActionDefinition:
headers := newAttribute(def.Parent.MediaType)
if dslengine.Execute(dsl, headers) {
def.Headers = headers
}
case *design.ResourceDefinition:
headers := newAttribute(def.MediaType)
if dslengine.Execute(dsl, headers) {
def.Headers = headers
}
case *design.ResponseDefinition:
if def.Headers != nil {
dslengine.ReportError("headers already defined")
return
}
var h *design.AttributeDefinition
switch actual := def.Parent.(type) {
case *design.ResourceDefinition:
h = newAttribute(actual.MediaType)
case *design.ActionDefinition:
h = newAttribute(actual.Parent.MediaType)
case nil: // API ResponseTemplate
h = &design.AttributeDefinition{}
default:
dslengine.ReportError("invalid use of Response or ResponseTemplate")
}
if dslengine.Execute(dsl, h) {
def.Headers = h
}
default:
dslengine.IncompatibleDSL()
}
} else if cors, ok := corsDefinition(); ok {
vals := make([]string, len(params))
for i, p := range params {
if v, ok := p.(string); ok {
vals[i] = v
} else {
dslengine.ReportError("invalid parameter at position %d: must be a string", i)
return
}
}
cors.Headers = vals
} else {
dslengine.IncompatibleDSL()
}
}
示例13: payload
func payload(isOptional bool, p interface{}, dsls ...func()) {
if len(dsls) > 1 {
dslengine.ReportError("too many arguments given to Payload")
return
}
if a, ok := actionDefinition(); ok {
var att *design.AttributeDefinition
var dsl func()
switch actual := p.(type) {
case func():
dsl = actual
att = newAttribute(a.Parent.MediaType)
att.Type = design.Object{}
case *design.AttributeDefinition:
att = design.DupAtt(actual)
case *design.UserTypeDefinition:
if len(dsls) == 0 {
a.Payload = actual
a.PayloadOptional = isOptional
return
}
att = design.DupAtt(actual.Definition())
case *design.MediaTypeDefinition:
att = design.DupAtt(actual.AttributeDefinition)
case string:
ut, ok := design.Design.Types[actual]
if !ok {
dslengine.ReportError("unknown payload type %s", actual)
}
att = design.DupAtt(ut.AttributeDefinition)
case *design.Array:
att = &design.AttributeDefinition{Type: actual}
case *design.Hash:
att = &design.AttributeDefinition{Type: actual}
case design.Primitive:
att = &design.AttributeDefinition{Type: actual}
default:
dslengine.ReportError("invalid Payload argument, must be a type, a media type or a DSL building a type")
return
}
if len(dsls) == 1 {
if dsl != nil {
dslengine.ReportError("invalid arguments in Payload call, must be (type), (dsl) or (type, dsl)")
}
dsl = dsls[0]
}
if dsl != nil {
dslengine.Execute(dsl, att)
}
rn := camelize(a.Parent.Name)
an := camelize(a.Name)
a.Payload = &design.UserTypeDefinition{
AttributeDefinition: att,
TypeName: fmt.Sprintf("%s%sPayload", an, rn),
}
a.PayloadOptional = isOptional
}
}
示例14: setupResponseTemplate
func setupResponseTemplate(a *design.APIDefinition, name string, p interface{}) {
if f, ok := p.(func()); ok {
r := &design.ResponseDefinition{Name: name}
if dslengine.Execute(f, r) {
a.Responses[name] = r
}
} else if tmpl, ok := p.(func(...string)); ok {
t := func(params ...string) *design.ResponseDefinition {
r := &design.ResponseDefinition{Name: name}
dslengine.Execute(func() { tmpl(params...) }, r)
return r
}
a.ResponseTemplates[name] = &design.ResponseTemplateDefinition{
Name: name,
Template: t,
}
} else {
typ := reflect.TypeOf(p)
if kind := typ.Kind(); kind != reflect.Func {
dslengine.ReportError("dsl must be a function but got %s", kind)
return
}
num := typ.NumIn()
val := reflect.ValueOf(p)
t := func(params ...string) *design.ResponseDefinition {
if len(params) < num {
args := "1 argument"
if num > 0 {
args = fmt.Sprintf("%d arguments", num)
}
dslengine.ReportError("expected at least %s when invoking response template %s", args, name)
return nil
}
r := &design.ResponseDefinition{Name: name}
in := make([]reflect.Value, num)
for i := 0; i < num; i++ {
// type checking
if t := typ.In(i); t.Kind() != reflect.String {
dslengine.ReportError("ResponseTemplate parameters must be strings but type of parameter at position %d is %s", i, t)
return nil
}
// append input arguments
in[i] = reflect.ValueOf(params[i])
}
dslengine.Execute(func() { val.Call(in) }, r)
return r
}
a.ResponseTemplates[name] = &design.ResponseTemplateDefinition{
Name: name,
Template: t,
}
}
}
示例15: View
// View adds a new view to a media type. A view has a name and lists attributes that are
// rendered when the view is used to produce a response. The attribute names must appear in the
// media type definition. If an attribute is itself a media type then the view may specify which
// view to use when rendering the attribute using the View function in the View apidsl. If not
// specified then the view named "default" is used. Examples:
//
// View("default", func() {
// Attribute("id") // "id" and "name" must be media type attributes
// Attribute("name")
// })
//
// View("extended", func() {
// Attribute("id")
// Attribute("name")
// Attribute("origin", func() {
// View("extended") // Use view "extended" to render attribute "origin"
// })
// })
func View(name string, apidsl ...func()) {
switch def := dslengine.CurrentDefinition().(type) {
case *design.MediaTypeDefinition:
mt := def
if !mt.Type.IsObject() && !mt.Type.IsArray() {
dslengine.ReportError("cannot define view on non object and non collection media types")
return
}
if mt.Views == nil {
mt.Views = make(map[string]*design.ViewDefinition)
} else {
if _, ok := mt.Views[name]; ok {
dslengine.ReportError("multiple definitions for view %#v in media type %#v", name, mt.TypeName)
return
}
}
at := &design.AttributeDefinition{}
ok := false
if len(apidsl) > 0 {
ok = dslengine.Execute(apidsl[0], at)
} else if mt.Type.IsArray() {
// inherit view from collection element if present
elem := mt.Type.ToArray().ElemType
if elem != nil {
if pa, ok2 := elem.Type.(*design.MediaTypeDefinition); ok2 {
if v, ok2 := pa.Views[name]; ok2 {
at = v.AttributeDefinition
ok = true
} else {
dslengine.ReportError("unknown view %#v", name)
return
}
}
}
}
if ok {
view, err := buildView(name, mt, at)
if err != nil {
dslengine.ReportError(err.Error())
return
}
mt.Views[name] = view
}
case *design.AttributeDefinition:
def.View = name
default:
dslengine.IncompatibleDSL()
}
}