-
Notifications
You must be signed in to change notification settings - Fork 0
/
conversion.go
169 lines (141 loc) · 4.89 KB
/
conversion.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package core
import (
"errors"
"github.com/procyon-projects/goo"
"sync"
)
type TypeConverter interface {
Support(sourceTyp goo.Type, targetTyp goo.Type) bool
Convert(source interface{}, sourceTyp goo.Type, targetTyp goo.Type) (interface{}, error)
}
type StringToNumberConverter struct {
}
func NewStringToNumberConverter() StringToNumberConverter {
return StringToNumberConverter{}
}
func (converter StringToNumberConverter) Support(sourceTyp goo.Type, targetTyp goo.Type) bool {
if sourceTyp.IsString() && targetTyp.IsNumber() && goo.ComplexType != targetTyp.ToNumberType().GetType() {
return true
}
return false
}
func (converter StringToNumberConverter) Convert(source interface{}, sourceTyp goo.Type, targetTyp goo.Type) (interface{}, error) {
if sourceTyp.IsString() && targetTyp.IsNumber() && goo.ComplexType != targetTyp.ToNumberType().GetType() {
number := targetTyp.ToNumberType()
return sourceTyp.ToStringType().ToNumber(source.(string), number)
}
return nil, errors.New("unsupported type")
}
type NumberToStringConverter struct {
}
func NewNumberToStringConverter() NumberToStringConverter {
return NumberToStringConverter{}
}
func (converter NumberToStringConverter) Support(sourceTyp goo.Type, targetTyp goo.Type) bool {
if targetTyp.IsString() && sourceTyp.IsNumber() && goo.ComplexType != sourceTyp.ToNumberType().GetType() {
return true
}
return false
}
func (converter NumberToStringConverter) Convert(source interface{}, sourceTyp goo.Type, targetTyp goo.Type) (interface{}, error) {
if targetTyp.IsString() && sourceTyp.IsNumber() && goo.ComplexType != sourceTyp.ToNumberType().GetType() {
return sourceTyp.ToNumberType().ToString(source), nil
}
return nil, errors.New("unsupported type")
}
type StringToBooleanConverter struct {
}
func NewStringToBooleanConverter() StringToBooleanConverter {
return StringToBooleanConverter{}
}
func (converter StringToBooleanConverter) Support(sourceTyp goo.Type, targetTyp goo.Type) bool {
if sourceTyp.IsString() && targetTyp.IsBoolean() {
return true
}
return false
}
func (converter StringToBooleanConverter) Convert(source interface{}, sourceTyp goo.Type, targetTyp goo.Type) (result interface{}, err error) {
if sourceTyp.IsString() && targetTyp.IsBoolean() {
return targetTyp.ToBooleanType().ToBoolean(source.(string)), nil
}
return nil, errors.New("unsupported type")
}
type BooleanToStringConverter struct {
}
func NewBooleanToStringConverter() BooleanToStringConverter {
return BooleanToStringConverter{}
}
func (converter BooleanToStringConverter) Support(sourceTyp goo.Type, targetTyp goo.Type) bool {
if targetTyp.IsString() && sourceTyp.IsBoolean() {
return true
}
return false
}
func (converter BooleanToStringConverter) Convert(source interface{}, sourceTyp goo.Type, targetTyp goo.Type) (interface{}, error) {
if targetTyp.IsString() && sourceTyp.IsBoolean() {
return sourceTyp.ToBooleanType().ToString(source.(bool)), nil
}
return nil, errors.New("unsupported type")
}
type TypeConverterRegistry interface {
RegisterConverter(converter TypeConverter)
}
type TypeConverterService interface {
TypeConverterRegistry
CanConvert(sourceTyp goo.Type, targetTyp goo.Type) bool
Convert(source interface{}, sourceTyp goo.Type, targetTyp goo.Type) (interface{}, error)
}
type DefaultTypeConverterService struct {
converters map[string]TypeConverter
mu sync.RWMutex
}
func NewDefaultTypeConverterService() *DefaultTypeConverterService {
converterService := &DefaultTypeConverterService{
converters: make(map[string]TypeConverter, 0),
}
converterService.registerDefaultConverters()
return converterService
}
func (cs *DefaultTypeConverterService) registerDefaultConverters() {
/* number to string and string to number */
cs.RegisterConverter(NewNumberToStringConverter())
cs.RegisterConverter(NewStringToNumberConverter())
/* bool to string and string to bool */
cs.RegisterConverter(NewBooleanToStringConverter())
cs.RegisterConverter(NewStringToBooleanConverter())
}
func (cs *DefaultTypeConverterService) CanConvert(sourceTyp goo.Type, targetTyp goo.Type) bool {
var result bool
cs.mu.Lock()
for _, converter := range cs.converters {
if converter.Support(sourceTyp, targetTyp) {
result = true
break
}
}
cs.mu.Unlock()
return result
}
func (cs *DefaultTypeConverterService) Convert(source interface{}, sourceTyp goo.Type, targetTyp goo.Type) (result interface{}, err error) {
var typConverter TypeConverter
cs.mu.Lock()
for _, converter := range cs.converters {
if converter.Support(sourceTyp, targetTyp) {
typConverter = converter
break
}
}
cs.mu.Unlock()
if typConverter != nil {
result, err = typConverter.Convert(source, sourceTyp, targetTyp)
}
return
}
func (cs *DefaultTypeConverterService) RegisterConverter(converter TypeConverter) {
if converter == nil {
panic("converter must not be nil")
}
cs.mu.Lock()
cs.converters[goo.GetType(converter).GetFullName()] = converter
cs.mu.Unlock()
}