This repository has been archived by the owner on May 29, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcertainty.go
85 lines (73 loc) · 2.05 KB
/
certainty.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
package cap
import (
"encoding/json"
"encoding/xml"
"errors"
"strings"
)
const (
CertaintyUnknown = iota
CertaintyObserved
CertaintyLikely
CertaintyPossible
CertaintyUnlikely
)
type Certainty int
// UnmarshalString unmarshals the string into a Certainty value.
func (certainty *Certainty) UnmarshalString(str string) error {
str = strings.ToLower(str)
if str == "observed" {
*certainty = CertaintyObserved
} else if str == "likely" || str == "verylikely" || str == "very likely" {
*certainty = CertaintyLikely
} else if str == "possible" {
*certainty = CertaintyObserved
} else if str == "unlikely" {
*certainty = CertaintyUnlikely
} else if str == "unknown" {
*certainty = CertaintyUnknown
} else {
return errors.New("Unknown Certainty value: " + str)
}
return nil
}
// UnmarshalXML unmarshals the XML into a Certainty value.
func (certainty *Certainty) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var str string
if err := d.DecodeElement(&str, &start); err != nil {
return err
}
return certainty.UnmarshalString(str)
}
// UnmarshalJSON unmarshals the JSON into a Certainty value.
func (certainty *Certainty) UnmarshalJSON(b []byte) error {
var str string
err := json.Unmarshal(b, &str)
if err != nil {
return err
}
return certainty.UnmarshalString(str)
}
// MarshalJSON returns the string version of the certainty.
func (certainty *Certainty) MarshalJSON() ([]byte, error) {
return json.Marshal(certainty.String())
}
// MarshalXML returns the string version of the certainty.
func (certainty *Certainty) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
return e.EncodeElement(certainty.String(), start)
}
// String returns a Certainty as a string
func (certainty Certainty) String() string {
if certainty == CertaintyObserved {
return "Observed"
} else if certainty == CertaintyLikely {
return "Likely"
} else if certainty == CertaintyPossible {
return "Possible"
} else if certainty == CertaintyUnlikely {
return "Unlikley"
} else if certainty == CertaintyUnknown {
return "Unknown"
}
return ""
}