-
Notifications
You must be signed in to change notification settings - Fork 20
/
environment.go
128 lines (107 loc) · 4.08 KB
/
environment.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
package utils
import (
"encoding/json"
"time"
)
type RedactionPolicy string
const (
RedactionPolicyNone RedactionPolicy = "none"
RedactionPolicyURNs RedactionPolicy = "urns"
)
// Environment defines the environment that the Excellent function is running in, this includes
// the timezone the user is in as well as the preferred date and time formats.
type Environment interface {
DateFormat() DateFormat
TimeFormat() TimeFormat
Timezone() *time.Location
Languages() LanguageList
RedactionPolicy() RedactionPolicy
// Convenience method to get the current time in the env timezone
Now() time.Time
// extensions to the engine can expect their own env values
Extension(string) json.RawMessage
}
// NewDefaultEnvironment creates a new Environment with our usual defaults in the UTC timezone
func NewDefaultEnvironment() Environment {
return &environment{
dateFormat: DateFormatYearMonthDay,
timeFormat: TimeFormatHourMinute,
timezone: time.UTC,
languages: LanguageList{},
redactionPolicy: RedactionPolicyNone,
}
}
// NewEnvironment creates a new Environment with the passed in date and time formats and timezone
func NewEnvironment(dateFormat DateFormat, timeFormat TimeFormat, timezone *time.Location, languages LanguageList, redactionPolicy RedactionPolicy) Environment {
return &environment{
dateFormat: dateFormat,
timeFormat: timeFormat,
timezone: timezone,
languages: languages,
redactionPolicy: redactionPolicy,
}
}
type environment struct {
dateFormat DateFormat
timeFormat TimeFormat
timezone *time.Location
languages LanguageList
redactionPolicy RedactionPolicy
extensions map[string]json.RawMessage
}
func (e *environment) DateFormat() DateFormat { return e.dateFormat }
func (e *environment) TimeFormat() TimeFormat { return e.timeFormat }
func (e *environment) Timezone() *time.Location { return e.timezone }
func (e *environment) Languages() LanguageList { return e.languages }
func (e *environment) RedactionPolicy() RedactionPolicy { return e.redactionPolicy }
func (e *environment) Now() time.Time { return Now().In(e.Timezone()) }
func (e *environment) Extension(name string) json.RawMessage {
return e.extensions[name]
}
//------------------------------------------------------------------------------------------
// JSON Encoding / Decoding
//------------------------------------------------------------------------------------------
type envEnvelope struct {
DateFormat DateFormat `json:"date_format" validate:"required,date_format"`
TimeFormat TimeFormat `json:"time_format" validate:"required,time_format"`
Timezone string `json:"timezone" validate:"required"`
Languages LanguageList `json:"languages"`
RedactionPolicy RedactionPolicy `json:"redaction_policy" validate:"omitempty,eq=none|eq=urns"`
Extensions map[string]json.RawMessage `json:"extensions,omitempty"`
}
// ReadEnvironment reads an environment from the given JSON
func ReadEnvironment(data json.RawMessage) (Environment, error) {
env := NewDefaultEnvironment().(*environment)
var envelope envEnvelope
if err := UnmarshalAndValidate(data, &envelope); err != nil {
return nil, err
}
env.dateFormat = envelope.DateFormat
env.timeFormat = envelope.TimeFormat
env.extensions = envelope.Extensions
tz, err := time.LoadLocation(envelope.Timezone)
if err != nil {
return nil, err
}
env.timezone = tz
if envelope.Languages != nil {
env.languages = envelope.Languages
}
env.redactionPolicy = envelope.RedactionPolicy
if env.redactionPolicy == "" {
env.redactionPolicy = RedactionPolicyNone
}
return env, nil
}
// MarshalJSON marshals this environment into JSON
func (e *environment) MarshalJSON() ([]byte, error) {
ee := &envEnvelope{
DateFormat: e.dateFormat,
TimeFormat: e.timeFormat,
Timezone: e.timezone.String(),
Languages: e.languages,
RedactionPolicy: e.redactionPolicy,
Extensions: e.extensions,
}
return json.Marshal(ee)
}