-
Notifications
You must be signed in to change notification settings - Fork 13
/
example_Unmarshal_Unmarshaler_test.go
41 lines (35 loc) · 1.6 KB
/
example_Unmarshal_Unmarshaler_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
package watson_test
import (
"errors"
"fmt"
"strings"
"github.com/genkami/watson"
"github.com/genkami/watson/pkg/types"
)
type Email struct {
Local string
Domain string
}
func (e *Email) UnmarshalWatson(v *types.Value) error {
if v.Kind != types.String {
return errors.New("expected string")
}
parts := strings.Split(string(v.String), "@")
if len(parts) != 2 {
return errors.New("value must be like '[email protected]'")
}
e.Local = parts[0]
e.Domain = parts[1]
return nil
}
const marshaledEmail = `
?SShkShaaaaarrkShaaaaaarrk-SShkShaarrkShaaarrkShaaaaarrkShaaaaaarrk-SShkShaarrkShaaaaarrkShaaaaaarrk-SShaarrkShaaarrkShaaaaarrkShaaaaaarrk-SShkShaaarrkShaaaaarrkShaaaaaarrk-SShkShaaaaarrkShaaaaaarrk-SSharrkShaarrkShaaarrkShaaaaarrk-SShkSharrkShaarrkShaaaarrkShaaaaarrkShaaaaaarrk-SShkShaaaaarrkShaaaaaarrk-SShaarrkShaaaarrkShaaaaarrkShaaaaaarrk-SShkSharrkShaaaarrkShaaaaarrkShaaaaaarrk-SShkSharrkShaarrkShaaarrkShaaaaarrkShaaaaaarrk-SSharrkShaarrkShaaarrkShaaaaarrkShaaaaaarrk-SShaaaaaarrk-SShkShaarrkShaaaaarrkShaaaaaarrk-SShaaarrkShaaaarrkShaaaaarrkShaaaaaarrk-SShkShaaaaarrkShaaaaaarrk-SShkShaarrkShaaarrkShaaaaarrkShaaaaaarrk-SShaaaarrkShaaaaarrkShaaaaaarrk-SShaarrkShaaarrkShaaaaarrkShaaaaaarrk-SShkShaarrkShaaaaarrkShaaaaaarrk-SSharrkShaarrkShaaarrkShaaaaarrk-SShkSharrkShaaaaarrkShaaaaaarrk-SShkSharrkShaarrkShaaarrkShaaaaarrkShaaaaaarrk-SShkShaarrkShaaarrkShaaaaarrkShaaaaaarrk-
`
func ExampleUnmarshal_unmarshaler() {
var email Email
err := watson.Unmarshal([]byte(marshaledEmail), &email)
if err != nil {
panic(err)
}
fmt.Printf("Local: %s, Domain: %s\n", email.Local, email.Domain)
}