rxhash

package module
v0.0.0-...-10b7a38 Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2023 License: MIT Imports: 4 Imported by: 3 modules, 3 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

rxhash GoDoc

rxhash is a Go library for creating a unique hash value for struct in Go, but data consistency.

What is data consistency?

import "github.com/rxwycdh/rxhash"
type A struct {
  Values []int
}

a1 := A{Values: []int{1,2,3}}
a2 := A{Values: []int{3,2,1}} // unorder but content is equal a1

fmt.Println(HashStruct(a) == HashStruct(b)) // true

So this library is useful for detecting whether the contents of a structure have changed.

Installation

Standard go get:

$ go get github.com/rxwycdh/rxhash

Qucik Start

A quick code example is shown below:

type Student struct {
    Name    string
    Address []string
    School  School
}

type School struct {
    Labels   map[string]any
    Teachers []Teacher
}

type Teacher struct {
    Subjects []string
}

// TEST1: hash equal if you change the order of the array in struct
student1 := Student{
    Name:    "xiaoming",
    Address: []string{"mumbai", "london", "tokyo", "seattle"},
    School: School{
        Labels: map[string]any{
            "phone":   "123456",
            "country": "China",
        },
    Teachers: []Teacher{{Subjects: []string{"math", "chinese", "art"}}},
    },
}

student1UnOrder := Student{
    Name:    "xiaoming",
    Address: []string{"mumbai", "london", "seattle", "tokyo"}, // **change this order!!**
    School: School{
        Labels: map[string]any{
            "phone":   "123456",
            "country": "China",
        },
    Teachers: []Teacher{{Subjects: []string{"math", "chinese", "art"}}},
    },
}

s1, _ := HashStruct(student1)
s2, _ := HashStruct(student1UnOrder)
fmt.Printf("student1 hash: %s, student2 hash: %s, student1 == student2 ? -> %t \n", s1, s2, s1 == s2)
// Output:
// student1 hash: 744398b55ba132754147289b30955aa4, student2 hash: 744398b55ba132754147289b30955aa4, student1 == student2 ? -> true

// TEST2: different attr in struct to calc hash
student3 := Student{
    // Name is different from student1, student1UnOrder
    Name:    "xiaohong",
    Address: []string{"mumbai", "london", "seattle", "tokyo"},
    School: School{
        Labels: map[string]any{
            "phone":   "123456",
            "country": "China",
        },
    Teachers: []Teacher{{Subjects: []string{"math", "chinese", "art"}}},
    },
}

s3, _ := HashStruct(student3)
fmt.Printf("student3 hash: %s", s3)
// Output:
// student3 hash: 3f3bbdb3dcc6e7645ce30a7ef58c2e58

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HashStruct

func HashStruct(target any) (string, error)

HashStruct Calculating a hash value for a structure can also be done correctly if the structure contains nested structures, such as map, slice. This is done by converting to json format and then sorting the json recursively.

func SortMap

func SortMap(target map[string]any) map[string]any

SortMap sort (nested)map

func SortSimpleMap

func SortSimpleMap(target map[string]any) map[string]any

SortSimpleMap sort simple map by keys, it would not work in which deep > 1.

func SortSlice

func SortSlice(target []any) []any

SortSlice sort slice, calculate the hash of the value to determine the position.

Types

This section is empty.

Jump to

Keyboard shortcuts

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