Skip to content

Commit aba7c0d

Browse files
Merge pull request #49 from elcengine/feature/schema-type-aliases
feat: added type aliases for reflect kinds to provide a single access point
2 parents e91d7c5 + aa93e09 commit aba7c0d

7 files changed

Lines changed: 59 additions & 34 deletions

File tree

core/schema_reflect_types.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package elemental
2+
3+
import (
4+
"go.mongodb.org/mongo-driver/bson/primitive"
5+
"reflect"
6+
)
7+
8+
// Inbuilt types
9+
10+
var Slice = reflect.Slice
11+
var Map = reflect.Map
12+
var Struct = reflect.Struct
13+
var Interface = reflect.Interface
14+
var Array = reflect.Array
15+
var Bool = reflect.Bool
16+
var Int = reflect.Int
17+
var Int32 = reflect.Int32
18+
var Int64 = reflect.Int64
19+
var Uint = reflect.Uint
20+
var Uint8 = reflect.Uint8
21+
var Uint16 = reflect.Uint16
22+
var Uint32 = reflect.Uint32
23+
var Uint64 = reflect.Uint64
24+
var Float32 = reflect.Float32
25+
var Float64 = reflect.Float64
26+
var String = reflect.String
27+
28+
// Custom types
29+
30+
var ObjectID = reflect.TypeOf(primitive.NilObjectID).Kind()

core/schema_types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type SchemaOptions struct {
1717
}
1818

