/
https.go
145 lines (127 loc) · 3.18 KB
/
https.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
// Golang program to show how to
// use structs as map keys
package main
// importing required packages
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"
"time"
)
// A Cookie represents an HTTP cookie as sent in the Set-Cookie header of an
// HTTP response or the Cookie header of an HTTP request.
//
// See https://tools.ietf.org/html/rfc6265 for details.
//Stolen from Net/http/cookies
type Cookie struct {
Name string `json:"name"`
Value string `json:"value"`
Path string `json:"path"` // optional
Domain string `json:"domain"` // optional
Expires time.Time `json:"expires"` // optional
RawExpires string `json:"rawExpires"` // for reading cookies only
// MaxAge=0 means no 'Max-Age' attribute specified.
// MaxAge<0 means delete cookie now, equivalently 'Max-Age: 0'
// MaxAge>0 means Max-Age attribute present and given in seconds
MaxAge int `json:"maxAge"`
Secure bool `json:"secure"`
HttpOnly bool `json:"httpOnly"`
SameSite http.SameSite `json:"sameSite"`
Raw string
Unparsed []string `json:"unparsed"` // Raw text of unparsed attribute-value pairs
Time Time `json:"time"`
}
type Options struct {
Cookies []Cookie
name string
}
// Time wraps time.Time overriddin the json marshal/unmarshal to pass
// timestamp as integer
type Time struct {
Time time.Time
}
// UnmarshalJSON implements json.Unmarshaler inferface.
func (t *Time) UnmarshalJSON(buf []byte) error {
// Try to parse the timestamp integer
ts, err := strconv.ParseInt(string(buf), 10, 64)
if err == nil {
if len(buf) == 19 {
t.Time = time.Unix(ts/1e9, ts%1e9)
} else {
t.Time = time.Unix(ts, 0)
}
return nil
}
// Try the default unmarshal
if err := json.Unmarshal(buf, &t.Time); err == nil {
return nil
}
str := strings.Trim(string(buf), `"`)
if str == "null" || str == "" {
return nil
}
fmt.Println(buf, "test")
// Try to manually parse the data
tt, err := ParseDateString(str)
if err != nil {
return err
}
fmt.Println(tt)
t.Time = tt
return nil
}
type TLS struct {
Time Time `json:"time"`
}
// ParseDateString takes a string and passes it through Approxidate
// Parses into a time.Time
func ParseDateString(dt string) (time.Time, error) {
const layout = "Mon, 02-Jan-2006 15:04:05 MST"
fmt.Println(layout, dt, "end")
return time.Parse(layout, dt)
}
func main() {
jsonData := []byte(`[
{
"name": "Standard",
"value": "aaaaaaa",
"time" : "Mon, 02-Jan-2006 15:04:05 MST"
}]`)
var basenameOpts = []Cookie{
Cookie{
Name: "test1",
Value: "arr",
Expires: time.Now(),
},
Cookie{
Name: "test2",
Value: "b-option",
Expires: time.Now(),
},
}
var d Time
jStr := `{"time":"Mon, 02-Jan-2006 15:04:05 MST"}`
_ = json.Unmarshal([]byte(jStr), &d)
fmt.Println(d.Time)
fmt.Println(Options{Cookies: []Cookie{
Cookie{
Name: "test1",
Value: "arr",
Expires: time.Now(),
},
Cookie{
Name: "test2",
Value: "b-option",
Expires: time.Now(),
},
}})
var appointment []Cookie
err := json.Unmarshal(jsonData, &appointment)
if err != nil {
fmt.Printf("Error: ", err)
}
fmt.Println(basenameOpts, "\n")
fmt.Println(appointment[0].Time)
}