Permalink
Browse files

Privatise codecs

  • Loading branch information...
1 parent 87898cb commit 111d944d6fbe9c865ec0e1f6bb9626ff30a6ffdc @cloudhead cloudhead committed Sep 6, 2012
Showing with 40 additions and 40 deletions.
  1. +2 −2 app.go
  2. +16 −16 codec.go
  3. +2 −2 codec_test.go
  4. +2 −2 endpoint.go
  5. +4 −4 file.go
  6. +1 −1 file_test.go
  7. +2 −2 proctype.go
  8. +1 −1 revision.go
  9. +5 −5 snapshot.go
  10. +3 −3 ticket.go
  11. +2 −2 visor.go
View
4 app.go
@@ -62,7 +62,7 @@ func (a *App) Register() (app *App, err error) {
attrs := &file{
Snapshot: a.Snapshot,
- Codec: new(JSONCodec),
+ codec: new(jsonCodec),
dir: a.dir.prefix("attrs"),
Value: map[string]interface{}{
"repo-url": a.RepoUrl,
@@ -201,7 +201,7 @@ func (a *App) Inspect() string {
func GetApp(s Snapshot, name string) (app *App, err error) {
app = NewApp(name, "", "", s)
- f, err := s.getFile(app.dir.prefix("attrs"), new(JSONCodec))
+ f, err := s.getFile(app.dir.prefix("attrs"), new(jsonCodec))
if err != nil {
return nil, err
}
View
@@ -14,26 +14,26 @@ import (
// A Codec represents a protocol for encoding and
// decoding file values in the coordinator.
-type Codec interface {
+type codec interface {
Encode(input interface{}) ([]byte, error)
Decode(input []byte) (interface{}, error)
}
// ByteCodec is a transparent Codec which doesn't
// perform any serialization or deserialization.
-type ByteCodec struct{}
+type byteCodec struct{}
-func (*ByteCodec) Encode(input interface{}) ([]byte, error) {
+func (*byteCodec) Encode(input interface{}) ([]byte, error) {
return input.([]byte), nil
}
-func (*ByteCodec) Decode(input []byte) (interface{}, error) {
+func (*byteCodec) Decode(input []byte) (interface{}, error) {
return input, nil
}
// StringCodec is a Codec which converts data to and from the Go *string* type.
-type StringCodec struct{}
+type stringCodec struct{}
-func (*StringCodec) Encode(input interface{}) (output []byte, err error) {
+func (*stringCodec) Encode(input interface{}) (output []byte, err error) {
switch i := input.(type) {
case string:
output = []byte(i)
@@ -44,34 +44,34 @@ func (*StringCodec) Encode(input interface{}) (output []byte, err error) {
}
return
}
-func (*StringCodec) Decode(input []byte) (interface{}, error) {
+func (*stringCodec) Decode(input []byte) (interface{}, error) {
return string(input), nil
}
-type JSONCodec struct{}
+type jsonCodec struct{}
-func (*JSONCodec) Encode(input interface{}) ([]byte, error) {
+func (*jsonCodec) Encode(input interface{}) ([]byte, error) {
return json.Marshal(input)
}
-func (*JSONCodec) Decode(input []byte) (val interface{}, err error) {
+func (*jsonCodec) Decode(input []byte) (val interface{}, err error) {
err = json.Unmarshal(input, &val)
return
}
-type IntCodec struct{}
+type intCodec struct{}
-func (*IntCodec) Encode(input interface{}) ([]byte, error) {
+func (*intCodec) Encode(input interface{}) ([]byte, error) {
return []byte(strconv.Itoa(input.(int))), nil
}
-func (*IntCodec) Decode(input []byte) (interface{}, error) {
+func (*intCodec) Decode(input []byte) (interface{}, error) {
return strconv.Atoi(string(input))
}
-type ListCodec struct{}
+type listCodec struct{}
-func (*ListCodec) Encode(input interface{}) ([]byte, error) {
+func (*listCodec) Encode(input interface{}) ([]byte, error) {
return []byte(strings.Join(input.([]string), " ")), nil
}
-func (*ListCodec) Decode(input []byte) (interface{}, error) {
+func (*listCodec) Decode(input []byte) (interface{}, error) {
return strings.Fields(string(input)), nil
}
View
@@ -10,7 +10,7 @@ import (
)
func TestListCodecEncoding(t *testing.T) {
- codec := new(ListCodec)
+ codec := new(listCodec)
encoded, _ := codec.Encode([]string{"a", "bb", "ccc"})
if string(encoded) != "a bb ccc" {
t.Errorf("expected '%s' got '%s'", []byte("a bb ccc"), encoded)
@@ -19,7 +19,7 @@ func TestListCodecEncoding(t *testing.T) {
}
func TestListCodecDecoding(t *testing.T) {
- codec := new(ListCodec)
+ codec := new(listCodec)
decoded, _ := codec.Decode([]byte("xxx yy z"))
d := decoded.([]string)
if (d[0] != "xxx") || (d[1] != "yy") || (d[2] != "z") {
View
@@ -65,7 +65,7 @@ func (e *Endpoint) Register() (ep *Endpoint, err error) {
e.Addr,
}
- f, err := createFile(e.Snapshot, e.dir.String(), data, new(ListCodec))
+ f, err := createFile(e.Snapshot, e.dir.String(), data, new(listCodec))
if err != nil {
return
}
@@ -93,7 +93,7 @@ func (e *Endpoint) Inspect() string {
func GetEndpoint(s Snapshot, srv *Service, addr string) (e *Endpoint, err error) {
path := srv.dir.prefix(endpointsPath, addr)
- f, err := s.getFile(path, new(ListCodec))
+ f, err := s.getFile(path, new(listCodec))
if err != nil {
return
}
View
@@ -16,11 +16,11 @@ type file struct {
FileRev int64 // File rev, or 0 if path doesn't exist
dir string
Value interface{}
- Codec Codec
+ codec codec
}
-func createFile(snapshot Snapshot, path string, value interface{}, codec Codec) (*file, error) {
- file := &file{dir: path, Value: value, Codec: codec, Snapshot: snapshot, FileRev: -1}
+func createFile(snapshot Snapshot, path string, value interface{}, codec codec) (*file, error) {
+ file := &file{dir: path, Value: value, codec: codec, Snapshot: snapshot, FileRev: -1}
return file.Create()
}
@@ -55,7 +55,7 @@ func (f *file) Create() (*file, error) {
// Set sets the value at this file's path to a new value.
func (f *file) Set(value interface{}) (file *file, err error) {
- bytes, err := f.Codec.Encode(value)
+ bytes, err := f.codec.Encode(value)
if err != nil {
return
}
View
@@ -18,7 +18,7 @@ func fileSetup(path string, value interface{}) *file {
r, _ := s.conn.Rev()
err = s.conn.Del("/", r)
- file := &file{dir: path, Value: value, Codec: new(ByteCodec), Snapshot: s.FastForward(-1)}
+ file := &file{dir: path, Value: value, codec: new(byteCodec), Snapshot: s.FastForward(-1)}
return file
}
View
@@ -58,7 +58,7 @@ func (p *ProcType) Register() (ptype *ProcType, err error) {
return nil, errors.New(fmt.Sprintf("couldn't claim port: %s", err.Error()))
}
- port := &file{p.Snapshot, -1, p.dir.prefix("port"), p.Port, new(IntCodec)}
+ port := &file{p.Snapshot, -1, p.dir.prefix("port"), p.Port, new(intCodec)}
port, err = port.Create()
if err != nil {
@@ -122,7 +122,7 @@ func (p *ProcType) GetInstances() (ins []*Instance, err error) {
func GetProcType(s Snapshot, app *App, name ProcessName) (p *ProcType, err error) {
path := app.dir.prefix(procsPath, string(name))
- port, err := s.getFile(path+"/port", new(IntCodec))
+ port, err := s.getFile(path+"/port", new(intCodec))
if err != nil {
return
}
View
@@ -89,7 +89,7 @@ func (r *Revision) Inspect() string {
func GetRevision(s Snapshot, app *App, ref string) (r *Revision, err error) {
path := app.dir.prefix(revsPath, ref)
- codec := new(StringCodec)
+ codec := new(stringCodec)
f, err := s.getFile(path+"/archive-url", codec)
if err != nil {
View
@@ -68,7 +68,7 @@ func (s Snapshot) GetScale(app string, revision string, processName string) (sca
// This is to avoid having to set the /apps/<app>/revs/<rev>/scale/<proc> paths to 0
// when registering revisions.
path := path.Join(appsPath, app, revsPath, revision, scalePath, processName)
- f, err := s.getFile(path, new(IntCodec))
+ f, err := s.getFile(path, new(intCodec))
// File doesn't exist, assume scale = 0
if IsErrNoEnt(err) {
@@ -121,7 +121,7 @@ func (s Snapshot) get(path string) (string, int64, error) {
}
// getFile returns the value at the specified path as a file, at this snapshot's revision
-func (s Snapshot) getFile(path string, codec Codec) (f *file, err error) {
+func (s Snapshot) getFile(path string, codec codec) (f *file, err error) {
bytes, rev, err := s.getBytes(path)
if err != nil {
return
@@ -132,7 +132,7 @@ func (s Snapshot) getFile(path string, codec Codec) (f *file, err error) {
return
}
- f = &file{dir: path, Value: value, FileRev: rev, Codec: codec, Snapshot: s}
+ f = &file{dir: path, Value: value, FileRev: rev, codec: codec, Snapshot: s}
return
}
@@ -203,7 +203,7 @@ func (s *Snapshot) fastForward(obj snapshotable, rev int64) snapshotable {
}
// getLatest returns the latest value for the given path
-func getLatest(s Snapshot, path string, codec Codec) (f *file, err error) {
+func getLatest(s Snapshot, path string, codec codec) (f *file, err error) {
evalue, rev, err := s.conn.Get(path, nil)
if err != nil {
return
@@ -214,7 +214,7 @@ func getLatest(s Snapshot, path string, codec Codec) (f *file, err error) {
return
}
- f = &file{dir: path, Value: value, Codec: codec, Snapshot: s.FastForward(rev)}
+ f = &file{dir: path, Value: value, codec: codec, Snapshot: s.FastForward(rev)}
return
}
View
@@ -110,11 +110,11 @@ func (t *Ticket) Create() (tt *Ticket, err error) {
t.Id = id
t.dir.Name = path.Join(ticketsPath, strconv.FormatInt(t.Id, 10))
- f, err := createFile(t.Snapshot, t.dir.prefix("op"), t.array(), new(ListCodec))
+ f, err := createFile(t.Snapshot, t.dir.prefix("op"), t.array(), new(listCodec))
if err != nil {
return
}
- f, err = createFile(t.Snapshot, t.dir.prefix("status"), string(t.Status), new(StringCodec))
+ f, err = createFile(t.Snapshot, t.dir.prefix("status"), string(t.Status), new(stringCodec))
if err == nil {
t.Snapshot = t.Snapshot.FastForward(f.FileRev)
}
@@ -277,7 +277,7 @@ func parseTicket(snapshot Snapshot, ev *doozer.Event, body []byte) (t *Ticket, e
p := path.Join(ticketsPath, idStr)
- f, err := snapshot.getFile(path.Join(p, "op"), new(ListCodec))
+ f, err := snapshot.getFile(path.Join(p, "op"), new(listCodec))
if err != nil {
return t, err
}
View
@@ -15,7 +15,7 @@
// import "soundcloud/visor"
//
// func main() {
-// client, err := visor.Dial("coordinator:8046", "/", new(visor.StringCodec))
+// client, err := visor.Dial("coordinator:8046", "/")
// if err != nil {
// panic(err)
// }
@@ -77,7 +77,7 @@ func Init(s Snapshot) (rev int64, err error) {
func ClaimNextPort(s Snapshot) (port int, err error) {
for {
- f, err := getLatest(s, nextPortPath, new(IntCodec))
+ f, err := getLatest(s, nextPortPath, new(intCodec))
if err == nil {
port = f.Value.(int)

0 comments on commit 111d944

Please sign in to comment.