initial commits

master
Charles Iliya Krempeaux 2021-11-07 01:32:08 -07:00
parent 28e3af3f2d
commit 83995b0379
2 changed files with 97 additions and 0 deletions

11
errors.go 100644
View File

@ -0,0 +1,11 @@
package iid
import (
"errors"
)
var (
errBadRequest = errors.New("iid: bad request")
errNilReceiver = errors.New("iid: nil receiver")
errNothing = errors.New("iid: nothing")
)

86
iid.go 100644
View File

@ -0,0 +1,86 @@
package iid
// IID represents an interactionidentifier — i.e., an interactionID — i.e., an IID.
//
// It is a scheme used to generate quazimonotonicallyincreasingunique.
type IID struct {
value uint64
loaded bool
}
func Nothing() IID {
return IID{}
}
func something(value uint64) IID {
return IID{
value:value,
loaded:true,
}
}
// Generate creates a new interactionidentifier — i.e., an interactionID — i.e., an IID.
func Generate() IID {
var value uint64 = generate()
return something(value)
}
// Chaos returns the randomness that is embeddd in the interactionidentifier — i.e., an interactionID — i.e., an IID.
func (receiver IID) Chaos() (uint64, bool) {
if Nothing() == receiver {
return 0, false
}
_, value := decompile(receiver.value)
return value, true
}
func (receiver IID) String() string {
if Nothing() == receiver {
return ""
}
var serialized string = serialize(receiver.value)
return serialized
}
func (receiver IID) MarshalText() (text []byte, err error) {
if Nothing() == receiver {
return nil, errNothing
}
var serialized string = serialize(receiver.value)
return []byte(serialized), nil
}
// UnixTime returns the unixtime that is embeddd in the interactionidentifier — i.e., an interactionID — i.e., an IID.
func (receiver IID) UnixTime() (int64, bool) {
if Nothing() == receiver {
return 0, false
}
value, _ := decompile(receiver.value)
return int64(value), true
}
func (receiver *IID) UnmarshalText(p []byte) error {
if nil == receiver {
return errNilReceiver
}
var serialized string = string(p)
value, successful := unserialize(serialized)
if !successful {
return errBadRequest
}
*receiver = something(value)
return nil
}