Giter Site home page Giter Site logo

typeid-go's Introduction

TypeID Go

A golang implementation of TypeIDs

License: Apache 2.0 Go Reference

TypeIDs are a modern, type-safe, globally unique identifier based on the upcoming UUIDv7 standard. They provide a ton of nice properties that make them a great choice as the primary identifiers for your data in a database, APIs, and distributed systems. Read more about TypeIDs in their spec.

This particular implementation provides a go library for generating and parsing TypeIDs.

Installation

To add this library as a dependency in your go module, run:

go get go.jetify.com/typeid

Usage

This library provides a go implementation of TypeID that allows you to define your own custom id types for added compile-time safety.

If you don't need compile-time safety, you can use the provided typeid.AnyID directly:

import (
  "go.jetify.com/typeid"
)

func example() {
  tid, _ := typeid.WithPrefix("user")
  fmt.Println(tid)
}

If you want compile-time safety, define your own custom types with two steps:

  1. Define a struct the implements the method Prefix. Prefix should return the string that should be used as the prefix for your custom type.
  2. Define you own id type, by embedding typeid.TypeID[CustomPrefix]

For example to define a UserID with prefix user:

import (
  "go.jetify.com/typeid"
)

// Define the prefix:
type UserPrefix struct {}
func (UserPrefix) Prefix() string { return "user" }

// Define UserID:
type UserID struct {
	typeid.TypeID[UserPrefix]
}

Now you can use the UserID type to generate new ids:

import (
  "go.jetify.com/typeid"
)

func example() {
  tid, _ := typeid.New[UserID]()
  fmt.Println(tid)
}

For the full documentation, see this package's godoc.

typeid-go's People

Contributors

broothie avatar fernandezpablo85 avatar gcurtis avatar github-actions[bot] avatar loreto avatar lucilleh avatar mikeland73 avatar webrat avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

typeid-go's Issues

implement the standard interfaces

Both the typed and untyped version should consider implementing MarshalText and MarshalBinary, probably as well as Decoder and Valuer.

Opening this issue because I'm not sure what the defaults should be; always marshalling to/from a prefix, or no prefix? Does it make sense to keep the prefix in the case of a database field?

Type error after upgrading to 1.0.0

Hi!

I recently upgraded to v1.0.0 and now I'm getting this type error Cannot use *T as the type SubtypePtr[T].

You can see it's happening for the example code taken from the readme.

Screenshot 2024-01-08 at 10 54 54 AM

Thanks!

Generics drastically increased my integration of typeid

I just bumped typeid-go and now with generics my integration has gone from 8 constants to 8 constants and 16 structs, or in lines from 14 to 78. What have I gained? Nothing.

Workaround is to use strings.HasPrefix for parsing.

Unit Test of Base32 implementation

I am not sure, but could you please double check the correctness of base32 implementation?

Both, my rust implementation and this website give me different result for
[00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f] input. My 000g40r40m30e209185gr38e1w vs this repo's 00041061050r3gg28a1c60t3gf.

Define binary representation for TypeIDs and implement binary marshaling

As suggested in #4, typeids could implement the encoding.BinaryMarshaler and encoding.BinaryUnmarshaler interfaces.

To do so, we would first have to define in the spec the standard binary representation for typeids. Options include:

  • Re-use the string representation and encoded in UTF-8
  • Instead of encoding the prefix as the bytes of a base32 string, be more efficient by using the "raw" 128-bit representation of the UUID. A follow up question would be how to distinguish between the prefix and suffix fields in the binary representation (use a separator? include an integer indicating the length of the prefix?)

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.