forked from googlecodelabs/tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
53 lines (47 loc) · 1.41 KB
/
context.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
package types
import (
"bytes"
"time"
)
// Context is an export context.
// It is defined in this package so that it can be used by both cli and a server.
type Context struct {
Env string `json:"environment"` // Current export environment
Format string `json:"format"` // Output format, e.g. "html"
Prefix string `json:"prefix,omitempty"` // Assets URL prefix for HTML-based formats
MainGA string `json:"mainga,omitempty"` // Global Google Analytics ID
Updated *ContextTime `json:"updated,omitempty"` // Last update timestamp
}
// ContextMeta is a composition of export context and meta data.
type ContextMeta struct {
Context
Meta
}
// ContextTime is a wrapper around time.Time so we can implement JSON marshalling.
type ContextTime time.Time
// MarshalJSON implements Marshaler interface.
// The output format is RFC3339.
func (ct ContextTime) MarshalJSON() ([]byte, error) {
v := time.Time(ct).Format(time.RFC3339)
b := make([]byte, len(v)+2)
b[0] = '"'
b[len(b)-1] = '"'
copy(b[1:], v)
return b, nil
}
// UnmarshalJSON implements Unmarshaler interface.
// Accepted formats:
// - RFC3339
// - YYYY-MM-DD
func (ct *ContextTime) UnmarshalJSON(b []byte) error {
b = bytes.Trim(b, `"`)
t, err := time.Parse(time.RFC3339, string(b))
if err != nil {
t, err = time.Parse("2006-01-02", string(b))
}
if err != nil {
return err
}
*ct = ContextTime(t)
return nil
}