-
Notifications
You must be signed in to change notification settings - Fork 1
/
licensefile-extras.go
83 lines (69 loc) · 2.17 KB
/
licensefile-extras.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
package licensefile
import (
"errors"
"fmt"
)
// ErrFieldDoesNotExist is returned when trying to retrieve a field from the Extra
// map of a File with a given key name using one of the ExtraAs... funcs but the
// named field does not exist in the map.
var ErrFieldDoesNotExist = errors.New("extra field does not exist")
// ExtraAsInt returns the value of the Extra field with the given name as an int64.
// If the field cannot be found, an error is returned. If the field cannot be type
// asserted to an int, an error is returned.
func (f *File) ExtraAsInt(name string) (i int, err error) {
v, ok := f.Extras[name]
if !ok {
return 0, ErrFieldDoesNotExist
}
i, ok = v.(int)
if !ok {
err = fmt.Errorf("could not assert field to int64, field is %T", v)
return
}
return
}
// ExtraAsFloat returns the value of the Extra field with the given name as a float64.
// If the field cannot be found, an error is returned. If the field cannot be type
// asserted to a float64, an error is returned.
func (f *File) ExtraAsFloat(name string) (x float64, err error) {
v, ok := f.Extras[name]
if !ok {
return 0.0, ErrFieldDoesNotExist
}
x, ok = v.(float64)
if !ok {
err = fmt.Errorf("could not assert field to float64, field is %T", v)
return
}
return
}
// ExtraAsString returns the value of the Extra field with the given name as a string.
// If the field cannot be found, an error is returned. If the field cannot be type
// asserted to a string, an error is returned.
func (f *File) ExtraAsString(name string) (s string, err error) {
v, ok := f.Extras[name]
if !ok {
return "", ErrFieldDoesNotExist
}
s, ok = v.(string)
if !ok {
err = fmt.Errorf("could not assert field to string, field is %T", v)
return
}
return
}
// ExtraAsBool returns the value of the Extra field with the given name as a bool.
// If the field cannot be found, an error is returned. If the field cannot be type
// asserted to an bool, an error is returned.
func (f *File) ExtraAsBool(name string) (b bool, err error) {
v, ok := f.Extras[name]
if !ok {
return false, ErrFieldDoesNotExist
}
b, ok = v.(bool)
if !ok {
err = fmt.Errorf("could not assert field to bool, field is %T", v)
return
}
return
}