<commit_before>package main

import (
	"fmt"
	"log"
	"strings"
	"unicode"
)

const (
	read = 1 << iota
	write
	dropCtor
	embedded
)

type StructDefaults struct {
	BaseRepr
	Constructor string
}

type StructRepr struct {
	StructDefaults
	Name   string
	Fields []*StructFieldRepr
}

type StructFieldRepr struct {
	BaseFieldRepr
	Name        string // Field name
	Type        string // Field data type
	Tag         string // Typical struct field tags
	Read        string // Method name for reads
	Write       string // Method name for writes
	DefaultExpr string // Default expression
}

var structDefaults StructDefaults

func init() {
	structDefaults.flags = 0
}

func (self *StructDefaults) gatherFlags(cgText string) (string, error) {
	cgText, flags, _, err := self.genericGatherFlags(cgText, self == &structDefaults)
	if err != nil {
		return cgText, err
	}

	for _, flag := range flags {
		switch strings.ToLower(flag.Name) {
		case "drop_json": // Do not generate JSON marshaling methods
			if err = self.doBooleanFlag(flag, dropJson); err != nil {
				return cgText, err
			}

		case "drop_ctor": // Do not generate default constructor function
			if err = self.doBooleanFlag(flag, dropCtor); err != nil {
				return cgText, err
			}

		case "ctor_name": // Custom name for the default constructor
			if self == (&structDefaults) { // Only if we're not setting defaults
				if self.Constructor, err = flag.getIdent(); err != nil {
					return cgText, err

				} else {
					continue
				}
			}

			fallthrough // Not available as a default value

		default:
			return cgText, fmt.Errorf("Unknown flag %q", flag.Name)
		}
	}

	return cgText, nil
}

func (self *StructRepr) GetPrivateTypeName() string {
	return "private_" + self.getUniqueId()
}
func (self *StructRepr) GetJSONTypeName() string {
	return "json_" + self.getUniqueId()
}
func (self *StructRepr) GetCtorName() string {
	if len(self.Constructor) == 0 {
		return "New" + strings.Title(self.Name)
	}
	return self.Constructor
}

func (self *StructRepr) DoCtor() bool { return self.flags&dropCtor == 0 }
func (self *StructRepr) DoJson() bool { return self.flags&dropJson == 0 }

func (self *StructFieldRepr) DoRead() bool  { return len(self.Read) > 0 }
func (self *StructFieldRepr) DoWrite() bool { return len(self.Write) > 0 }
func (self *StructFieldRepr) DoDefaultExpr() bool {
	return len(self.DefaultExpr) > 0
}
func (self *StructFieldRepr) IsEmbedded() bool {
	return self.flags&embedded == embedded
}
func (self *StructFieldRepr) IsPrivate() bool {
	return !self.IsPublic() && !self.IsEmbedded()
}
func (self *StructFieldRepr) IsPublic() bool {
	return self.flags&(read|write) == (read | write)
}
func (self *StructFieldRepr) GetSpaceAndTag() string {
	if len(self.Tag) > 0 {
		return fmt.Sprintf(" `%s`", self.Tag)
	}
	return ""
}

// embedded fields use the `.Name` to store the embedded type. Since the type
// also serves as a name, the leading '*' may need to be stripped away.
func (self *StructFieldRepr) GetNameMaybeType() string {
	return strings.TrimLeft(self.Name, "*")
}

func (self *FileData) doStructDefaults(cgText string) (string, error) {
	return structDefaults.gatherFlags(cgText)
}

