-
Notifications
You must be signed in to change notification settings - Fork 0
/
converter.go
46 lines (39 loc) · 974 Bytes
/
converter.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
package slicetostruct
import (
"fmt"
"reflect"
"sync"
)
type ConvertValueParams struct {
Items []string
Index int
ReflectValue *reflect.Value
Tags []string
FieldName *string
FieltType string
}
type Converter interface {
Set(value *ConvertValueParams) error
}
var ErrConverterDoesNotExist = fmt.Errorf("converter does not exist")
type converters struct {
converters map[string]Converter
mu sync.Mutex
}
func (converters *converters) SetConverter(name string, converter Converter) {
converters.mu.Lock()
defer converters.mu.Unlock()
if converters.converters == nil {
converters.converters = make(map[string]Converter)
}
converters.converters[name] = converter
}
func (converters *converters) GetConverter(name string) (Converter, error) {
converters.mu.Lock()
defer converters.mu.Unlock()
converter, ok := converters.converters[name]
if !ok {
return nil, ErrConverterDoesNotExist
}
return converter, nil
}