1919
type Field struct {
20-
Type reflect.Kind // Type of the field. Can be any of the reflect.Kind types
20+
Type reflect.Kind // Type of the field. Can be of reflect.Kind, an alias defined within elemental such as elemental.String or a custom reflection
2121
Schema *Schema // Defines a subschema for the field if it is a subdocument
2222
Required bool // Whether the field is required or not when creating a new document
2323
Default any // Default value for the field when creating a new document

tests/core_audit_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package tests
22

33
import (
4-
"reflect"
54
"testing"
65

76
pc "github.com/akalanka47000/go-modkit/parallel_convey"
@@ -19,7 +18,7 @@ func TestCoreAudit(t *testing.T) {
1918

2019
KingdomModel := elemental.NewModel[Kingdom](entity, elemental.NewSchema(map[string]elemental.Field{
2120
"Name": {
22-
Type: reflect.String,
21+
Type: elemental.String,
2322
Required: true,
2423
},
2524
})).SetDatabase(t.Name())

tests/core_middleware_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package tests
22

33
import (
4-
"reflect"
54
"testing"
65

76
elemental "github.com/elcengine/elemental/core"
@@ -21,7 +20,7 @@ func TestCoreMiddleware(t *testing.T) {
2120

2221
CastleModel := elemental.NewModel[Castle]("Castle-For-Middleware", elemental.NewSchema(map[string]elemental.Field{
2322
"Name": {
24-
Type: reflect.String,
23+
Type: elemental.String,
2524
Required: true,
2625
},
2726
})).SetDatabase(t.Name())

tests/core_schema_test.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package tests
22

33
import (
44
"fmt"
5-
"reflect"
65
"regexp"
76
"testing"
87

@@ -31,7 +30,7 @@ func TestCoreSchemaOptions(t *testing.T) {
3130
Convey("Collection should be a plural of the model name if not specified", func() {
3231
CastleModel := elemental.NewModel[Castle]("Castle", elemental.NewSchema(map[string]elemental.Field{
3332
"Name": {
34-
Type: reflect.String,
33+
Type: elemental.String,
3534
Required: true,
3635
},
3736
})).SetDatabase(t.Name())
@@ -44,7 +43,7 @@ func TestCoreSchemaOptions(t *testing.T) {
4443
collectionOptions.SetSizeInBytes(1024)
4544
KingdomModel := elemental.NewModel[Kingdom]("Kingdom-Temporary", elemental.NewSchema(map[string]elemental.Field{
4645
"Name": {
47-
Type: reflect.String,
46+
Type: elemental.String,
4847
Required: true,
4948
},
5049
}, elemental.SchemaOptions{
@@ -58,7 +57,7 @@ func TestCoreSchemaOptions(t *testing.T) {
5857
DATABASE := fmt.Sprintf("%s_%s", t.Name(), "temporary_1")
5958
MonsterModel := elemental.NewModel[Monster]("Monster-Temporary", elemental.NewSchema(map[string]elemental.Field{
6059
"Name": {
61-
Type: reflect.String,
60+
Type: elemental.String,
6261
Required: true,
6362
},
6463
}, elemental.SchemaOptions{
@@ -79,7 +78,7 @@ func TestCoreSchemaOptions(t *testing.T) {
7978
Convey("Required field with default", func() {
8079
Model := elemental.NewModel[User](uuid.NewString(), elemental.NewSchema(map[string]elemental.Field{
8180
"Name": {
82-
Type: reflect.String,
81+
Type: elemental.String,
8382
Required: true,
8483
Default: "Placeholder",
8584
},
@@ -94,7 +93,7 @@ func TestCoreSchemaOptions(t *testing.T) {
9493
Convey("Min check", func() {
9594
Model := elemental.NewModel[User](uuid.NewString(), elemental.NewSchema(map[string]elemental.Field{
9695
"Age": {
97-
Type: reflect.Int,
96+
Type: elemental.Int,
9897
Min: 10,
9998
},
10099
}))
@@ -111,7 +110,7 @@ func TestCoreSchemaOptions(t *testing.T) {
111110
Convey("Max check", func() {
112111
Model := elemental.NewModel[User](uuid.NewString(), elemental.NewSchema(map[string]elemental.Field{
113112
"Age": {
114-
Type: reflect.Int,
113+
Type: elemental.Int,
115114
Max: 120,
116115
},
117116
}))
@@ -128,7 +127,7 @@ func TestCoreSchemaOptions(t *testing.T) {
128127
Convey("Length check", func() {
129128
Model := elemental.NewModel[User](uuid.NewString(), elemental.NewSchema(map[string]elemental.Field{
130129
"Name": {
131-
Type: reflect.String,
130+
Type: elemental.String,
132131
Length: 10,
133132
},
134133
}))
@@ -142,7 +141,7 @@ func TestCoreSchemaOptions(t *testing.T) {
142141
Convey("Regex check", func() {
143142
Model := elemental.NewModel[User](uuid.NewString(), elemental.NewSchema(map[string]elemental.Field{
144143
"Name": {
145-
Type: reflect.String,
144+
Type: elemental.String,
146145
Regex: regexp.MustCompile("^[A-Z]+$"),
147146
},
148147
}))

tests/core_triggers_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package tests
22

33
import (
44
"context"
5-
"reflect"
65
"testing"
76
"time"
87

@@ -20,7 +19,7 @@ func TestCoreTriggers(t *testing.T) {
2019

2120
CastleModel := elemental.NewModel[Castle]("Castle-For-Triggers", elemental.NewSchema(map[string]elemental.Field{
2221
"Name": {
23-
Type: reflect.String,
22+
Type: elemental.String,
2423
Required: true,
2524
},
2625
})).SetDatabase(t.Name())

tests/fixtures/fixtures.go

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
package fixtures
33

44
import (
5-
"reflect"
65
"time"
76

87
elemental "github.com/elcengine/elemental/core"
@@ -70,23 +69,23 @@ const DefaultUserAge = 18
7069

7170
var UserModel = elemental.NewModel[User]("User", elemental.NewSchema(map[string]elemental.Field{
7271
"Name": {
73-
Type: reflect.String,
72+
Type: elemental.String,
7473
Required: true,
7574
Index: options.Index().SetUnique(true),
7675
},
7776
"Age": {
78-
Type: reflect.Int,
77+
Type: elemental.Int,
7978
Default: DefaultUserAge,
8079
},
8180
"Occupation": {
82-
Type: reflect.String,
81+
Type: elemental.String,
8382
},
8483
"Weapons": {
85-
Type: reflect.Slice,
84+
Type: elemental.Slice,
8685
Default: []string{},
8786
},
8887
"Retired": {
89-
Type: reflect.Bool,
88+
Type: elemental.Bool,
9089
Default: false,
9190
},
9291
}, elemental.SchemaOptions{
@@ -95,30 +94,30 @@ var UserModel = elemental.NewModel[User]("User", elemental.NewSchema(map[string]
9594

9695
var MonsterModel = elemental.NewModel[Monster]("Monster", elemental.NewSchema(map[string]elemental.Field{
9796
"Name": {
98-
Type: reflect.String,
97+
Type: elemental.String,
9998
Required: true,
10099
},
101100
"Category": {
102-
Type: reflect.String,
101+
Type: elemental.String,
103102
},
104103
"Weaknesses": {
105-
Type: reflect.Struct,
104+
Type: elemental.Struct,
106105
Schema: lo.ToPtr(elemental.NewSchema(map[string]elemental.Field{
107106
"Oils": {
108-
Type: reflect.Slice,
107+
Type: elemental.Slice,
109108
},
110109
"Signs": {
111-
Type: reflect.Slice,
110+
Type: elemental.Slice,
112111
Default: []string{"Igni"},
113112
},
114113
"Decoctions": {
115-
Type: reflect.Slice,
114+
Type: elemental.Slice,
116115
},
117116
"Bombs": {
118-
Type: reflect.Slice,
117+
Type: elemental.Slice,
119118
},
120119
"InvulnerableTo": {
121-
Type: reflect.Slice,
120+
Type: elemental.Slice,
122121
Default: []string{"Steel"},
123122
},
124123
})),
@@ -129,7 +128,7 @@ var MonsterModel = elemental.NewModel[Monster]("Monster", elemental.NewSchema(ma
129128

130129
var KingdomModel = elemental.NewModel[Kingdom]("Kingdom", elemental.NewSchema(map[string]elemental.Field{
131130
"Name": {
132-
Type: reflect.String,
131+
Type: elemental.String,
133132
Required: true,
134133
},
135134
}, elemental.SchemaOptions{
@@ -138,11 +137,11 @@ var KingdomModel = elemental.NewModel[Kingdom]("Kingdom", elemental.NewSchema(ma
138137

139138
var BestiaryModel = elemental.NewModel[Bestiary]("Bestiary", elemental.NewSchema(map[string]elemental.Field{
140139
"Monster": {
141-
Type: reflect.Struct,
140+
Type: elemental.Struct,
142141
Ref: "Monster",
143142
},
144143
"Kingdom": {
145-
Type: reflect.Struct,
144+
Type: elemental.Struct,
146145
Ref: "Kingdom",
147146
},
148147
}, elemental.SchemaOptions{
@@ -151,7 +150,7 @@ var BestiaryModel = elemental.NewModel[Bestiary]("Bestiary", elemental.NewSchema
151150

152151
var BestiaryWithIDModel = elemental.NewModel[BestiaryWithID]("BestiaryWithID", elemental.NewSchema(map[string]elemental.Field{
153152
"MonsterID": {
154-
Type: reflect.String,
153+
Type: elemental.String,
155154
Ref: "Monster",
156155
IsRefID: true,
157156
},

0 commit comments

Comments
 (0)