github.com/lmars/docker@v1.6.0-rc2/engine/table.go (about)

     1  package engine
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"io"
     7  	"sort"
     8  	"strconv"
     9  )
    10  
    11  type Table struct {
    12  	Data    []*Env
    13  	sortKey string
    14  	Chan    chan *Env
    15  }
    16  
    17  func NewTable(sortKey string, sizeHint int) *Table {
    18  	return &Table{
    19  		make([]*Env, 0, sizeHint),
    20  		sortKey,
    21  		make(chan *Env),
    22  	}
    23  }
    24  
    25  func (t *Table) SetKey(sortKey string) {
    26  	t.sortKey = sortKey
    27  }
    28  
    29  func (t *Table) Add(env *Env) {
    30  	t.Data = append(t.Data, env)
    31  }
    32  
    33  func (t *Table) Len() int {
    34  	return len(t.Data)
    35  }
    36  
    37  func (t *Table) Less(a, b int) bool {
    38  	return t.lessBy(a, b, t.sortKey)
    39  }
    40  
    41  func (t *Table) lessBy(a, b int, by string) bool {
    42  	keyA := t.Data[a].Get(by)
    43  	keyB := t.Data[b].Get(by)
    44  	intA, errA := strconv.ParseInt(keyA, 10, 64)
    45  	intB, errB := strconv.ParseInt(keyB, 10, 64)
    46  	if errA == nil && errB == nil {
    47  		return intA < intB
    48  	}
    49  	return keyA < keyB
    50  }
    51  
    52  func (t *Table) Swap(a, b int) {
    53  	tmp := t.Data[a]
    54  	t.Data[a] = t.Data[b]
    55  	t.Data[b] = tmp
    56  }
    57  
    58  func (t *Table) Sort() {
    59  	sort.Sort(t)
    60  }
    61  
    62  func (t *Table) ReverseSort() {
    63  	sort.Sort(sort.Reverse(t))
    64  }
    65  
    66  func (t *Table) WriteListTo(dst io.Writer) (n int64, err error) {
    67  	if _, err := dst.Write([]byte{'['}); err != nil {
    68  		return -1, err
    69  	}
    70  	n = 1
    71  	for i, env := range t.Data {
    72  		bytes, err := env.WriteTo(dst)
    73  		if err != nil {
    74  			return -1, err
    75  		}
    76  		n += bytes
    77  		if i != len(t.Data)-1 {
    78  			if _, err := dst.Write([]byte{','}); err != nil {
    79  				return -1, err
    80  			}
    81  			n++
    82  		}
    83  	}
    84  	if _, err := dst.Write([]byte{']'}); err != nil {
    85  		return -1, err
    86  	}
    87  	return n + 1, nil
    88  }
    89  
    90  func (t *Table) ToListString() (string, error) {
    91  	buffer := bytes.NewBuffer(nil)
    92  	if _, err := t.WriteListTo(buffer); err != nil {
    93  		return "", err
    94  	}
    95  	return buffer.String(), nil
    96  }
    97  
    98  func (t *Table) WriteTo(dst io.Writer) (n int64, err error) {
    99  	for _, env := range t.Data {
   100  		bytes, err := env.WriteTo(dst)
   101  		if err != nil {
   102  			return -1, err
   103  		}
   104  		n += bytes
   105  	}
   106  	return n, nil
   107  }
   108  
   109  func (t *Table) ReadListFrom(src []byte) (n int64, err error) {
   110  	var array []interface{}
   111  
   112  	if err := json.Unmarshal(src, &array); err != nil {
   113  		return -1, err
   114  	}
   115  
   116  	for _, item := range array {
   117  		if m, ok := item.(map[string]interface{}); ok {
   118  			env := &Env{}
   119  			for key, value := range m {
   120  				env.SetAuto(key, value)
   121  			}
   122  			t.Add(env)
   123  		}
   124  	}
   125  
   126  	return int64(len(src)), nil
   127  }
   128  
   129  func (t *Table) ReadFrom(src io.Reader) (n int64, err error) {
   130  	decoder := NewDecoder(src)
   131  	for {
   132  		env, err := decoder.Decode()
   133  		if err == io.EOF {
   134  			return 0, nil
   135  		} else if err != nil {
   136  			return -1, err
   137  		}
   138  		t.Add(env)
   139  	}
   140  }