elvish/eval/map.go

74 lines
1.2 KiB
Go
Raw Normal View History

2016-02-19 05:52:05 +08:00
package eval
import (
"bytes"
"errors"
)
// Map is a map from string to Value.
type Map struct {
inner *map[Value]Value
}
2016-02-19 19:21:55 +08:00
type MapLike interface {
Lener
IndexOneer
}
2016-02-19 05:52:05 +08:00
// NewMap creates a new Map.
func NewMap(inner map[Value]Value) Map {
return Map{&inner}
}
func (Map) Kind() string {
return "map"
}
func (m Map) Repr() string {
var builder MapReprBuilder
for k, v := range *m.inner {
builder.WritePair(k.Repr(), v.Repr())
}
return builder.String()
}
2016-02-19 18:49:19 +08:00
func (m Map) Len() int {
return len(*m.inner)
}
2016-02-19 05:52:05 +08:00
func (m Map) IndexOne(idx Value) Value {
v, ok := (*m.inner)[idx]
if !ok {
throw(errors.New("no such key: " + idx.Repr()))
}
return v
}
func (m Map) IndexSet(idx Value, v Value) {
(*m.inner)[idx] = v
}
// MapReprBuilder helps building the Repr of a Map. It is also useful for
// implementing other Map-like values. The zero value of a MapReprBuilder is
// ready to use.
type MapReprBuilder struct {
buf bytes.Buffer
}
func (b *MapReprBuilder) WritePair(k, v string) {
if b.buf.Len() == 0 {
b.buf.WriteByte('[')
} else {
b.buf.WriteByte(' ')
}
b.buf.WriteString("&" + k + "=" + v)
2016-02-19 05:52:05 +08:00
}
func (b *MapReprBuilder) String() string {
if b.buf.Len() == 0 {
return "[&]"
}
b.buf.WriteByte(']')
return b.buf.String()
}