forked from fcgmedia/gorest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
130 lines (115 loc) · 4.53 KB
/
util.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
/*
Copyright Siyabonga Dlamini (siyabonga.dlamini@gmail.com).
All rights reserved.
Modified by HourlyHost, Inc.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package gorest
import (
"bytes"
"errors"
"reflect"
"strconv"
)
//Marshals the data in interface i into a byte slice, using the Marhaller/Unmarshaller specified in mime.
//The Marhaller/Unmarshaller must have been registered before using gorest.RegisterMarshaller
func Marshal(i interface{}, mime string) ([]byte, error) {
return InterfaceToBytes(i, mime)
}
//Marshals the data in interface i into a byte slice, using the Marhaller/Unmarshaller specified in mime.
//The Marhaller/Unmarshaller must have been registered before using gorest.RegisterMarshaller
func InterfaceToBytes(i interface{}, mime string) ([]byte, error) {
v := reflect.ValueOf(i)
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
switch v.Kind() {
case reflect.Bool:
x := v.Bool()
if x {
return []byte("true"), nil
}
return []byte("false"), nil
case reflect.String:
return []byte(v.String()), nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return []byte(strconv.FormatInt(v.Int(), 10)), nil
case reflect.Struct, reflect.Slice, reflect.Array, reflect.Map:
m := GetMarshallerByMime(mime)
return m.Marshal(i)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return []byte(strconv.FormatUint(v.Uint(), 10)), nil
case reflect.Float32, reflect.Float64:
return []byte(strconv.FormatFloat(v.Float(), 'g', -1, v.Type().Bits())), nil
default:
return nil, errors.New("Type " + v.Type().Name() + " is not handled by GoRest.")
}
return nil, nil
}
//Unmarshals the data in buf into interface i, using the Marhaller/Unmarshaller specified in mime.
//The Marhaller/Unmarshaller must have been registered before using gorest.RegisterMarshaller
func Unmarshal(buf *bytes.Buffer, i interface{}, mime string) error {
return BytesToInterface(buf, i, mime)
}
func BytesToInterface(buf *bytes.Buffer, i interface{}, mime string) error {
v := reflect.ValueOf(i)
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
switch v.Kind() {
case reflect.Bool:
n, err := strconv.ParseBool(buf.String())
if err != nil {
return errors.New("Invalid value. " + err.Error())
}
reflect.ValueOf(i).Elem().SetBool(n)
break
case reflect.String:
reflect.ValueOf(i).Elem().SetString(buf.String())
break
case reflect.Struct, reflect.Slice, reflect.Array, reflect.Map:
m := GetMarshallerByMime(mime)
return m.Unmarshal(buf.Bytes(), i)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
n, err := strconv.ParseInt(buf.String(), 10, 64)
if err != nil || v.OverflowInt(n) {
return errors.New("Invalid value. " + err.Error())
}
reflect.ValueOf(i).Elem().SetInt(n)
break
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
n, err := strconv.ParseUint(buf.String(), 10, 64)
if err != nil || v.OverflowUint(n) {
return errors.New("Invalid value. " + err.Error())
}
reflect.ValueOf(i).Elem().SetUint(n)
break
case reflect.Float32, reflect.Float64:
n, err := strconv.ParseFloat(buf.String(), v.Type().Bits())
if err != nil || v.OverflowFloat(n) {
return errors.New("Invalid value. " + err.Error())
}
reflect.ValueOf(i).Elem().SetFloat(n)
default:
return errors.New("Type " + v.Type().Name() + " is not handled by GoRest.")
}
return nil
}