1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

move Table to a separate file and add additional unit tests

Docker-DCO-1.1-Signed-off-by: Bryan Murphy <bmurphy1976@gmail.com> (github: bmurphy1976)
This commit is contained in:
Bryan Murphy 2014-05-01 20:09:39 +00:00
parent fb99f992c0
commit fdccfaf72a
3 changed files with 225 additions and 133 deletions

View file

@ -5,7 +5,6 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"sort"
"strconv" "strconv"
"strings" "strings"
) )
@ -251,135 +250,3 @@ func (env *Env) Map() map[string]string {
} }
return m return m
} }
type Table struct {
Data []*Env
sortKey string
Chan chan *Env
}
func NewTable(sortKey string, sizeHint int) *Table {
return &Table{
make([]*Env, 0, sizeHint),
sortKey,
make(chan *Env),
}
}
func (t *Table) SetKey(sortKey string) {
t.sortKey = sortKey
}
func (t *Table) Add(env *Env) {
t.Data = append(t.Data, env)
}
func (t *Table) Len() int {
return len(t.Data)
}
func (t *Table) Less(a, b int) bool {
return t.lessBy(a, b, t.sortKey)
}
func (t *Table) lessBy(a, b int, by string) bool {
keyA := t.Data[a].Get(by)
keyB := t.Data[b].Get(by)
intA, errA := strconv.ParseInt(keyA, 10, 64)
intB, errB := strconv.ParseInt(keyB, 10, 64)
if errA == nil && errB == nil {
return intA < intB
}
return keyA < keyB
}
func (t *Table) Swap(a, b int) {
tmp := t.Data[a]
t.Data[a] = t.Data[b]
t.Data[b] = tmp
}
func (t *Table) Sort() {
sort.Sort(t)
}
func (t *Table) ReverseSort() {
sort.Sort(sort.Reverse(t))
}
func (t *Table) WriteListTo(dst io.Writer) (n int64, err error) {
if _, err := dst.Write([]byte{'['}); err != nil {
return -1, err
}
n = 1
for i, env := range t.Data {
bytes, err := env.WriteTo(dst)
if err != nil {
return -1, err
}
n += bytes
if i != len(t.Data)-1 {
if _, err := dst.Write([]byte{','}); err != nil {
return -1, err
}
n += 1
}
}
if _, err := dst.Write([]byte{']'}); err != nil {
return -1, err
}
return n + 1, nil
}
func (t *Table) ToListString() (string, error) {
buffer := bytes.NewBuffer(nil)
if _, err := t.WriteListTo(buffer); err != nil {
return "", err
}
return buffer.String(), nil
}
func (t *Table) WriteTo(dst io.Writer) (n int64, err error) {
for _, env := range t.Data {
bytes, err := env.WriteTo(dst)
if err != nil {
return -1, err
}
n += bytes
}
return n, nil
}
func (t *Table) ReadListFrom(src []byte) (n int64, err error) {
var array []interface{}
if err := json.Unmarshal(src, &array); err != nil {
return -1, err
}
for _, item := range array {
if m, ok := item.(map[string]interface{}); ok {
env := &Env{}
for key, value := range m {
env.SetAuto(key, value)
}
t.Add(env)
}
}
return int64(len(src)), nil
}
func (t *Table) ReadFrom(src io.Reader) (n int64, err error) {
decoder := NewDecoder(src)
for {
env, err := decoder.Decode()
if err == io.EOF {
return 0, nil
} else if err != nil {
return -1, err
}
t.Add(env)
}
return 0, nil
}

141
engine/table.go Normal file
View file

