forked from ardanlabs/gotraining
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercise1.go
71 lines (59 loc) · 2.14 KB
/
exercise1.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
// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
// http://play.golang.org/p/xy-wyPrsjz
// Declare a struct type that represents a request for a customer invoice. Include a CustomerID and InvoiceID field. Define
// tags that can be used to validate the request. Define tags that specify both the length and range for the ID to be valid.
// Declare a function named validate that accepts values of any type and processes the tags. Display the resutls of the validation.
package main
import (
"fmt"
"reflect"
"strconv"
"strings"
"unicode/utf8"
)
// Customer represents a customer
type Customer struct {
CustomerID int `length:"3" range:"100-300"`
InvoiceID int `length:"5" range:"60000-99999"`
}
// main is the entry point for the application.
func main() {
// Declare a variable of type Customer.
customer := Customer{
CustomerID: 202,
InvoiceID: 76545,
}
// Validate the value and display the results.
validate(&customer)
}
// validate performs data validation on any struct type value.
func validate(value interface{}) {
// Retrieve the value that the interface contains or points to.
val := reflect.ValueOf(value).Elem()
// Iterate over the fields of the struct value.
for i := 0; i < val.NumField(); i++ {
// Retrieve the field information.
typeField := val.Type().Field(i)
// Get the value as an int, string and the length.
field := typeField.Name
value := int(val.Field(i).Int())
stringValue := strconv.Itoa(value)
valueLength := utf8.RuneCountInString(stringValue)
// Test the length first
length, _ := strconv.Atoi(typeField.Tag.Get("length"))
if valueLength != length {
fmt.Printf("Invalid Length: Field[%s] Value[%d] - Len[%d] - ExpLen[%d]\n", field, value, valueLength, length)
continue
}
// Test the range.
r := strings.Split(typeField.Tag.Get("range"), "-")
front, _ := strconv.Atoi(r[0])
end, _ := strconv.Atoi(r[1])
if value < front || value > end {
fmt.Printf("Invalid Range: Field[%s] Value[%d] - Front[%d] - End[%d]\n", field, value, front, end)
continue
}
fmt.Printf("VALID: Field[%s] Value[%d]\n", field, value)
}
}