Code Monkey home page Code Monkey logo

structhash's Introduction

structhash GoDoc Build Status

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

Features

  • fields may be ignored or renamed (like in json.Marshal, but using different struct tag)
  • fields may be versioned
  • fields order in struct doesn't matter (unlike json.Marshal)
  • nil values are treated equally to zero values

Installation

Standard go get:

$ go get github.com/cnf/structhash

Documentation

For usage and examples see the Godoc.

Quick start

package main

import (
    "fmt"
    "crypto/md5"
    "crypto/sha1"
    "github.com/cnf/structhash"
)

type S struct {
    Str string
    Num int
}

func main() {
    s := S{"hello", 123}

    hash, err := structhash.Hash(s, 1)
    if err != nil {
        panic(err)
    }
    fmt.Println(hash)
    // Prints: v1_41011bfa1a996db6d0b1075981f5aa8f

    fmt.Println(structhash.Version(hash))
    // Prints: 1

    fmt.Printf("%x\n", structhash.Md5(s, 1))
    // Prints: 41011bfa1a996db6d0b1075981f5aa8f

    fmt.Printf("%x\n", structhash.Sha1(s, 1))
    // Prints: 5ff72df7212ce8c55838fb3ec6ad0c019881a772

    fmt.Printf("%x\n", md5.Sum(structhash.Dump(s, 1)))
    // Prints: 41011bfa1a996db6d0b1075981f5aa8f

    fmt.Printf("%x\n", sha1.Sum(structhash.Dump(s, 1)))
    // Prints: 5ff72df7212ce8c55838fb3ec6ad0c019881a772
}

Struct tags

structhash supports struct tags in the following forms:

  • hash:"-", or
  • hash:"name:{string} version:{number} lastversion:{number}"

All fields are optional and may be ommitted. Their semantics is:

  • - - ignore field
  • name:{string} - rename field (may be useful when you want to rename field but keep hashes unchanged for backward compatibility)
  • version:{number} - ignore field if version passed to structhash is smaller than given number
  • lastversion:{number} - ignore field if version passed to structhash is greater than given number

Example:

type MyStruct struct {
    Ignored string `hash:"-"`
    Renamed string `hash:"name:OldName version:1"`
    Legacy  string `hash:"version:1 lastversion:2"`
}

Nil values

When hash is calculated, nil pointers, nil slices, and nil maps are treated equally to zero values of corresponding type. E.g., nil pointer to string is equivalent to empty string, and nil slice is equivalent to empty slice.

structhash's People

Contributors

cnf avatar gavv avatar robertogyn19 avatar rowdyroad avatar tmaiaroto avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.