Created
December 10, 2021 04:54
-
-
Save manakuro/0cfd94e4cebfbfdd43a463413df3266f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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