Skip to content

Commit 70b19ee

Browse files
committed
version 1.0.0
1 parent 724f149 commit 70b19ee

2 files changed

Lines changed: 212 additions & 1 deletion

File tree

reader.go

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,42 +7,221 @@ import (
77
)
88

99
type (
10+
// Reader is helper interface which provides possibility to
11+
// access struct's fields' values, reads and provides theirs values.
1012
Reader interface {
13+
// HasField checks if struct instance has a field with a given name.
14+
//
15+
// if reader.HasField("SomeFloatField") { ...
16+
//
1117
HasField(name string) bool
18+
// GetField returns struct instance's field value.
19+
// If there is no such field, it returns nil.
20+
// Usable to edit existing struct's field.
21+
//
22+
// field := reader.GetField("SomeFloatField")
23+
//
1224
GetField(name string) Field
25+
// GetAllFields returns a list of all struct instance's fields.
26+
//
27+
// for _, field := range reader.GetAllFields() { ...
28+
//
29+
GetAllFields() []Field
1330
}
1431

32+
// Field is a wrapper for struct's field's value.
33+
// It provides methods for easier field's value reading.
1534
Field interface {
35+
// GetName returns field's name in struct.
36+
//
37+
// name := field.GetName()
38+
//
39+
Name() string
40+
// PointerInt returns a pointer for instance of int type.
41+
// It panics if field's value can't be casted to desired type.
42+
//
43+
// number := reader.GetField("SomeField").PointerInt()
44+
//
1645
PointerInt() *int
46+
// Int returns an instance of int type.
47+
// It panics if field's value can't be casted to desired type.
48+
//
49+
// number := reader.GetField("SomeField").Int()
50+
//
1751
Int() int
52+
// PointerInt8 returns a pointer for instance of int8 type.
53+
// It panics if field's value can't be casted to desired type.
54+
//
55+
// number := reader.GetField("SomeField").PointerInt8()
56+
//
1857
PointerInt8() *int8
58+
// Int8 returns aan instance of int8 type.
59+
// It panics if field's value can't be casted to desired type.
60+
//
61+
// number := reader.GetField("SomeField").Int8()
62+
//
1963
Int8() int8
64+
// PointerInt16 returns a pointer for instance of int16 type.
65+
// It panics if field's value can't be casted to desired type.
66+
//
67+
// number := reader.GetField("SomeField").PointerInt16()
68+
//
2069
PointerInt16() *int16
70+
// Int16 returns an instance of int16 type.
71+
// It panics if field's value can't be casted to desired type.
72+
//
73+
// number := reader.GetField("SomeField").Int16()
74+
//
2175
Int16() int16
76+
// PointerInt32 returns a pointer for instance of int32 type.
77+
// It panics if field's value can't be casted to desired type.
78+
//
79+
// number := reader.GetField("SomeField").PointerInt32()
80+
//
2281
PointerInt32() *int32
82+
// Int32 returns an instance of int32 type.
83+
// It panics if field's value can't be casted to desired type.
84+
//
85+
// number := reader.GetField("SomeField").Int32()
86+
//
2387
Int32() int32
88+
// PointerInt64 returns a pointer for instance of int64 type.
89+
// It panics if field's value can't be casted to desired type.
90+
//
91+
// number := reader.GetField("SomeField").PointerInt64()
92+
//
2493
PointerInt64() *int64
94+
// Int64 returns an instance of int64 type.
95+
// It panics if field's value can't be casted to desired type.
96+
//
97+
// number := reader.GetField("SomeField").Int64()
98+
//
2599
Int64() int64
100+
// PointerUint returns a pointer for instance of uint type.
101+
// It panics if field's value can't be casted to desired type.
102+
//
103+
// unsigned := reader.GetField("SomeField").PointerUint()
104+
//
26105
PointerUint() *uint
106+
// Uint returns an instance of uint type.
107+
// It panics if field's value can't be casted to desired type.
108+
//
109+
// unsigned := reader.GetField("SomeField").Uint()
110+
//
27111
Uint() uint
112+
// PointerUint8 returns a pointer for instance of uint8 type.
113+
// It panics if field's value can't be casted to desired type.
114+
//
115+
// unsigned := reader.GetField("SomeField").PointerUint8()
116+
//
28117
PointerUint8() *uint8
118+
// Uint8 returns an instance of uint8 type.
119+
// It panics if field's value can't be casted to desired type.
120+
//
121+
// unsigned := reader.GetField("SomeField").Uint8()
122+
//
29123
Uint8() uint8
124+
// PointerUint16 returns a pointer for instance of uint16 type.
125+
// It panics if field's value can't be casted to desired type.
126+
//
127+
// unsigned := reader.GetField("SomeField").PointerUint16()
128+
//
30129
PointerUint16() *uint16
130+
// Uint16 returns an instance of uint16 type.
131+
// It panics if field's value can't be casted to desired type.
132+
//
133+
// unsigned := reader.GetField("SomeField").Uint16()
134+
//
31135
Uint16() uint16
136+
// PointerUint32 returns a pointer for instance of uint32 type.
137+
// It panics if field's value can't be casted to desired type.
138+
//
139+
// unsigned := reader.GetField("SomeField").PointerUint32()
140+
//
32141
PointerUint32() *uint32
142+
// Uint32 returns an instance of uint32 type.
143+
// It panics if field's value can't be casted to desired type.
144+
//
145+
// unsigned := reader.GetField("SomeField").Uint32()
146+
//
33147
Uint32() uint32
148+
// PointerUint64 returns a pointer for instance of uint64 type.
149+
// It panics if field's value can't be casted to desired type.
150+
//
151+
// unsigned := reader.GetField("SomeField").PointerUint64()
152+
//
34153
PointerUint64() *uint64
154+
// Uint64 returns an instance of uint64 type.
155+
// It panics if field's value can't be casted to desired type.
156+
//
157+
// unsigned := reader.GetField("SomeField").Uint64()
158+
//
35159
Uint64() uint64
160+
// PointerFloat32 returns a pointer for instance of float32 type.
161+
// It panics if field's value can't be casted to desired type.
162+
//
163+
// boolean := reader.GetField("SomeField").PointerFloat32()
164+
//
36165
PointerFloat32() *float32
166+
// Float32 returns an of float32 type.
167+
// It panics if field's value can't be casted to desired type.
168+
//
169+
// boolean := reader.GetField("SomeField").Float32()
170+
//
37171
Float32() float32
172+
// PointerFloat64 returns a pointer for instance of float64 type.
173+
// It panics if field's value can't be casted to desired type.
174+
//
175+
// boolean := reader.GetField("SomeField").PointerFloat64()
176+
//
38177
PointerFloat64() *float64
178+
// Float64 returns an instance of float64 type.
179+
// It panics if field's value can't be casted to desired type.
180+
//
181+
// boolean := reader.GetField("SomeField").Float64()
182+
//
39183
Float64() float64
184+
// PointerString returns a pointer for instance of string type.
185+
// It panics if field's value can't be casted to desired type.
186+
//
187+
// text := reader.GetField("SomeField").PointerString()
188+
//
40189
PointerString() *string
190+
// String returns aan instance of string type.
191+
// It panics if field's value can't be casted to desired type.
192+
//
193+
// text := reader.GetField("SomeField").String()
194+
//
41195
String() string
196+
// PointerBool returns a pointer for instance of bool type.
197+
// It panics if field's value can't be casted to desired type.
198+
//
199+
// boolean := reader.GetField("SomeField").PointerBool()
200+
//
42201
PointerBool() *bool
202+
// Bool returns an instance of bool type.
203+
// It panics if field's value can't be casted to desired type.
204+
//
205+
// boolean := reader.GetField("SomeField").Bool()
206+
//
43207
Bool() bool
208+
// PointerTime returns a pointer for instance of time.Time{} type.
209+
// It panics if field's value can't be casted to desired type.
210+
//
211+
// dateTime := reader.GetField("SomeField").PointerTime()
212+
//
44213
PointerTime() *time.Time
214+
// Time returns an instance of time.Time{} type.
215+
// It panics if field's value can't be casted to desired type.
216+
//
217+
// dateTime := reader.GetField("SomeField").Time()
218+
//
45219
Time() time.Time
220+
// Interface returns an interface which represents field's value.
221+
// Useful for casting value into desired type.
222+
//
223+
// slice, ok := reader.GetField("SomeField").Interface().([]int)
224+
//
46225
Interface() interface{}
47226
}
48227

@@ -56,6 +235,8 @@ type (
56235
}
57236
)
58237

