gotygen/pkg/document/struct.go

71 lines
1.1 KiB
Go

package document
import (
"fmt"
"strings"
"golang.org/x/exp/maps"
"githouse.ru/macrox/gotygen/pkg/utils"
)
type structType struct {
fields map[string]*Field
pointer bool
}
func (s *structType) String() string {
return fmt.Sprintf(
`struct{
%s
}`,
strings.Join(
utils.SliceTransform(maps.Values(s.fields), func(field *Field, _ int) string {
return field.String()
}),
"\n\t",
),
)
}
func (s *structType) IsNullable() bool {
return s.pointer
}
func (s *structType) Nullable(v ...bool) Type {
if len(v) > 0 {
s.pointer = v[0]
} else {
s.pointer = true
}
return s
}
func (s *structType) Len() int {
return len(s.fields)
}
func (s *structType) Keys() []string {
return maps.Keys(s.fields)
}
func (s *structType) Fields() []*Field {
return maps.Values(s.fields)
}
func (s *structType) Get(key string) (field *Field, ok bool) {
field, ok = s.fields[key]
return
}
func (s *structType) Set(field *Field) Struct {
s.fields[field.Key] = field
return s
}
func NewStruct() Struct {
return &structType{
fields: make(map[string]*Field),
}
}