<commit_before>package codegen

import (
	"bytes"
	"encoding/json"
	"fmt"
	"sort"
	"strings"
	"text/template"
	"unicode"

	"github.com/raphael/goa/design"
)

var (
	// TempCount holds the value appended to variable names to make them unique.
	TempCount int

	mArrayT          *template.Template
	mObjectT         *template.Template
	mHashT           *template.Template
	mLinkT           *template.Template
	mCollectionT     *template.Template
	unmPrimitiveT    *template.Template
	unmArrayT        *template.Template
	unmObjectOrUserT *template.Template
	unmHashT         *template.Template
)

//  init instantiates the templates.
func init() {
	var err error
	fm := template.FuncMap{
		"marshalAttribute":   attributeMarshalerR,
		"marshalMediaType":   mediaTypeMarshalerR,
		"unmarshalAttribute": attributeUnmarshalerR,
		"validate":           validationCheckerR,
		"gotypename":         GoTypeName,
		"gotyperef":          GoTypeRef,
		"goify":              Goify,
		"gonative":           GoNativeType,
		"tabs":               Tabs,
		"add":                func(a, b int) int { return a + b },
		"tempvar":            Tempvar,
		"has":                has,
	}
	if mArrayT, err = template.New("array marshaler").Funcs(fm).Parse(mArrayTmpl); err != nil {
		panic(err)
	}
	if mObjectT, err = template.New("object marshaler").Funcs(fm).Parse(mObjectTmpl); err != nil {
		panic(err)
	}
	if mHashT, err = template.New("hash marshaler").Funcs(fm).Parse(mHashTmpl); err != nil {
		panic(err)
	}
	if mLinkT, err = template.New("links marshaler").Funcs(fm).Parse(mLinkTmpl); err != nil {
		panic(err)
	}
	if mCollectionT, err = template.New("collection marshaler").Funcs(fm).Parse(mCollectionTmpl); err != nil {
		panic(err)
	}
	if unmPrimitiveT, err = template.New("primitive unmarshaler").Funcs(fm).Parse(unmPrimitiveTmpl); err != nil {
		panic(err)
	}
	if unmArrayT, err = template.New("array unmarshaler").Funcs(fm).Parse(unmArrayTmpl); err != nil {
		panic(err)
	}
	if unmObjectOrUserT, err = template.New("object unmarshaler").Funcs(fm).Parse(unmObjectTmpl); err != nil {
		panic(err)
	}
	if unmHashT, err = template.New("hash unmarshaler").Funcs(fm).Parse(unmHashTmpl); err != nil {
		panic(err)
	}
}

// TypeMarshaler produces the Go code that initiliazes the variable named target which is an
// interface{} with the content of the variable named source which contains an instance of the type
// data structure.
// The code takes care of rendering media types according to the view defined on the attribute
// if any. It also renders media type links. Finally it validates the results using any type
// validation that is defined on the type attributes (if the type contains attributes).
// The generated code assumes that there is a variable called "err" of type error that it can use
// to record errors.
func TypeMarshaler(t design.DataType, context, source, target string) string {
	return typeMarshalerR(t, context, source, target, 1)
}
func typeMarshalerR(t design.DataType, context, source, target string, depth int) string {
	switch actual := t.(type) {
	case design.Primitive:
		return fmt.Sprintf("%s%s = %s", Tabs(depth), target, source)
	case *design.Array:
		return arrayMarshalerR(actual, context, source, target, depth)
	case *design.Hash:
		return hashMarshalerR(actual, context, source, target, depth)
	case design.Object, *design.UserTypeDefinition:
		return objectMarshalerR(actual.ToObject(), nil, context, source, target, depth)
	default:
		// this should never get called with a MediaType, MediaTypeMarshaler should be
		// called instead so the view is properly taken into account.
		panic(actual)
	}
}

// AttributeMarshaler produces the Go code that initiliazes the variable named with the value of
// target which holds an interface{} with the content of the variable named with the value of source
// which contains an instance of the attribute type data structure. The attribute view is used to
// render child attributes if there are any. As with TypeMarshaler the code renders media type links
// and runs any validation defined on the type definition.
//
// The generated code assumes that there is a variable called "err" of type error that it can use
// to record errors.
func AttributeMarshaler(att *design.AttributeDefinition, context, source, target string) string {
	return attributeMarshalerR(att, context, source, target, 1)
}
func attributeMarshalerR(att *design.AttributeDefinition, context, source, target string, depth int) string {
	var marshaler string
	switch actual := att.Type.(type) {
	case *design.MediaTypeDefinition:
		marshaler = mediaTypeMarshalerR(actual, context, source, target, att.View, depth)
	case design.Object:
		marshaler = objectMarshalerR(actual, att.AllRequired(), context, source, target, depth)
	default:
		marshaler = typeMarshalerR(att.Type, context, source, target, depth)
	}
	validation := ValidationChecker(att, source, context)
	if validation != "" {
		if !strings.HasPrefix(strings.TrimLeft(" \t\n", marshaler), "if err == nil {") {
			return validation + "\n	if err == nil {\n" + marshaler + "\n	}"
		}
		return validation + marshaler
	}
	return marshaler
}

// ArrayMarshaler produces the Go code that marshals an array for rendering.
// source is the name of the variable that contains the array value and target the name of the<commit_msg>Fix tabulation of validation checks<commit_after>package codegen

import (
	"bytes"
	"encoding/json"
	"fmt"
	"sort"
	"strings"
	"text/template"
	"unicode"

	"github.com/raphael/goa/design"
)