238+
// NewReader reads struct instance and provides instance of
239+
// Reader interface to give possibility to read all fields' values.
59240
func NewReader(value interface{}) Reader {
60241
fields := map[string]fieldImpl{}
61242

@@ -87,6 +268,20 @@ func (r readImpl) GetField(name string) Field {
87268
return r.fields[name]
88269
}
89270

271+
func (r readImpl) GetAllFields() []Field {
272+
var fields []Field
273+
274+
for _, field := range r.fields {
275+
fields = append(fields, field)
276+
}
277+
278+
return fields
279+
}
280+
281+
func (f fieldImpl) Name() string {
282+
return f.field.Name
283+
}
284+
90285
func (f fieldImpl) PointerInt() *int {
91286
if f.value.IsNil() {
92287
return nil

reader_test.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,22 @@ func TestReaderImpl_HasField(t *testing.T) {
5050
}
5151
}
5252

53+
func TestReaderImpl_GetAllFields(t *testing.T) {
54+
reader := NewReader(testStruct{})
55+
56+
if len(reader.GetAllFields()) != 13 {
57+
t.Errorf(`TestReaderImpl_GetAllFields - expected to have 10 fields but got %d`, len(reader.GetAllFields()))
58+
}
59+
}
60+
61+
func TestFieldImpl_Name(t *testing.T) {
62+
reader := NewReader(testStruct{})
63+
64+
if reader.GetField("String").Name() != "String" {
65+
t.Errorf(`TestFieldImpl_Name - expected field name to be "String" got %s`, reader.GetField("String").Name())
66+
}
67+
}
68+
5369
func TestFieldImpl_PointerInt(t *testing.T) {
5470
expected := 123
5571

@@ -613,4 +629,4 @@ func TestFieldImpl_Interface(t *testing.T) {
613629
if !reflect.DeepEqual(value, expected) {
614630
t.Errorf(`TestFieldImpl_Interface - expected field "String" to be equal %#v but got %#v`, expected, value)
615631
}
616-
}
632+
}

0 commit comments

Comments
 (0)