Skip to content

Instantly share code, notes, and snippets.

@manakuro
Created December 10, 2021 04:54
Show Gist options
  • Save manakuro/0cfd94e4cebfbfdd43a463413df3266f to your computer and use it in GitHub Desktop.
Save manakuro/0cfd94e4cebfbfdd43a463413df3266f to your computer and use it in GitHub Desktop.
package ent
import (
"context"
"fmt"
"golang-clean-architecture-ent-gqlgen/ent/schema/ulid"
"golang-clean-architecture-ent-gqlgen/pkg/const/globalid"
)
var globalIDs = globalid.New()
// IDToType maps an ulid.ID to the underlying table.
func IDToType(_ context.Context, id ulid.ID) (string, error) {
if len(id) < 3 {
return "", fmt.Errorf("IDToType: id too short")
}
prefix := id[:3]
t, err := globalIDs.FindTableByID(string(prefix))
if err != nil {
return "", fmt.Errorf("IDToType: could not map prefix '%s' to a type", prefix)
}
return t, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment