forked from elves/elvish
-
Notifications
You must be signed in to change notification settings - Fork 0
/
assoc.go
72 lines (63 loc) · 1.78 KB
/
assoc.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
package vals
import (
"errors"
"github.com/xiaq/persistent/vector"
)
// Assocer wraps the Assoc method.
type Assocer interface {
// Assoc returns a slightly modified version of the receiver with key k
// associated with value v.
Assoc(k, v interface{}) (interface{}, error)
}
var (
errAssocUnsupported = errors.New("assoc is not supported")
errReplacementMustBeString = errors.New("replacement must be string")
errAssocWithSlice = errors.New("assoc with slice not yet supported")
)
// Assoc takes a container, a key and value, and returns a modified version of
// the container, in which the key associated with the value. It is implemented
// for the builtin type string, and types satisfying the listAssocable,
// mapAssocable or Assocer interface. For other types, it returns an error.
func Assoc(a, k, v interface{}) (interface{}, error) {
switch a := a.(type) {
case Assocer:
return a.Assoc(k, v)
case string:
return assocString(a, k, v)
case listAssocable:
return assocList(a, k, v)
case mapAssocable:
return a.Assoc(k, v), nil
}
return nil, errAssocUnsupported
}
func assocString(s string, k, v interface{}) (interface{}, error) {
i, j, err := convertStringIndex(k, s)
if err != nil {
return nil, err
}
repl, ok := v.(string)
if !ok {
return nil, errReplacementMustBeString
}
return s[:i] + repl + s[j:], nil
}
type listAssocable interface {
Lener
Assoc(int, interface{}) vector.Vector
}
var _ listAssocable = vector.Vector(nil)
func assocList(l listAssocable, k, v interface{}) (interface{}, error) {
kstring, ok := k.(string)
if !ok {
return nil, errIndexMustBeString
}
index, err := ConvertListIndex(kstring, l.Len())
if err != nil {
return nil, err
}
if index.Slice {
return nil, errAssocWithSlice
}
return l.Assoc(index.Lower, v), nil
}