This repository has been archived by the owner on Jan 30, 2020. It is now read-only.
/
marshalling.go
149 lines (131 loc) · 4.8 KB
/
marshalling.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
146
147
148
149
/*
* Copyright 2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package server
import (
"io"
"reflect"
"mime"
"encoding/json"
"fmt"
"bytes"
"strconv"
)
type MediaType string
// Marshaller is used to convert a runtime instance of some type t to bytes
type Marshaller interface {
// supportedMediaTypes returns the list of types that this marshaller is able to use to marshall type y.
// Should return nil/empty if t is not supported. Any returned media type should then be acceptable as
// a call to marshall. Typically, the return mediaTypes will be matched against an 'Accept' header.
supportedMediaTypes(t reflect.Type) []MediaType
// marshall should write out the given value to w according to the given mediaType
marshall(value interface{}, w io.Writer, mediaType MediaType) error
}
// Unmarshaller is used to read bytes to a runtime go object, according to a given mediaType.
type Unmarshaller interface {
// canUnmarshall should return true if it supports the target receiving type t and the given mediaType (typically
// the value of a 'Content-Type' header). A subsequent call to unmarshall with that mediaType should not fail
// by lack of support of that (type, mediaType) combination.
canUnmarshall(t reflect.Type, mediaType MediaType) bool
// unmarshall should read bytes from r and turn them into an instance of t, according to the given mediaType.
unmarshall(r io.Reader, t reflect.Type, mediaType MediaType) (interface{}, error)
}
// jsonMarshalling supports both marshalling and unmarshalling to/from json according to golang's json rules.
type jsonMarshalling struct {
}
func (*jsonMarshalling) supportedMediaTypes(t reflect.Type) []MediaType {
// Technically, should check that t is marshallable to json
return []MediaType{"application/json"}
}
func (*jsonMarshalling) marshall(value interface{}, w io.Writer, mediaType MediaType) error {
err := json.NewEncoder(w).Encode(value)
return err
}
func (*jsonMarshalling) canUnmarshall(t reflect.Type, mediaType MediaType) bool {
contentType, _, err := mime.ParseMediaType(string(mediaType))
if err != nil {
return false
}
// TODO should also chack that t is one of the supported types
return contentType == "application/json"
}
func (*jsonMarshalling) unmarshall(r io.Reader, t reflect.Type, mediaType MediaType) (interface{}, error) {
ptrToData := reflect.New(t)
err := json.NewDecoder(r).Decode(ptrToData.Interface())
if err != nil {
return nil, err
}
return reflect.Indirect(ptrToData).Interface(), nil
}
// textMarshalling supports marshalling from golang's fmt.Stringer type and unmarshalling to golang's string
type textMarshalling struct {
}
func (*textMarshalling) supportedMediaTypes(t reflect.Type) []MediaType {
var pstringer *fmt.Stringer
stringerType := reflect.TypeOf(pstringer).Elem()
if t.AssignableTo(stringerType) || t.Kind() == reflect.String || t.Kind() == reflect.Int || t.Kind() == reflect.Float32 ||
t.Kind() == reflect.Float64 {
return []MediaType{"text/plain"}
} else {
return nil
}
}
// X -> string
func (*textMarshalling) marshall(value interface{}, w io.Writer, mediaType MediaType) error {
var s string
switch v := value.(type) {
case int:
s = strconv.Itoa(v)
case float32:
s = strconv.FormatFloat(float64(v), 'f', -1, 32)
case float64:
s = strconv.FormatFloat(v, 'f', -1, 64)
case string:
s = v
case fmt.Stringer:
s = v.String()
}
_, error := io.WriteString(w, s)
return error
}
func (*textMarshalling) canUnmarshall(t reflect.Type, mediaType MediaType) bool {
contentType, _, err := mime.ParseMediaType(string(mediaType))
if err != nil {
return false
}
return contentType == "text/plain" &&
(t.Kind() == reflect.String || t.Kind() == reflect.Int || t.Kind() == reflect.Float32 || t.Kind() == reflect.Float64)
}
// string -> X
func (*textMarshalling) unmarshall(r io.Reader, t reflect.Type, mediaType MediaType) (interface{}, error) {
buf := new(bytes.Buffer)
_, err := buf.ReadFrom(r)
if err != nil {
return nil, err
}
switch t.Kind() {
case reflect.String:
return buf.String(), err
case reflect.Int:
return strconv.Atoi(buf.String())
case reflect.Float32:
f, err := strconv.ParseFloat(buf.String(), 32)
return float32(f), err
case reflect.Float64:
return strconv.ParseFloat(buf.String(), 64)
default:
panic("Unreachable thanks to canUnmarshall()")
}
}