Exemple #1
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)
}
Exemple #2
0
// userTypeMarshalerFuncName returns the name for the given media type marshaler function.
func userTypeMarshalerFuncName(u *design.UserTypeDefinition) string {
	return fmt.Sprintf("Marshal%s", GoTypeName(u, u.AllRequired(), 0))
}