var (
	// TempCount holds the value appended to variable names to make them unique.
	TempCount int

	mArrayT          *template.Template
	mObjectT         *template.Template
	mHashT           *template.Template
	mLinkT           *template.Template
	mCollectionT     *template.Template
	unmPrimitiveT    *template.Template
	unmArrayT        *template.Template
	unmObjectOrUserT *template.Template
	unmHashT         *template.Template
)

//  init instantiates the templates.
func init() {
	var err error
	fm := template.FuncMap{
		"marshalAttribute":   attributeMarshalerR,
		"marshalMediaType":   mediaTypeMarshalerR,
		"unmarshalAttribute": attributeUnmarshalerR,
		"validate":           validationCheckerR,
		"gotypename":         GoTypeName,
		"gotyperef":          GoTypeRef,
		"goify":              Goify,
		"gonative":           GoNativeType,
		"tabs":               Tabs,
		"add":                func(a, b int) int { return a + b },
		"tempvar":            Tempvar,
		"has":                has,
	}
	if mArrayT, err = template.New("array marshaler").Funcs(fm).Parse(mArrayTmpl); err != nil {
		panic(err)
	}
	if mObjectT, err = template.New("object marshaler").Funcs(fm).Parse(mObjectTmpl); err != nil {
		panic(err)
	}
	if mHashT, err = template.New("hash marshaler").Funcs(fm).Parse(mHashTmpl); err != nil {
		panic(err)
	}
	if mLinkT, err = template.New("links marshaler").Funcs(fm).Parse(mLinkTmpl); err != nil {
		panic(err)
	}
	if mCollectionT, err = template.New("collection marshaler").Funcs(fm).Parse(mCollectionTmpl); err != nil {
		panic(err)
	}
	if unmPrimitiveT, err = template.New("primitive unmarshaler").Funcs(fm).Parse(unmPrimitiveTmpl); err != nil {
		panic(err)
	}
	if unmArrayT, err = template.New("array unmarshaler").Funcs(fm).Parse(unmArrayTmpl); err != nil {
		panic(err)
	}
	if unmObjectOrUserT, err = template.New("object unmarshaler").Funcs(fm).Parse(unmObjectTmpl); err != nil {
		panic(err)
	}
	if unmHashT, err = template.New("hash unmarshaler").Funcs(fm).Parse(unmHashTmpl); err != nil {
		panic(err)
	}
}

// TypeMarshaler produces the Go code that initiliazes the variable named target which is an
// interface{} with the content of the variable named source which contains an instance of the type
// data structure.
// The code takes care of rendering media types according to the view defined on the attribute
// if any. It also renders media type links. Finally it validates the results using any type
// validation that is defined on the type attributes (if the type contains attributes).
// The generated code assumes that there is a variable called "err" of type error that it can use
// to record errors.
func TypeMarshaler(t design.DataType, context, source, target string) string {
	return typeMarshalerR(t, context, source, target, 1)
}
func typeMarshalerR(t design.DataType, context, source, target string, depth int) string {
	switch actual := t.(type) {
	case design.Primitive:
		return fmt.Sprintf("%s%s = %s", Tabs(depth), target, source)
	case *design.Array:
		return arrayMarshalerR(actual, context, source, target, depth)
	case *design.Hash:
		return hashMarshalerR(actual, context, source, target, depth)
	case design.Object, *design.UserTypeDefinition:
		return objectMarshalerR(actual.ToObject(), nil, context, source, target, depth)
	default:
		// this should never get called with a MediaType, MediaTypeMarshaler should be
		// called instead so the view is properly taken into account.
		panic(actual)
	}
}

// AttributeMarshaler produces the Go code that initiliazes the variable named with the value of
// target which holds an interface{} with the content of the variable named with the value of source
// which contains an instance of the attribute type data structure. The attribute view is used to
// render child attributes if there are any. As with TypeMarshaler the code renders media type links
// and runs any validation defined on the type definition.
//
// The generated code assumes that there is a variable called "err" of type error that it can use
// to record errors.
func AttributeMarshaler(att *design.AttributeDefinition, context, source, target string) string {
	return attributeMarshalerR(att, context, source, target, 1)
}
func attributeMarshalerR(att *design.AttributeDefinition, context, source, target string, depth int) string {
	var marshaler string
	switch actual := att.Type.(type) {
	case *design.MediaTypeDefinition:
		marshaler = mediaTypeMarshalerR(actual, context, source, target, att.View, depth)
	case design.Object:
		marshaler = objectMarshalerR(actual, att.AllRequired(), context, source, target, depth)
	default:
		marshaler = typeMarshalerR(att.Type, context, source, target, depth)
	}
	validation := ValidationChecker(att, source, context)
	if validation != "" {
		if !strings.HasPrefix(strings.TrimLeft(" \t\n", marshaler), "if err == nil {") {
			return fmt.Sprintf(
				"%s\n%sif err == nil {\n%s\n%s}",
				validation,
				Tabs(depth),
				marshaler,
				Tabs(depth),
			)
		}
		return validation + marshaler
	}
	return marshaler
}

// ArrayMarshaler produces the Go code that marshals an array for rendering.
// source is the name of the variable that contains the array value and target the name of the
{'fraction_non_alphanumeric': 0.10432593697557337, 'fraction_numerical': 0.00037292560134253216, 'mean_word_length': 3.9183860614397066, 'pattern_counts': {'":': 24, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6308470', 'n_tokens_mistral': 3342, 'n_tokens_neox': 3202, 'n_words': 1090}