Exemple #1
0
func okResp(a *design.ActionDefinition) map[string]interface{} {
	var ok *design.ResponseDefinition
	for _, resp := range a.Responses {
		if resp.Status == 200 {
			ok = resp
			break
		}
	}
	if ok == nil {
		return nil
	}
	var mt *design.MediaTypeDefinition
	var ok2 bool
	if mt, ok2 = design.Design.MediaTypes[ok.MediaType]; !ok2 {
		return nil
	}
	typeref := codegen.GoTypeRef(mt, 1)
	if strings.HasPrefix(typeref, "*") {
		typeref = "&app." + typeref[1:]
	} else {
		typeref = "app." + typeref
	}
	return map[string]interface{}{
		"Name":             ok.Name,
		"HasMultipleViews": len(mt.Views) > 1,
		"GoType":           codegen.GoNativeType(mt),
		"TypeRef":          typeref,
	}
}
Exemple #2
0
// gotTypeRefExt computes the type reference for a type in a different package.
func goTypeRefExt(t design.DataType, tabs int, pkg string) string {
	ref := codegen.GoTypeRef(t, nil, tabs)
	if strings.HasPrefix(ref, "*") {
		return fmt.Sprintf("%s.%s", pkg, ref[1:])
	}
	return fmt.Sprintf("%s.%s", pkg, ref)
}
Exemple #3
0
func okResp(a *design.ActionDefinition, v string) map[string]interface{} {
	var ok *design.ResponseDefinition
	for _, resp := range a.Responses {
		if resp.Status == 200 {
			ok = resp
			break
		}
	}
	if ok == nil {
		return nil
	}
	var mt *design.MediaTypeDefinition
	var ok2 bool
	if mt, ok2 = design.Design.MediaTypes[design.CanonicalIdentifier(ok.MediaType)]; !ok2 {
		return nil
	}
	var pkg string
	if v == "" {
		pkg = TargetPackage
	} else {
		pkg = codegen.VersionPackage(v)
	}
	name := codegen.GoTypeRef(mt, mt.AllRequired(), 1)
	var pointer string
	if strings.HasPrefix(name, "*") {
		name = name[1:]
		pointer = "*"
	}
	typeref := fmt.Sprintf("%s%s.%s", pointer, pkg, name)
	if strings.HasPrefix(typeref, "*") {
		typeref = "&" + typeref[1:]
	}
	return map[string]interface{}{
		"Name":             ok.Name,
		"HasMultipleViews": len(mt.Views) > 1,
		"GoType":           codegen.GoNativeType(mt),
		"TypeRef":          typeref,
	}
}
Exemple #4
0
				})

				Context("unmarshaler code", func() {
					BeforeEach(func() {
						unmarshaler = codegen.TypeUnmarshaler(o, false, "", context, source, target)
						data := map[string]interface{}{
							"raw": `interface{}(map[string]interface{}{
								"baz": map[string]interface{}{
									"foo": 345.0,
									"bar":[]interface{}{[]interface{}{1.0,2.0,3.0}},
								},
								"faz": 2.0,
							})`,
							"source":     unmarshaler,
							"target":     target,
							"targetType": codegen.GoTypeRef(o, nil, 1),
						}
						err := tmpl.Execute(tmpFile, data)
						Ω(err).ShouldNot(HaveOccurred())
					})
					It("compiles", func() {
						Ω(string(out)).Should(BeEmpty())

						bin := "codegen"
						if runtime.GOOS == "windows" {
							bin += ".exe"
						}
						cmd := exec.Command(filepath.FromSlash(fmt.Sprintf("./%s", bin)))
						cmd.Env = []string{fmt.Sprintf("PATH=%s", filepath.Join(gopath, "bin"))}
						cmd.Dir = srcDir
						code, err := cmd.CombinedOutput()