structhash

package
v0.0.0-...-d69b078 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 12, 2015 License: ISC, Apache-2.0 Imports: 6 Imported by: 0

README

structhash

structhash is a Go library for generating hash strings of arbitrary data structures.

Installation

Standard go get:

$ go get github.com/cnf/structhash

Documentation

For usage and examples see the Godoc.

Documentation

Overview

Package structhash creates hash strings from arbitrary go data structures.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Hash

func Hash(c interface{}, version int) (string, error)

Hash takes a data structure and returns a hash string of that data structure at the version asked

Example
type Person struct {
	Name   string
	Age    int
	Emails []string
	Extra  map[string]string
	Spouse *Person
}
bill := &Person{
	Name:   "Bill",
	Age:    24,
	Emails: []string{"bob@foo.org", "bob@bar.org"},
	Extra: map[string]string{
		"facebook": "Bob42",
	},
}
bob := &Person{
	Name:   "Bob",
	Age:    42,
	Emails: []string{"bob@foo.org", "bob@bar.org"},
	Extra: map[string]string{
		"facebook": "Bob42",
	},
	Spouse: bill,
}

hash, err := Hash(bob, 1)
if err != nil {
	panic(err)
}
fmt.Printf("%s", hash)
Output:

v1_55743877f3ffd5fc834e97bc43a6e7bd
Example (Version)
type Person struct {
	Name   string            `version:"1"`
	Age    int               `version:"1"`
	Emails []string          `version:"1"`
	Extra  map[string]string `version:"1" lastversion:"2"`
	Spouse *Person           `version:"2"`
}
bill := &Person{
	Name:   "Bill",
	Age:    24,
	Emails: []string{"bob@foo.org", "bob@bar.org"},
	Extra: map[string]string{
		"facebook": "Bob42",
	},
}
bob := &Person{
	Name:   "Bob",
	Age:    42,
	Emails: []string{"bob@foo.org", "bob@bar.org"},
	Extra: map[string]string{
		"facebook": "Bob42",
	},
	Spouse: bill,
}
hashV1, err := Hash(bob, 1)
if err != nil {
	panic(err)
}
hashV2, err := Hash(bob, 2)
if err != nil {
	panic(err)
}
hashV3, err := Hash(bob, 3)
if err != nil {
	panic(err)
}
fmt.Printf("%s\n", hashV1)
fmt.Printf("%s\n", hashV2)
fmt.Printf("%s\n", hashV3)
Output:

v1_461558d2570e10f79693e34ea309d1ad
v2_d00068b9441e09d87689c7cb06a646a1
v3_b5b651c6650939ef4d063d05caa5c778

func Version

func Version(h string) int

Version returns the version of the supplied hash as an integer or -1 on failure

Example
// A hash string gotten from Hash(). Returns the version as an int.
i := Version("v1_55743877f3ffd5fc834e97bc43a6e7bd")
fmt.Printf("%d", i)
Output:

1
Example (Errors)
// A hash string gotten from Hash(). Returns -1 on error.
i := Version("va_55743877f3ffd5fc834e97bc43a6e7bd")
fmt.Printf("%d", i)
Output:

-1

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL