-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
67 lines (57 loc) · 1.89 KB
/
main.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
package main
import (
"encoding/json"
"log"
"net/mail"
"net/url"
"os"
"regexp"
"time"
"github.com/MicahParks/jsontype"
)
const exampleConfig = `{
"ends": "Wed, 04 Oct 2022 00:00:00 MST",
"getInterval": "1h30m",
"notificationMsg": "Your item is on sale!",
"notify": "EXAMPLE@example.com",
"targetPage": "https://www.example.com",
"targetRegExp": "example",
"targetUUID": "84abbfc2-b7a8-4446-a351-927c0fd26a3a"
}`
type myConfig struct {
Ends *jsontype.JSONType[time.Time] `json:"ends"`
GetInterval *jsontype.JSONType[time.Duration] `json:"getInterval"`
NotificationMsg string `json:"notificationMsg"`
Notify *jsontype.JSONType[*mail.Address] `json:"notify"`
TargetPage *jsontype.JSONType[*url.URL] `json:"targetPage"`
TargetRegExp *jsontype.JSONType[*regexp.Regexp] `json:"targetRegExp"`
}
func main() {
logger := log.New(os.Stdout, "", 0)
var config myConfig
// Set non-default unmarshal behavior.
endOpts := jsontype.Options{
TimeFormatUnmarshal: time.RFC1123,
}
config.Ends = jsontype.NewWithOptions(time.Time{}, endOpts)
// Unmarshal the configuration.
err := json.Unmarshal(json.RawMessage(exampleConfig), &config)
if err != nil {
logger.Fatalf("failed to unmarshal JSON: %s", err)
}
// Access fields on the unmarshalled configuration.
logger.Printf("Ends: %s", config.Ends.Get().String())
logger.Printf("Get interval: %s", config.GetInterval.Get().String())
// Set non-default marshal behavior.
emailOpts := jsontype.Options{
MailAddressAddressOnlyMarshal: true,
MailAddressLowerMarshal: true,
}
config.Notify = jsontype.NewWithOptions(config.Notify.Get(), emailOpts)
// Marshal the configuration back to JSON.
remarshaled, err := json.MarshalIndent(config, "", " ")
if err != nil {
logger.Fatalf("failed to re-marshal configuration: %s", err)
}
logger.Println(string(remarshaled))
}