-
Notifications
You must be signed in to change notification settings - Fork 0
/
marshalers.go
147 lines (137 loc) · 3.45 KB
/
marshalers.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
// Copyright (c) 2024 Six After, Inc
//
// This source code is licensed under the Apache 2.0 License found in the
// LICENSE file in the root directory of this source tree.
package semver
import (
"database/sql/driver"
"encoding/json"
)
// MarshalText implements encoding.TextMarshaler.
// It returns the string representation of the Version.
//
// Example:
//
// v := semver.MustParse("1.2.3-alpha+build.456")
// text, err := v.MarshalText()
// if err != nil {
// log.Fatal(err)
// }
// fmt.Println(string(text)) // Output: 1.2.3-alpha+build.456
func (v Version) MarshalText() ([]byte, error) {
return []byte(v.String()), nil
}
// UnmarshalText implements encoding.TextUnmarshaler.
// It parses the given text into a Version.
//
// Example:
//
// var v semver.Version
// err := v.UnmarshalText([]byte("1.2.3-alpha+build.456"))
// if err != nil {
// log.Fatal(err)
// }
// fmt.Println(v) // Output: 1.2.3-alpha+build.456
func (v *Version) UnmarshalText(text []byte) error {
parsed, err := Parse(string(text))
if err != nil {
return err
}
*v = parsed
return nil
}
// MarshalBinary implements encoding.BinaryMarshaler.
// It returns the binary encoding of the Version.
//
// Example:
//
// v := semver.MustParse("1.2.3-alpha")
// binaryData, err := v.MarshalBinary()
// if err != nil {
// log.Fatal(err)
// }
// fmt.Printf("%s\n", binaryData) // Output: 1.2.3-alpha
func (v Version) MarshalBinary() ([]byte, error) {
return v.MarshalText()
}
// UnmarshalBinary implements encoding.BinaryUnmarshaler.
// It decodes the given binary data into a Version.
//
// Example:
//
// var v semver.Version
// err := v.UnmarshalBinary([]byte("1.2.3+build.123"))
// if err != nil {
// log.Fatal(err)
// }
// fmt.Println(v) // Output: 1.2.3+build.123
func (v *Version) UnmarshalBinary(data []byte) error {
return v.UnmarshalText(data)
}
// MarshalJSON implements json.Marshaler.
// It returns the JSON encoding of the Version.
//
// Example:
//
// v := semver.MustParse("1.2.3-beta")
// jsonData, err := v.MarshalJSON()
// if err != nil {
// log.Fatal(err)
// }
// fmt.Println(string(jsonData)) // Output: "1.2.3-beta"
func (v Version) MarshalJSON() ([]byte, error) {
return json.Marshal(v.String())
}
// UnmarshalJSON implements json.Unmarshaler.
// It decodes JSON data into a Version.
//
// Example:
//
// var v semver.Version
// err := v.UnmarshalJSON([]byte("\"1.2.3-beta+build\""))
// if err != nil {
// log.Fatal(err)
// }
// fmt.Println(v) // Output: 1.2.3-beta+build
func (v *Version) UnmarshalJSON(data []byte) error {
var text string
if err := json.Unmarshal(data, &text); err != nil {
return err
}
return v.UnmarshalText([]byte(text))
}
// Value implements database/sql/driver.Valuer.
// It returns the string representation of the Version as a database value.
//
// Example:
//
// v := semver.MustParse("1.2.3-alpha")
// dbValue, err := v.Value()
// if err != nil {
// log.Fatal(err)
// }
// fmt.Println(dbValue) // Output: 1.2.3-alpha
func (v Version) Value() (driver.Value, error) {
return v.String(), nil
}
// Scan implements database/sql.Scanner.
// It scans a database value into a Version.
//
// Example:
//
// var v semver.Version
// err := v.Scan("1.2.3-alpha+build")
// if err != nil {
// log.Fatal(err)
// }
// fmt.Println(v) // Output: 1.2.3-alpha+build
func (v *Version) Scan(value interface{}) error {
switch t := value.(type) {
case string:
return v.UnmarshalText([]byte(t))
case []byte:
return v.UnmarshalText(t)
default:
return ErrUnsupportedType
}
}