From ad88cb11c56eec286fd740c948247f25fade1109 Mon Sep 17 00:00:00 2001 From: Kevin Atkinson Date: Thu, 9 Aug 2018 00:04:26 -0400 Subject: [PATCH] Rename Format to Builder. --- format.go => builder.go | 30 +++++++++++++++--------------- format_test.go => builder_test.go | 5 ++--- cid.go | 2 +- 3 files changed, 18 insertions(+), 19 deletions(-) rename format.go => builder.go (62%) rename format_test.go => builder_test.go (91%) diff --git a/format.go b/builder.go similarity index 62% rename from format.go rename to builder.go index 331c613..524c03d 100644 --- a/format.go +++ b/builder.go @@ -4,23 +4,23 @@ import ( mh "github.com/multiformats/go-multihash" ) -type Format interface { +type Builder interface { Sum(data []byte) (*Cid, error) GetCodec() uint64 - WithCodec(uint64) Format + WithCodec(uint64) Builder } -type FormatV0 struct{} +type V0Builder struct{} -type FormatV1 struct { +type V1Builder struct { Codec uint64 HashFun uint64 HashLen int // HashLen <= 0 means the default length } -func PrefixToFormat(p Prefix) Format { +func PrefixToBuilder(p Prefix) Builder { if p.Version == 0 { - return FormatV0{} + return V0Builder{} } mhLen := p.MhLength if p.MhType == mh.ID { @@ -29,7 +29,7 @@ func PrefixToFormat(p Prefix) Format { if mhLen < 0 { mhLen = 0 } - return FormatV1{ + return V1Builder{ Codec: p.Codec, HashFun: p.MhType, HashLen: mhLen, @@ -40,7 +40,7 @@ func (p Prefix) GetCodec() uint64 { return p.Codec } -func (p Prefix) WithCodec(c uint64) Format { +func (p Prefix) WithCodec(c uint64) Builder { if c == p.Codec { return p } @@ -51,7 +51,7 @@ func (p Prefix) WithCodec(c uint64) Format { return p } -func (p FormatV0) Sum(data []byte) (*Cid, error) { +func (p V0Builder) Sum(data []byte) (*Cid, error) { hash, err := mh.Sum(data, mh.SHA2_256, -1) if err != nil { return nil, err @@ -59,18 +59,18 @@ func (p FormatV0) Sum(data []byte) (*Cid, error) { return NewCidV0(hash), nil } -func (p FormatV0) GetCodec() uint64 { +func (p V0Builder) GetCodec() uint64 { return DagProtobuf } -func (p FormatV0) WithCodec(c uint64) Format { +func (p V0Builder) WithCodec(c uint64) Builder { if c == DagProtobuf { return p } - return FormatV1{Codec: c, HashFun: mh.SHA2_256} + return V1Builder{Codec: c, HashFun: mh.SHA2_256} } -func (p FormatV1) Sum(data []byte) (*Cid, error) { +func (p V1Builder) Sum(data []byte) (*Cid, error) { mhLen := p.HashLen if mhLen <= 0 { mhLen = -1 @@ -82,11 +82,11 @@ func (p FormatV1) Sum(data []byte) (*Cid, error) { return NewCidV1(p.Codec, hash), nil } -func (p FormatV1) GetCodec() uint64 { +func (p V1Builder) GetCodec() uint64 { return p.Codec } -func (p FormatV1) WithCodec(c uint64) Format { +func (p V1Builder) WithCodec(c uint64) Builder { p.Codec = c return p } diff --git a/format_test.go b/builder_test.go similarity index 91% rename from format_test.go rename to builder_test.go index 3f74989..7a021ee 100644 --- a/format_test.go +++ b/builder_test.go @@ -10,7 +10,7 @@ func TestFormatV1(t *testing.T) { data := []byte("this is some test content") // Construct c1 - format := FormatV1{Codec: DagCBOR, HashFun: mh.SHA2_256} + format := V1Builder{Codec: DagCBOR, HashFun: mh.SHA2_256} c1, err := format.Sum(data) if err != nil { t.Fatal(err) @@ -35,7 +35,7 @@ func TestFormatV0(t *testing.T) { data := []byte("this is some test content") // Construct c1 - format := FormatV0{} + format := V0Builder{} c1, err := format.Sum(data) if err != nil { t.Fatal(err) @@ -55,4 +55,3 @@ func TestFormatV0(t *testing.T) { t.Fatal("prefixes mismatch") } } - diff --git a/cid.go b/cid.go index 6a1f36e..b9e0de4 100644 --- a/cid.go +++ b/cid.go @@ -445,7 +445,7 @@ func (c *Cid) Prefix() Prefix { // and the Multihash length. It does not contains // any actual content information. // NOTE: The use -1 in MhLength to mean default length is deprecated, -// use the PrefixV0 or PrefixV1 structures instead +// use the V0Builder or V1Builder structures instead type Prefix struct { Version uint64 Codec uint64