<commit_before>package fields

import (
	"encoding/json"
	"github.com/cjtoolkit/form"
	"strings"
)

type StringSlice struct {
	Name     string    // Mandatory
	Label    string    // Mandatory
	Norm     *[]string // Mandatory
	Model    *[]string // Mandatory
	Err      *error    // Mandatory
	Required bool      // Mandatory
	Extra    func()
}

type stringSliceJson struct {
	Type     string `json:"type"`
	Name     string `json:"name"`
	Required bool   `json:"required"`
	Success  bool   `json:"success"`
	Error    string `json:"error,omitempty"`
}

func (s StringSlice) MarshalJSON() ([]byte, error) {
	return json.Marshal(stringSliceJson{
		Type:     "string_slice",
		Name:     s.Name,
		Required: s.Required,
		Success:  nil == *s.Err,
		Error:    getMessageFromError(*s.Err),
	})
}

func (s StringSlice) PreCheck() {
	switch {
	case "" == strings.TrimSpace(s.Name):
		panic(form.ErrorPreCheck("StringSlice Field: Name cannot be empty string"))
	case "" == strings.TrimSpace(s.Label):
		panic(form.ErrorPreCheck("StringSlice Field: " + s.Name + ": Label cannot be empty string"))
	case nil == s.Norm:
		panic(form.ErrorPreCheck("StringSlice Field: " + s.Name + ": Norm cannot be nil value"))
	case nil == s.Model:
		panic(form.ErrorPreCheck("StringSlice Field: " + s.Name + ": Model cannot be nil value"))
	case nil == s.Err:
		panic(form.ErrorPreCheck("StringSlice Field: " + s.Name + ": Err cannot be nil value"))
	}
}

func (s StringSlice) GetErrorPtr() *error {
	return s.Err
}

func (s StringSlice) PopulateNorm(values form.ValuesInterface) {
	*s.Norm = values.GetAll(s.Name)
}

func (s StringSlice) Transform() {
	*s.Norm = nil
	for _, str := range *s.Model {
		*s.Norm = append(*s.Norm, strings.TrimSpace(str))
	}
}

func (s StringSlice) ReverseTransform() {
	*s.Model = nil
	for _, str := range *s.Norm {
		*s.Model = append(*s.Model, strings.TrimSpace(str))
	}
}

func (s StringSlice) ValidateModel() {
	s.validateRequired()
	execFnIfNotNil(s.Extra)
}

func (s StringSlice) validateRequired() {
	switch {
	case !s.Required:
		return<commit_msg>Having sort might be a good idea<commit_after>package fields

import (
	"encoding/json"
	"github.com/cjtoolkit/form"
	"strings"
	"sort"
)

type StringSlice struct {
	Name     string    // Mandatory
	Label    string    // Mandatory
	Norm     *[]string // Mandatory
	Model    *[]string // Mandatory
	Err      *error    // Mandatory
	Required bool      // Mandatory
	Extra    func()
}

type stringSliceJson struct {
	Type     string `json:"type"`
	Name     string `json:"name"`
	Required bool   `json:"required"`
	Success  bool   `json:"success"`
	Error    string `json:"error,omitempty"`
}

func (s StringSlice) MarshalJSON() ([]byte, error) {
	return json.Marshal(stringSliceJson{
		Type:     "string_slice",
		Name:     s.Name,
		Required: s.Required,
		Success:  nil == *s.Err,
		Error:    getMessageFromError(*s.Err),
	})
}

func (s StringSlice) PreCheck() {
	switch {
	case "" == strings.TrimSpace(s.Name):
		panic(form.ErrorPreCheck("StringSlice Field: Name cannot be empty string"))
	case "" == strings.TrimSpace(s.Label):
		panic(form.ErrorPreCheck("StringSlice Field: " + s.Name + ": Label cannot be empty string"))
	case nil == s.Norm:
		panic(form.ErrorPreCheck("StringSlice Field: " + s.Name + ": Norm cannot be nil value"))
	case nil == s.Model:
		panic(form.ErrorPreCheck("StringSlice Field: " + s.Name + ": Model cannot be nil value"))
	case nil == s.Err:
		panic(form.ErrorPreCheck("StringSlice Field: " + s.Name + ": Err cannot be nil value"))
	}
}

func (s StringSlice) GetErrorPtr() *error {
	return s.Err
}

func (s StringSlice) PopulateNorm(values form.ValuesInterface) {
	*s.Norm = values.GetAll(s.Name)
}

func (s StringSlice) Transform() {
	*s.Norm = nil
	for _, str := range *s.Model {
		*s.Norm = append(*s.Norm, strings.TrimSpace(str))
	}
	sort.Sort(sort.StringSlice{*s.Norm})
}

func (s StringSlice) ReverseTransform() {
	*s.Model = nil
	for _, str := range *s.Norm {
		*s.Model = append(*s.Model, strings.TrimSpace(str))
	}
	sort.Sort(sort.StringSlice{*s.Model})
}

func (s StringSlice) ValidateModel() {
	s.validateRequired()
	execFnIfNotNil(s.Extra)
}

func (s StringSlice) validateRequired() {
	switch {
	case !s.Required:
		return
{'fraction_non_alphanumeric': 0.14793741109530584, 'fraction_numerical': 0.0, 'mean_word_length': 3.7351290684624017, 'pattern_counts': {'":': 8, '<': 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': '5794058', 'n_tokens_mistral': 1483, 'n_tokens_neox': 1443, 'n_words': 372}