Exemplo n.º 1
0
Arquivo: types.go Projeto: cw2018/goa
// UserTypeUnmarshalerImpl returns the code implementing the user type unmarshaler function.
func UserTypeUnmarshalerImpl(u *design.UserTypeDefinition, context string) string {
	var required []string
	for _, v := range u.Validations {
		if r, ok := v.(*design.RequiredValidationDefinition); ok {
			required = r.Names
			break
		}
	}
	var impl string
	switch {
	case u.IsObject():
		impl = objectUnmarshalerR(u, required, context, "source", "target", 1)
	case u.IsArray():
		impl = arrayUnmarshalerR(u.ToArray(), context, "source", "target", 1)
	case u.IsHash():
		impl = hashUnmarshalerR(u.ToHash(), 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,
	}
	return RunTemplate(unmUserImplT, data)
}
Exemplo n.º 2
0
// 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)
}