本文整理汇总了Golang中github.com/ardielle/ardielle-go/rdl.TypeRegistry.BaseTypeName方法的典型用法代码示例。如果您正苦于以下问题:Golang TypeRegistry.BaseTypeName方法的具体用法?Golang TypeRegistry.BaseTypeName怎么用?Golang TypeRegistry.BaseTypeName使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/ardielle/ardielle-go/rdl.TypeRegistry
的用法示例。
在下文中一共展示了TypeRegistry.BaseTypeName方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: explodeURL
func explodeURL(reg rdl.TypeRegistry, r *rdl.Resource) string {
path := r.Path
params := ""
delim := ""
for _, v := range r.Inputs {
k := v.Name
gk := goName(string(k))
if v.PathParam {
//
if v.Type == "String" {
path = strings.Replace(path, "{"+string(k)+"}", "\" + "+gk+" + \"", -1)
} else {
path = strings.Replace(path, "{"+string(k)+"}", "\" + fmt.Sprint("+gk+") + \"", -1)
}
} else if v.QueryParam != "" {
qp := v.QueryParam
item := ""
if reg.IsArrayTypeName(v.Type) {
item = "encodeListParam(\"" + qp + "\"," + gk + ")"
} else {
baseType := reg.BaseTypeName(v.Type)
if v.Optional && baseType != "String" {
item = "encodeOptional" + string(baseType) + "Param(\"" + qp + "\", " + gk + ")"
} else {
def := goLiteral(v.Default, string(baseType))
if baseType == "Enum" {
def = "\"" + def + "\""
item = "encodeStringParam(\"" + qp + "\", " + gk + ".String(), " + def + ")"
} else {
item = "encode" + string(baseType) + "Param(\"" + qp + "\", " + strings.ToLower(string(baseType)) + "(" + gk + "), " + def + ")"
}
}
}
params += delim + item
delim = ", "
}
}
path = "\"" + path
if strings.HasSuffix(path, " + \"") {
path = path[0 : len(path)-4]
} else {
path += "\""
}
if params != "" {
path = path + " + encodeParams(" + params + ")"
}
return path
}
示例2: goHandlerBody
func goHandlerBody(reg rdl.TypeRegistry, name string, r *rdl.Resource, precise bool, prefixEnums bool) string {
s := ""
var fargs []string
bodyName := ""
for _, in := range r.Inputs {
name := "arg" + capitalize(string(in.Name))
if in.QueryParam != "" {
qname := in.QueryParam
if in.Optional || in.Default != nil {
s += goParamInit(reg, qname, name, in.Type, in.Default, in.Optional, precise, prefixEnums)
} else {
log.Println("RDL error: queryparam must either be optional or have a default value")
}
fargs = append(fargs, name)
} else if in.PathParam {
bt := reg.BaseTypeName(in.Type)
switch bt {
case "Enum":
s += fmt.Sprintf("\t%s := New%s(context.Params[%q])\n", name, in.Type, in.Name)
case "Int32", "Int64", "Int16", "Int8":
if precise {
s += fmt.Sprintf("\t%s := %s(intFromString(context.Params[%q]))\n", name, in.Type, in.Name)
} else {
s += fmt.Sprintf("\t%s := intFromString(context.Params[%q])\n", name, in.Name)
}
case "Float32", "Float64":
if precise {
s += fmt.Sprintf("\t%s := %s(floatFromString(context.Params[%q]))\n", name, in.Type, in.Name)
} else {
s += fmt.Sprintf("\t%s := floatFromString(context.Params[%q])\n", name, in.Name)
}
default:
if precise && strings.ToLower(string(in.Type)) != "string" {
s += fmt.Sprintf("\t%s := %s(context.Params[%q])\n", name, in.Type, in.Name)
} else {
s += fmt.Sprintf("\t%s := context.Params[%q]\n", name, in.Name)
}
}
fargs = append(fargs, name)
} else if in.Header != "" {
hname := in.Header
def := ""
if in.Default != nil {
switch v := in.Default.(type) {
case string:
def = fmt.Sprintf("%q", v)
default:
panic(fmt.Sprintf("implement me, default value: %v", in))
}
s += "\t" + name + "Optional := " + def + "\n"
s += fmt.Sprintf("\t%s := rdl.HeaderParam(request, %q, %sOptional)\n", name, hname, name)
} else if in.Optional {
s += fmt.Sprintf("\t%s := rdl.OptionalHeaderParam(request, %q)\n", name, hname)
} else {
s += fmt.Sprintf("\t%s := rdl.HeaderParam(request, %q, \"\")\n", name, hname)
}
fargs = append(fargs, name)
} else {
bodyName = name
s += "\tbody, oserr := ioutil.ReadAll(request.Body)\n"
s += "\tif oserr != nil {\n"
s += "\t\trdl.JSONResponse(writer, http.StatusBadRequest, rdl.ResourceError{Code: http.StatusBadRequest, Message: \"Bad request: \" + oserr.Error()})\n"
s += "\t\treturn\n"
s += "\t}\n"
pgtype := goType(reg, in.Type, false, "", "", precise, true)
s += "\tvar " + bodyName + " " + pgtype + "\n"
s += "\toserr = json.Unmarshal(body, &" + bodyName + ")\n"
s += "\tif oserr != nil {\n"
s += "\t\trdl.JSONResponse(writer, http.StatusBadRequest, rdl.ResourceError{Code: http.StatusBadRequest, Message: \"Bad request: \" + oserr.Error()})\n"
s += "\t\treturn\n"
s += "\t}\n"
fargs = append(fargs, bodyName)
}
}
if r.Auth != nil {
if r.Auth.Authenticate {
s += authenticateTemplate
} else if r.Auth.Action != "" && r.Auth.Resource != "" {
resource := r.Auth.Resource
i := strings.Index(resource, "{")
for i >= 0 {
j := strings.Index(resource[i:], "}")
if j < 0 {
break
}
j += i
val := "string(arg" + capitalize(resource[i+1:j]) + ")"
resource = resource[0:i] + "\" + " + val + " + \"" + resource[j+1:]
i = strings.Index(resource, "{")
}
resource = "\"" + resource
if strings.HasSuffix(resource, "+ \"") {
resource = resource[0 : len(resource)-3]
} else {
resource = resource + "\""
}
if strings.HasPrefix(resource, "\"\" + ") {
resource = resource[5:]
}
s += fmt.Sprintf(authorizeTemplate, r.Auth.Action, resource)
//.........这里部分代码省略.........