@@ -7,147 +7,114 @@ import (
77 "unsafe"
88)
99
10+ // BaseType makes up the most basic building blocks of all
11+ // primitive data types for HellDB. There is no differentiation
12+ // between composite or self sufficient types.
13+ //
14+ // The Name function is a meta function that returns the string
15+ // identifier for a type - in case we need a strongly typed interpreter
16+ // that supports operations for reducing or mapping over keys.
17+ //
18+ // SizeOf returns an unsigned integer for the number of bytes taken
19+ // up by a data member - it's statically defined for some types (ex. Boolean),
20+ // grows with size for some (ex. String, Collection) and is platform dependent
21+ // for others (ex. Int).
22+ //
23+ // String is another meta function returns a native string
24+ // representation for a type useful for taking snapshots (currently
25+ // unsupported) before shutdown or at random intervals.
26+ //
27+ // Native returns a native Go type representation for a BaseType. Compound
28+ // data types can also be represented such as heterogeneous arrays (Collection)
29+ // since they all implement the BaseType interface and required methods.
1030type BaseType interface {
1131 Name () string
1232 SizeOf () uint
1333 String () string
1434 Native () interface {}
1535}
1636
37+ // Int is a struct that implements BaseType for signed 64
38+ // bit integers.
1739type Int struct {
18- data int64
40+ Data int64 `json:"int"`
1941}
2042
21- type String struct {
22- data string
23- }
24-
25- type Boolean struct {
26- data bool
27- }
28-
29- type Collection struct {
30- data []BaseType
31- }
32-
33- /* -----------------int------------------ */
34-
35- func (i * Int ) Name () string {
36- return "integer"
37- }
38-
39- func (i * Int ) SizeOf () uint {
40- return uint (unsafe .Sizeof (i .data ))
41- }
42-
43- func (i * Int ) String () string {
44- return strconv .FormatInt (i .data , 10 )
45- }
46-
47- func (i * Int ) Native () interface {} {
48- return i .data
49- }
43+ func (i * Int ) Native () interface {} { return i .Data }
44+ func (i * Int ) Name () string { return "integer" }
45+ func NewInt (data int64 ) * Int { return & Int {Data : data } }
46+ func (i * Int ) SizeOf () uint { return uint (unsafe .Sizeof (i .Data )) }
47+ func (i * Int ) String () string { return strconv .FormatInt (i .Data , 10 ) }
5048
51- func NewInt ( data int64 ) * Int {
52- return & Int { data : data }
49+ type String struct {
50+ Data string `json:"string"`
5351}
5452
55- /* ----------------- str----------------- */
56-
57- func (s * String ) Name () string {
58- return "str"
59- }
53+ func ( s * String ) Name () string { return " str" }
54+ func ( s * String ) String () string { return s . Data }
55+ func (s * String ) Native () interface {} { return s . Data }
56+ func ( s * String ) SizeOf () uint { return uint ( len ( s . Data )) }
57+ func NewString ( data string ) * String { return & String { Data : data } }
6058
61- func ( s * String ) SizeOf () uint {
62- return uint ( len ( s . data ))
59+ type Boolean struct {
60+ Data bool `json:"bool"`
6361}
6462
65- func (s * String ) String () string {
66- return s .data
67- }
63+ func (b * Boolean ) SizeOf () uint { return 1 }
64+ func (b * Boolean ) Native () interface {} { return b .Data }
65+ func (b * Boolean ) Name () string { return "boolean" }
66+ func NewBoolean (data bool ) * Boolean { return & Boolean {Data : data } }
6867
69- func (s * String ) Native () interface {} {
70- return s .data
68+ func (b * Boolean ) String () string {
69+ if b .Data {
70+ return "true"
71+ } else {
72+ return "false"
73+ }
7174}
7275
73- func NewString ( data string ) * String {
74- return & String { data : data }
76+ type Collection struct {
77+ Data [] BaseType `json:"collection"`
7578}
7679
77- /* ----------------collection---------------- */
80+ func (c * Collection ) Name () string { return "collection" }
81+ func (c * Collection ) Native () interface {} { return serializeCollection (c ) }
82+ func NewCollection (data []BaseType ) * Collection { return & Collection {Data : data } }
7883
79- func (c * Collection ) Name () string {
80- return "collection"
84+ func serializeCollection (collection * Collection ) []interface {} {
85+ var list []interface {}
86+ for _ , val := range collection .Data {
87+ if c , ok := val .(* Collection ); ok {
88+ list = append (list , serializeCollection (c ))
89+ } else {
90+ list = append (list , val .Native ())
91+ }
92+ }
93+ return list
8194}
8295
8396func (c * Collection ) SizeOf () uint {
84- if l := uint (len (c .data )); l == 0 {
97+ if l := uint (len (c .Data )); l == 0 {
8598 return 0
8699 } else {
87100 var total uint = 0
88- for _ , item := range c .data {
101+ for _ , item := range c .Data {
89102 total += item .SizeOf ()
90103 }
91104 return total
92105 }
93106}
94107
95108func (c * Collection ) String () string {
96- if len (c .data ) == 0 {
109+ if len (c .Data ) == 0 {
97110 return "[]"
98111 } else {
99112 var b strings.Builder
100113 b .WriteString ("[ " )
101- for i := 0 ; i < len (c .data )- 1 ; i ++ {
102- _ , _ = fmt .Fprintf (& b , "%s, " , c .data [i ].String ())
114+ for i := 0 ; i < len (c .Data )- 1 ; i ++ {
115+ _ , _ = fmt .Fprintf (& b , "%s, " , c .Data [i ].String ())
103116 }
104- _ , _ = fmt .Fprintf (& b , "%s ]" , c .data [len (c .data )- 1 ].String ())
117+ _ , _ = fmt .Fprintf (& b , "%s ]" , c .Data [len (c .Data )- 1 ].String ())
105118 return b .String ()
106119 }
107120}
108-
109- func serializeCollection (collection * Collection ) []interface {} {
110- var list []interface {}
111- for _ , val := range collection .data {
112- if c , ok := val .(* Collection ); ok {
113- list = append (list , serializeCollection (c ))
114- } else {
115- list = append (list , val .Native ())
116- }
117- }
118- return list
119- }
120-
121- func (c * Collection ) Native () interface {} {
122- return serializeCollection (c )
123- }
124-
125- func NewCollection (data []BaseType ) * Collection {
126- return & Collection {data : data }
127- }
128-
129- /* ----------------boolean---------------- */
130-
131- func (b * Boolean ) Name () string {
132- return "boolean"
133- }
134-
135- func (b * Boolean ) SizeOf () uint {
136- return 1
137- }
138-
139- func (b * Boolean ) String () string {
140- if b .data {
141- return "true"
142- } else {
143- return "false"
144- }
145- }
146-
147- func (b * Boolean ) Native () interface {} {
148- return b .data
149- }
150-
151- func NewBoolean (data bool ) * Boolean {
152- return & Boolean {data : data }
153- }
0 commit comments