gotygen/pkg/document/primitives.go

235 lines
3.1 KiB
Go

package document
import "fmt"
type anyType struct {
}
func (i *anyType) String() string {
return "any"
}
func (i *anyType) Nullable(v ...bool) Type {
return i
}
func (i *anyType) IsNullable() bool {
return false
}
type refType struct {
id string
pointer bool
}
func (r *refType) String() string {
if r.pointer {
return "*" + r.id
} else {
return r.id
}
}
func (r *refType) Id() string {
return r.id
}
func (r *refType) IsNullable() bool {
return r.pointer
}
func (r *refType) Nullable(v ...bool) Type {
if len(v) > 0 {
r.pointer = v[0]
} else {
r.pointer = true
}
return r
}
type intType struct {
pointer bool
}
func (i *intType) String() string {
if i.pointer {
return "*int"
} else {
return "int"
}
}
func (i *intType) Nullable(v ...bool) Type {
if len(v) > 0 {
i.pointer = v[0]
} else {
i.pointer = true
}
return i
}
func (i *intType) IsNullable() bool {
return i.pointer
}
type floatType struct {
pointer bool
}
func (f *floatType) String() string {
if f.pointer {
return "*float64"
} else {
return "float64"
}
}
func (f *floatType) Nullable(v ...bool) Type {
if len(v) > 0 {
f.pointer = v[0]
} else {
f.pointer = true
}
return f
}
func (f *floatType) IsNullable() bool {
return f.pointer
}
type boolType struct {
pointer bool
}
func (b *boolType) String() string {
if b.pointer {
return "*bool"
} else {
return "bool"
}
}
func (b *boolType) Nullable(v ...bool) Type {
if len(v) > 0 {
b.pointer = v[0]
} else {
b.pointer = true
}
return b
}
func (b *boolType) IsNullable() bool {
return b.pointer
}
type stringType struct {
pointer bool
}
func (s *stringType) String() string {
if s.pointer {
return "*string"
} else {
return "string"
}
}
func (s *stringType) Nullable(v ...bool) Type {
if len(v) > 0 {
s.pointer = v[0]
} else {
s.pointer = true
}
return s
}
func (s *stringType) IsNullable() bool {
return s.pointer
}
type arrayType struct {
element Type
}
func (a *arrayType) String() string {
return fmt.Sprintf("[]%s", a.element)
}
func (a *arrayType) IsNullable() bool {
return false
}
func (a *arrayType) Nullable(v ...bool) Type {
return a
}
func (a *arrayType) Element(e ...Type) Type {
if len(e) > 0 && e[0] != nil {
a.element = e[0]
}
return a.element
}
type mapType struct {
key Type
value Type
}
func (m *mapType) String() string {
return fmt.Sprintf("map[%s]%s", m.key, m.value)
}
func (m *mapType) IsNullable() bool {
return false
}
func (m *mapType) Nullable(v ...bool) Type {
return m
}
func (m *mapType) Key(k ...Type) Type {
if len(k) > 0 && k[0] != nil {
m.key = k[0]
}
return m.key
}
func (m *mapType) Value(v ...Type) Type {
if len(v) > 0 && v[0] != nil {
m.value = v[0]
}
return m.value
}
func NewAny() Any {
return new(anyType)
}
func NewRef(id string) Any {
return &refType{id: id}
}
func NewInt() Int {
return new(intType)
}
func NewFloat() Float {
return new(floatType)
}
func NewBool() Bool {
return new(boolType)
}
func NewString() String {
return new(stringType)
}
func NewArray(element Type) Array {
return &arrayType{element: element}
}
func NewMap(key, value Type) Map {
return &mapType{key: key, value: value}
}