Skip to content

Instantly share code, notes, and snippets.

@slntopp
Last active November 18, 2021 10:43
Show Gist options
  • Save slntopp/000435b4443a9da511876d6fbe51b757 to your computer and use it in GitHub Desktop.
Save slntopp/000435b4443a9da511876d6fbe51b757 to your computer and use it in GitHub Desktop.
Coding Challenge | Hasher

Synopsis

We have a great, fast growing, microservices based service of storing whatever and their personal data. We are known for our compliance with all DataSecurity laws, and wanna keep going with that.

So, let's say we have some Golang struct like:

type PersonalData struct {
    Title string `json:"title"`
}

type Whatever struct {
    Uuid  string       `json:"uuid"`
    Data  PersonalData `json:"data"`
}

We have stored numerous "whatevers" in NoSQL database, for example:

DB.Write(
    Whatever{"a-b-c-d", PersonalData{"i'm whatever"}}
)

Which, despite we call them whatevers, have their PersonalData.

Next day, whatever with uuid a-b-c-d decided to call itself "i'm whoever" instead of "i'm whatever". Which means we need to change a title. Ofcourse we please whatever with this request. That's be:

we := DB.Get("a-b-c-d")
we.Data.Title = "i'm whoever"
DB.Write(we)

Good job so far!

But happens so, that there is another service, let's call it ServiceX, might be third-party or so, which needs to know whether personal data changed(updated), but can't read the data itself.

Let's just send it pings when we have update requests, won't we? Yeeeah, but no. Despite we process update request, we don't know if data actually changed, while that ServiceX is very lazy, so can be disturbed only and ONLY then, when data ACTUALLY changed.

Let's help ServiceX, let's make a hashsum!

Challenge

Base

Make a Marshaller(no need of unmarshaller) for structs, which would make hash check sum for any struct, e.g.:

import "hasher"

/// a few lines later

obj  := Whatever{"a-b-c-d", PersonalData{"i'm whatever"}}
hash := hasher.Marshall(obj)
fmt.Println(hash) // => "f714d0221fef51f2c0d26fa1ac1e3513"
// a few moments later
obj.Data.Title = "i'm whoever"
hash := hasher.Marshall(obj)
fmt.Println(hash) // => "605671cbae106e973f9b0f50dbbcf416"

Extra Point 1

Make it possible to define which fields must be "participating" in hashing. For example we add new field to struct PersonalData:

type PersonalData struct {
    Title string `json:"title"`
    Address string `json:"address" hasher:"+"`
}

So if we pass such object to our hasher.Marshall it will lookup only for the fields with "hasher" tag.

Extra Point 2

Make it working with nested structs.

Extra Point 3

Tests, comments, all that fancy things ;)

Remarks

  1. You choose any hashing algorythm you prefer.
  2. You can call your package and function any name you prefer.
  3. Package should be installable with go get and Go Modules
  4. Names, comments, everything must be in english.

You can contact me any with any questions at any time via:

Telegram E-Mail

Good luck and Happy Coding!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment