Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions etf.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ type Ref struct {
Id []uint32
}

type MapEntry struct {
Key Term
Value Term
}

type Map []MapEntry

type Function struct {
Arity byte
Unique [16]byte
Expand Down Expand Up @@ -90,6 +97,7 @@ const (
ettSmallInteger = 'a'
ettSmallTuple = 'h'
ettString = 'k'
ettMap = 't'
)

const (
Expand Down Expand Up @@ -129,6 +137,7 @@ var tagNames = map[byte]string{
ettSmallInteger: "SMALL_INTEGER_EXT",
ettSmallTuple: "SMALL_TUPLE_EXT",
ettString: "STRING_EXT",
ettMap: "MAP",
}

func (t Tuple) Element(i int) Term {
Expand Down
17 changes: 17 additions & 0 deletions read.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,23 @@ func (c *Context) Read(r io.Reader) (term Term, err error) {
}
term = tuple

case ettMap:
// $iKVKVKV…
var arity uint32
if arity, err = ruint32(r); err != nil {
break
}
mapVal := make(Map, arity)
for i := 0; i < cap(mapVal); i++ {
if mapVal[i].Key, err = c.Read(r); err != nil {
break
}
if mapVal[i].Value, err = c.Read(r); err != nil {
break
}
}
term = mapVal

case ettList:
// $lLLLL…$j
var n uint32
Expand Down
28 changes: 28 additions & 0 deletions write.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ func (c *Context) Write(w io.Writer, term interface{}) (err error) {
err = c.writePid(w, v)
case Tuple:
err = c.writeTuple(w, v)
case Map:
err = c.writeMap(w, v)
case Ref:
err = c.writeRef(w, v)
default:
Expand Down Expand Up @@ -353,6 +355,32 @@ func (c *Context) writeTuple(w io.Writer, tuple Tuple) (err error) {
return
}

func (c *Context) writeMap(w io.Writer, theMap Map) (err error) {
n := len(theMap)
_, err = w.Write([]byte{
ettMap,
byte(n >> 24),
byte(n >> 16),
byte(n >> 8),
byte(n),
})

if err != nil {
return
}

for _, v := range theMap {
if err = c.Write(w, v.Key); err != nil {
return
}
if err = c.Write(w, v.Value); err != nil {
return
}
}

return
}

func reverse(b []byte) []byte {
size := len(b)
hsize := size >> 1
Expand Down