@ -0,0 +1,141 @@
package engine
import (
"bytes"
"encoding/json"
"io"
"sort"
"strconv"
)
type Table struct {
Data []*Env
sortKey string
Chan chan *Env
}
func NewTable(sortKey string, sizeHint int) *Table {
return &Table{
make([]*Env, 0, sizeHint),
sortKey,
make(chan *Env),
}
}
func (t *Table) SetKey(sortKey string) {
t.sortKey = sortKey
}
func (t *Table) Add(env *Env) {
t.Data = append(t.Data, env)
}
func (t *Table) Len() int {
return len(t.Data)
}
func (t *Table) Less(a, b int) bool {
return t.lessBy(a, b, t.sortKey)
}
func (t *Table) lessBy(a, b int, by string) bool {
keyA := t.Data[a].Get(by)
keyB := t.Data[b].Get(by)
intA, errA := strconv.ParseInt(keyA, 10, 64)
intB, errB := strconv.ParseInt(keyB, 10, 64)
if errA == nil && errB == nil {
return intA < intB
}
return keyA < keyB
}
func (t *Table) Swap(a, b int) {
tmp := t.Data[a]
t.Data[a] = t.Data[b]
t.Data[b] = tmp
}
func (t *Table) Sort() {
sort.Sort(t)
}
func (t *Table) ReverseSort() {
sort.Sort(sort.Reverse(t))
}
func (t *Table) WriteListTo(dst io.Writer) (n int64, err error) {
if _, err := dst.Write([]byte{'['}); err != nil {
return -1, err
}
n = 1
for i, env := range t.Data {
bytes, err := env.WriteTo(dst)
if err != nil {
return -1, err
}
n += bytes
if i != len(t.Data)-1 {
if _, err := dst.Write([]byte{','}); err != nil {
return -1, err
}
n += 1
}
}
if _, err := dst.Write([]byte{']'}); err != nil {
return -1, err
}
return n + 1, nil
}
func (t *Table) ToListString() (string, error) {
buffer := bytes.NewBuffer(nil)
if _, err := t.WriteListTo(buffer); err != nil {
return "", err
}
return buffer.String(), nil
}
func (t *Table) WriteTo(dst io.Writer) (n int64, err error) {
for _, env := range t.Data {
bytes, err := env.WriteTo(dst)
if err != nil {
return -1, err
}
n += bytes
}
return n, nil
}
func (t *Table) ReadListFrom(src []byte) (n int64, err error) {
var array []interface{}
if err := json.Unmarshal(src, &array); err != nil {
return -1, err
}
for _, item := range array {
if m, ok := item.(map[string]interface{}); ok {
env := &Env{}
for key, value := range m {
env.SetAuto(key, value)
}
t.Add(env)
}
}
return int64(len(src)), nil
}
func (t *Table) ReadFrom(src io.Reader) (n int64, err error) {
decoder := NewDecoder(src)
for {
env, err := decoder.Decode()
if err == io.EOF {
return 0, nil
} else if err != nil {
return -1, err
}
t.Add(env)
}
return 0, nil
}

View file

@ -26,3 +26,87 @@ func TestTableWriteTo(t *testing.T) {
t.Fatalf("Inccorect output: %v", output) t.Fatalf("Inccorect output: %v", output)
} }
} }
func TestTableSortStringValue(t *testing.T) {
table := NewTable("Key", 0)
e := &Env{}
e.Set("Key", "A")
table.Add(e)
e = &Env{}
e.Set("Key", "D")
table.Add(e)
e = &Env{}
e.Set("Key", "B")
table.Add(e)
e = &Env{}
e.Set("Key", "C")
table.Add(e)
table.Sort()
if len := table.Len(); len != 4 {
t.Fatalf("Expected 4, got %d", len)
}
if value := table.Data[0].Get("Key"); value != "A" {
t.Fatalf("Expected A, got %s", value)
}
if value := table.Data[1].Get("Key"); value != "B" {
t.Fatalf("Expected B, got %s", value)
}
if value := table.Data[2].Get("Key"); value != "C" {
t.Fatalf("Expected C, got %s", value)
}
if value := table.Data[3].Get("Key"); value != "D" {
t.Fatalf("Expected D, got %s", value)
}
}
func TestTableReverseSortStringValue(t *testing.T) {
table := NewTable("Key", 0)
e := &Env{}
e.Set("Key", "A")
table.Add(e)
e = &Env{}
e.Set("Key", "D")
table.Add(e)
e = &Env{}
e.Set("Key", "B")
table.Add(e)
e = &Env{}
e.Set("Key", "C")
table.Add(e)
table.ReverseSort()
if len := table.Len(); len != 4 {
t.Fatalf("Expected 4, got %d", len)
}
if value := table.Data[0].Get("Key"); value != "D" {
t.Fatalf("Expected D, got %s", value)
}
if value := table.Data[1].Get("Key"); value != "C" {
t.Fatalf("Expected B, got %s", value)
}
if value := table.Data[2].Get("Key"); value != "B" {
t.Fatalf("Expected C, got %s", value)
}
if value := table.Data[3].Get("Key"); value != "A" {
t.Fatalf("Expected A, got %s", value)
}
}