-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
165 lines (145 loc) · 4.49 KB
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package structformat_test
import (
"fmt"
"strconv"
"github.com/gentlemanautomaton/structformat"
"github.com/gentlemanautomaton/structformat/fieldformat"
)
// PersonFormat describes a format for people.
type PersonFormat struct {
ID fieldformat.Options
Name fieldformat.Options
Age fieldformat.Options
Email fieldformat.Options
Status fieldformat.Options
}
// Age is a person's age in years.
type Age int
// String returns a string representation of the age.
func (age Age) String() string {
if age == 0 {
return ""
}
return strconv.Itoa(int(age))
}
// Person holds information about a person.
type Person struct {
ID string
Name string
Age Age
Email string
Status string
}
// String returns a string representation of a person.
func (p Person) String() string {
return p.Format(PersonFormat{})
}
// Format returns a string representation of a person with the given
// formatting options.
func (p Person) Format(format PersonFormat) string {
var builder structformat.Builder
builder.ApplyRules(structformat.InferRules(
format.ID,
format.Name,
format.Age,
format.Email,
format.Status,
))
builder.WritePrimary(p.ID, format.ID)
builder.WriteStandard(p.Name, format.Name)
builder.WriteField(p.Age.String(), fieldformat.Label("Age"), fieldformat.Note, format.Age)
builder.WriteNote(p.Email, format.Email)
builder.Divide()
builder.WriteField(p.Status, format.Status)
return builder.String()
}
func Example() {
// Prepare a list of people.
people := []Person{
{ID: "1591", Name: "Alice", Age: 57, Email: "[email protected]", Status: "Online"},
{ID: "122520", Name: "Bob", Age: 53, Email: "[email protected]", Status: "On Vacation"},
{Name: "Eve", Age: 34, Email: "[email protected]", Status: "Listening"},
{ID: "128", Name: "Mallory", Age: 29, Email: "[email protected]", Status: "Tinkering"},
{ID: "172340", Name: "Doug", Age: 1},
{Name: "Felix", Status: "Busy"},
}
// Print the ID and email address for each person.
idAndEmail := PersonFormat{
ID: fieldformat.Options{
Include: true,
Alignment: fieldformat.Right,
},
Email: fieldformat.Combine(fieldformat.Include, fieldformat.Standard),
}
for _, person := range people {
idAndEmail.ID.AdjustWidth(len(person.ID))
}
fmt.Println("People (ID, Email):")
for _, person := range people {
fmt.Println(person.Format(idAndEmail))
}
// Print all fields except for email.
// Use a column width for ID and name.
allExceptID := PersonFormat{
ID: fieldformat.Exclude.Options(),
Age: fieldformat.Right.Options(),
}
allExceptID.Age.Padding = "0"
for _, person := range people {
allExceptID.Name.AdjustWidth(len(person.Name))
allExceptID.Age.AdjustWidth(len(person.Age.String()))
allExceptID.Email.AdjustWidth(len(person.Email))
}
fmt.Println("People (Name, Age, Email, Status):")
for i, person := range people {
fmt.Printf("%d: %s\n", i, person.Format(allExceptID))
}
// Print all fields, with most fields shown as indented blocks.
allWithBlocks := PersonFormat{
ID: fieldformat.Combine(fieldformat.Block, fieldformat.Indent(2), fieldformat.Label("ID")),
Age: fieldformat.Combine(fieldformat.Block, fieldformat.Indent(2), fieldformat.Label("Age"), fieldformat.NumericSuffix("year old", "years old")),
Email: fieldformat.Combine(fieldformat.Standard, fieldformat.Wrap("<", ">")),
Status: fieldformat.Combine(fieldformat.Block, fieldformat.Indent(2), fieldformat.Label("Status")),
}
fmt.Println("\nPeople (Name, Email / ID, Age, Status):")
for _, person := range people {
fmt.Println(person.Format(allWithBlocks))
}
// Output:
// People (ID, Email):
// 1591: [email protected]
// 122520: [email protected]
// 128: [email protected]
// 172340
//
// People (Name, Age, Email, Status):
// 0: Alice (Age: 57, [email protected]): Online
// 1: Bob (Age: 53, [email protected]): On Vacation
// 2: Eve (Age: 34, [email protected]): Listening
// 3: Mallory (Age: 29, [email protected]): Tinkering
// 4: Doug (Age: 01)
// 5: Felix: Busy
//
// People (Name, Email / ID, Age, Status):
// Alice <[email protected]>
// ID: 1591
// Age: 57 years old
// Status: Online
// Bob <[email protected]>
// ID: 122520
// Age: 53 years old
// Status: On Vacation
// Eve <[email protected]>
// Age: 34 years old
// Status: Listening
// Mallory <[email protected]>
// ID: 128
// Age: 29 years old
// Status: Tinkering
// Doug
// ID: 172340
// Age: 1 year old
// Felix
// Status: Busy
}