func (self *FileData) doStruct(cgText string, docs []string) (string, string, error) {
	var err error

	strct := StructRepr{
		StructDefaults: structDefaults, // copy of current defaults
	}
	strct.StructDefaults.Base.docs = docs

	if !unicode.IsSpace(rune(cgText[0])) {
		return cgText, "",
			fmt.Errorf("@struct is expected to be followed by a space and the name.")
	}

	cgText, foundNewline := trimLeftCheckNewline(cgText)
	if foundNewline {
		return cgText, "",
			fmt.Errorf("The name must be on the same line as the @enum")
	}

	if cgText, strct.Name, err = getIdent(cgText); err != nil {
		return cgText, strct.Name, err
	}

	if cgText, err = strct.gatherFlags(cgText); err != nil {
		return cgText, strct.Name, err
	}
<commit_msg>fixed word in error message<commit_after>package main

import (
	"fmt"
	"log"
	"strings"
	"unicode"
)

const (
	read = 1 << iota
	write
	dropCtor
	embedded
)

type StructDefaults struct {
	BaseRepr
	Constructor string
}

type StructRepr struct {
	StructDefaults
	Name   string
	Fields []*StructFieldRepr
}

type StructFieldRepr struct {
	BaseFieldRepr
	Name        string // Field name
	Type        string // Field data type
	Tag         string // Typical struct field tags
	Read        string // Method name for reads
	Write       string // Method name for writes
	DefaultExpr string // Default expression
}

var structDefaults StructDefaults

func init() {
	structDefaults.flags = 0
}

func (self *StructDefaults) gatherFlags(cgText string) (string, error) {
	cgText, flags, _, err := self.genericGatherFlags(cgText, self == &structDefaults)
	if err != nil {
		return cgText, err
	}

	for _, flag := range flags {
		switch strings.ToLower(flag.Name) {
		case "drop_json": // Do not generate JSON marshaling methods
			if err = self.doBooleanFlag(flag, dropJson); err != nil {
				return cgText, err
			}

		case "drop_ctor": // Do not generate default constructor function
			if err = self.doBooleanFlag(flag, dropCtor); err != nil {
				return cgText, err
			}

		case "ctor_name": // Custom name for the default constructor
			if self == (&structDefaults) { // Only if we're not setting defaults
				if self.Constructor, err = flag.getIdent(); err != nil {
					return cgText, err

				} else {
					continue
				}
			}

			fallthrough // Not available as a default value

		default:
			return cgText, fmt.Errorf("Unknown flag %q", flag.Name)
		}
	}

	return cgText, nil
}

func (self *StructRepr) GetPrivateTypeName() string {
	return "private_" + self.getUniqueId()
}
func (self *StructRepr) GetJSONTypeName() string {
	return "json_" + self.getUniqueId()
}
func (self *StructRepr) GetCtorName() string {
	if len(self.Constructor) == 0 {
		return "New" + strings.Title(self.Name)
	}
	return self.Constructor
}

func (self *StructRepr) DoCtor() bool { return self.flags&dropCtor == 0 }
func (self *StructRepr) DoJson() bool { return self.flags&dropJson == 0 }

func (self *StructFieldRepr) DoRead() bool  { return len(self.Read) > 0 }
func (self *StructFieldRepr) DoWrite() bool { return len(self.Write) > 0 }
func (self *StructFieldRepr) DoDefaultExpr() bool {
	return len(self.DefaultExpr) > 0
}
func (self *StructFieldRepr) IsEmbedded() bool {
	return self.flags&embedded == embedded
}
func (self *StructFieldRepr) IsPrivate() bool {
	return !self.IsPublic() && !self.IsEmbedded()
}
func (self *StructFieldRepr) IsPublic() bool {
	return self.flags&(read|write) == (read | write)
}
func (self *StructFieldRepr) GetSpaceAndTag() string {
	if len(self.Tag) > 0 {
		return fmt.Sprintf(" `%s`", self.Tag)
	}
	return ""
}

// embedded fields use the `.Name` to store the embedded type. Since the type
// also serves as a name, the leading '*' may need to be stripped away.
func (self *StructFieldRepr) GetNameMaybeType() string {
	return strings.TrimLeft(self.Name, "*")
}

func (self *FileData) doStructDefaults(cgText string) (string, error) {
	return structDefaults.gatherFlags(cgText)
}

func (self *FileData) doStruct(cgText string, docs []string) (string, string, error) {
	var err error

	strct := StructRepr{
		StructDefaults: structDefaults, // copy of current defaults
	}
	strct.StructDefaults.Base.docs = docs

	if !unicode.IsSpace(rune(cgText[0])) {
		return cgText, "",
			fmt.Errorf("@struct is expected to be followed by a space and the name.")
	}

	cgText, foundNewline := trimLeftCheckNewline(cgText)
	if foundNewline {
		return cgText, "",
			fmt.Errorf("The name must be on the same line as the @struct")
	}

	if cgText, strct.Name, err = getIdent(cgText); err != nil {
		return cgText, strct.Name, err
	}

	if cgText, err = strct.gatherFlags(cgText); err != nil {
		return cgText, strct.Name, err
	}

{'fraction_non_alphanumeric': 0.1177522349936143, 'fraction_numerical': 0.002554278416347382, 'mean_word_length': 3.8309685379395435, 'pattern_counts': {'":': 6, '<': 7, '<?xml version=': 0, '>': 11, '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': '2293126', 'n_tokens_mistral': 2761, 'n_tokens_neox': 2552, 'n_words': 806}