-
Notifications
You must be signed in to change notification settings - Fork 1
/
util_netentity.go
114 lines (92 loc) · 2.57 KB
/
util_netentity.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
// Copyright 2015 Luke Shumaker
package httpentity
import (
"encoding/json"
"fmt"
"io"
"locale"
"net/http"
"net/url"
)
func ErrorToNetEntity(status int16, err locale.Error) NetEntity {
return NetPrintf("%d %s %v", status, http.StatusText(int(status)), err)
}
//////////////////////////////////////////////////////////////////////
// NetString is a string that implements httpentity.NetEntity.
type NetStringer struct {
locale.Stringer
}
var _ NetEntity = NetStringer{}
// NetPrintf is fmt.Sprintf as a NetString.
func NetPrintf(format string, a ...interface{}) NetStringer {
return NetStringer{locale.Sprintf(format, a...)}
}
// Encoders fulfills the httpentity.NetEntity interface.
func (s NetStringer) Encoders() map[string]Encoder {
return map[string]Encoder{
"text/plain": EncoderTXT{s},
"application/json": EncoderJSONStr{s},
}
}
//////////////////////////////////////////////////////////////////////
type NetLocations []*url.URL
type netLocationsTXT struct{ NetLocations }
type netLocationsJSON struct{ NetLocations }
// Encoders fulfills the httpentity.NetEntity interface.
func (l NetLocations) Encoders() map[string]Encoder {
return map[string]Encoder{
"text/plain": netLocationsTXT{l},
"application/json": netLocationsJSON{l},
}
}
func (l NetLocations) Locales() []locale.Spec {
return []locale.Spec{}
}
func (l NetLocations) IsText() bool {
return true
}
func (l netLocationsTXT) Write(w io.Writer, loc locale.Spec) locale.Error {
for _, line := range l.NetLocations {
_, uerr := fmt.Fprintln(w, line)
if uerr != nil {
return locale.UntranslatedError(uerr)
}
}
return nil
}
func (l netLocationsJSON) Write(w io.Writer, loc locale.Spec) locale.Error {
strs := make([]string, len(l.NetLocations))
for i, u := range l.NetLocations {
strs[i] = u.String()
}
bytes, uerr := json.Marshal(strs)
if uerr != nil {
return locale.UntranslatedError(uerr)
}
_, uerr = w.Write(bytes)
return locale.UntranslatedError(uerr)
}
//////////////////////////////////////////////////////////////////////
type NetJSON struct {
Data interface{}
}
// Encoders fulfills the httpentity.NetEntity interface.
func (l NetJSON) Encoders() map[string]Encoder {
return map[string]Encoder{
"application/json": l,
}
}
func (l NetJSON) Locales() []locale.Spec {
return []locale.Spec{}
}
func (l NetJSON) IsText() bool {
return true
}
func (l NetJSON) Write(w io.Writer, loc locale.Spec) locale.Error {
bytes, uerr := json.Marshal(l.Data)
if uerr != nil {
return locale.UntranslatedError(uerr)
}
_, uerr = w.Write(bytes)
return locale.UntranslatedError(uerr)
}