Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
b5 committed Jul 27, 2017
1 parent 813e79f commit 12fb9fd
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 10 deletions.
4 changes: 3 additions & 1 deletion dataFormat.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"fmt"
)

var ErrUnknownDataFormat = fmt.Errorf("Unknown Data Format")

// DataFormat represents different types of data
type DataFormat int

Expand Down Expand Up @@ -63,7 +65,7 @@ func ParseDataFormatString(s string) (df DataFormat, err error) {
// MarshalJSON satisfies the json.Marshaler interface
func (f DataFormat) MarshalJSON() ([]byte, error) {
if f == UnknownDataFormat {
return nil, nil
return nil, ErrUnknownDataFormat
}
return []byte(fmt.Sprintf(`"%s"`, f.String())), nil
}
Expand Down
14 changes: 9 additions & 5 deletions hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@ import (
"github.com/multiformats/go-multihash"
)

// CalcHash calculates the hash of a json.Marshaler
// TODO - this will have to place nice with IPFS block hashing strategies
func CalcHash(m json.Marshaler) (hash string, err error) {
h := sha256.New()

// JSONHash calculates the hash of a json.Marshaler
func JSONHash(m json.Marshaler) (hash string, err error) {
// marshal to cannoncical JSON representation
data, err := m.MarshalJSON()
if err != nil {
return
}
return HashBytes(data)
}

// TODO - this will have to place nice with IPFS block hashing strategies
func HashBytes(data []byte) (hash string, err error) {
h := sha256.New()

if _, err = h.Write(data); err != nil {
return
}
Expand Down
28 changes: 28 additions & 0 deletions hash_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package dataset

import (
"testing"
)

func TestHashBytes(t *testing.T) {
cases := []struct {
in []byte
out string
err error
}{
{[]byte(""), "1220e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", nil},
}

for i, c := range cases {
got, err := HashBytes(c.in)
if err != c.err {
t.Errorf("case %d error mismatch. expected: %s got: %s", i, c.err, err)
continue
}

if got != c.out {
t.Errorf("case %d result mismatch. expected: %s got: %s", i, c.out, got)
continue
}
}
}
3 changes: 1 addition & 2 deletions resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package dataset

import (
"encoding/json"
"errors"
"github.com/ipfs/go-datastore"
)

Expand Down Expand Up @@ -40,7 +39,7 @@ type Resource struct {

// Hash gives the hash of this resource
func (r *Resource) Hash() (string, error) {
return CalcHash(r)
return JSONHash(r)
}

// truthCount returns the number of arguments that are true
Expand Down
27 changes: 25 additions & 2 deletions resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,29 @@ import (
"testing"
)

func TestResouceHash(t *testing.T) {
cases := []struct {
r *Resource
hash string
err error
}{
{&Resource{Format: CsvDataFormat}, "1220c2f881931bffda4b33de1fcc9c6085b4d4b9dcc5d18083d97c6415c1a3590b66", nil},
}

for i, c := range cases {
hash, err := c.r.Hash()
if err != c.err {
t.Errorf("case %d error mismatch. expected %s, got %s", i, c.err, err)
continue
}

if hash != c.hash {
t.Errorf("case %d hash mismatch. expected %s, got %s", i, c.hash, hash)
continue
}
}
}

func TestResourceUnmarshalJSON(t *testing.T) {
cases := []struct {
FileName string
Expand All @@ -30,7 +53,7 @@ func TestResourceUnmarshalJSON(t *testing.T) {
continue
}

if err = ResourceEqual(ds, c.result); err != nil {
if err = CompareResources(ds, c.result); err != nil {
t.Errorf("case %d resource comparison error: %s", i, err)
continue
}
Expand All @@ -42,7 +65,7 @@ func TestResourceMarshalJSON(t *testing.T) {

}

func ResourceEqual(a, b *Resource) error {
func CompareResources(a, b *Resource) error {
if a == nil && b == nil {
return nil
} else if a == nil && b != nil || a != nil && b == nil {
Expand Down

0 comments on commit 12fb9fd

Please sign in to comment.