Skip to content

Commit 724f149

Browse files
committed
builder documentation
1 parent 3bb22f1 commit 724f149

1 file changed

Lines changed: 65 additions & 0 deletions

File tree

builder.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,70 @@ package dynamicstruct
33
import "reflect"
44

55
type (
6+
// Builder holds all fields' definitions for desired structs.
7+
// It gives opportunity to add or remove fields, or to edit
8+
// existing ones. In the end, it provides definition for
9+
// dynamic structs, used to create new instances.
610
Builder interface {
11+
// AddField creates new struct's field.
12+
// It expects field's name, type and string.
13+
// Type is provided as an instance of some golang type.
14+
// Tag is provided as classical golang field tag.
15+
//
16+
// builder.AddField("SomeFloatField", 0.0, `json:"boolean" validate:"gte=10"`)
17+
//
718
AddField(name string, typ interface{}, tag string) Builder
19+
// RemoveField removes existing struct's field.
20+
//
21+
// builder.RemoveField("SomeFloatField")
22+
//
823
RemoveField(name string) Builder
24+
// HasField checks if struct has a field with a given name.
25+
//
26+
// if builder.HasField("SomeFloatField") { ...
27+
//
928
HasField(name string) bool
29+
// GetField returns struct's field definition.
30+
// If there is no such field, it returns nil.
31+
// Usable to edit existing struct's field.
32+
//
33+
// field := builder.GetField("SomeFloatField")
34+
//
1035
GetField(name string) FieldConfig
36+
// Build returns definition for dynamic struct.
37+
// Definition can be used to create new instances.
38+
//
39+
// dStruct := builder.Build()
40+
//
1141
Build() DynamicStruct
1242
}
1343

44+
// FieldConfig holds single field's definition.
45+
// It provides possibility to edit field's type and tag.
1446
FieldConfig interface {
47+
// SetType changes field's type.
48+
// Expected value is an instance of golang type.
49+
//
50+
// field.SetType([]int{})
51+
//
1552
SetType(typ interface{}) FieldConfig
53+
// SetTag changes fields's tag.
54+
// Expected value is an string which represents classical
55+
// golang tag.
56+
//
57+
// field.SetTag(`json:"slice"`)
58+
//
1659
SetTag(tag string) FieldConfig
1760
}
1861

62+
// DynamicStruct contains defined dynamic struct.
63+
// This definition can't be changed anymore, once is built.
64+
// It provides a method for creating new instances of same defintion.
1965
DynamicStruct interface {
66+
// New provides new instance of defined dynamic struct.
67+
//
68+
// value := dStruct.New()
69+
//
2070
New() interface{}
2171
}
2272

@@ -34,16 +84,31 @@ type (
3484
}
3585
)
3686

87+
// NewStruct returns new clean instance of Builder interface
88+
// for defining fresh dynamic struct.
89+
//
90+
// builder := dynamicstruct.NewStruct()
91+
//
3792
func NewStruct() Builder {
3893
return &builderImpl{
3994
fields: map[string]*fieldConfigImpl{},
4095
}
4196
}
4297

98+
// ExtendStruct extends existing instance of struct and
99+
// returns new instance of Builder interface.
100+
//
101+
// builder := dynamicstruct.MergeStructs(MyStruct{})
102+
//
43103
func ExtendStruct(value interface{}) Builder {
44104
return MergeStructs(value)
45105
}
46106

107+
// MergeStructs merges a list of existing instances of structs and
108+
// returns new instance of Builder interface.
109+
//
110+
// builder := dynamicstruct.MergeStructs(MyStructOne{}, MyStructTwo{}, MyStructThree{})
111+
//
47112
func MergeStructs(values ...interface{}) Builder {
48113
builder := NewStruct()
49114

0 commit comments

Comments
 (0)