-
Notifications
You must be signed in to change notification settings - Fork 34
/
assign.go
52 lines (48 loc) · 1.18 KB
/
assign.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
package option
import (
"github.com/viant/afs/storage"
"reflect"
)
//Assign assign supplied option, if returns un assign options and true if assign at least one
func Assign(options []storage.Option, supported ...interface{}) ([]storage.Option, bool) {
return assign(options, supported)
}
//Assign assign supplied option
func assign(options []storage.Option, supported []interface{}) ([]storage.Option, bool) {
var unfiltered = make([]storage.Option, 0)
if len(options) == 0 {
return options, false
}
if len(supported) == 0 {
return options, false
}
var index = make(map[reflect.Type]interface{})
for i := range supported {
index[reflect.TypeOf(supported[i]).Elem()] = supported[i]
}
assigned := false
for i := range options {
option := options[i]
if option == nil {
continue
}
optionValue := reflect.ValueOf(option)
target, ok := index[optionValue.Type()]
if !ok {
for k, v := range index {
if optionValue.Type().AssignableTo(k) {
target = v
ok = true
break
}
}
}
if !ok {
unfiltered = append(unfiltered, options[i])
continue
}
assigned = true
reflect.ValueOf(target).Elem().Set(optionValue)
}
return unfiltered, assigned
}