Skip to content

Commit

Permalink
[typeid] Unify typed and untyped versions (#187)
Browse files Browse the repository at this point in the history
## Summary
Unifies typed and untyped versions of typeid. Basically we now have a
single version of the library that uses generics.
To support the "untyped" case where you might be dealing with an
arbitrary (and unknown) prefix from a string, we have
a special `AnyPrefix` type.

A follow up PR will: make `Nil` equal to the empty struct. I also think
I can incorporate Mike's changes so that they are optional (that way the
user can choose whether they want to use reflection or not).

Note that in order to fully support the "typed" and "untyped" cases, I
often need to create two versions of a method:
+ The typed version *only* takes a Prefix Type, and it "infers" the
prefix string from the type.
+ The untyped version takes the prefix as a string, and fixes the type
of the typeid to `TypeID[AnyPrefix]`

The main thing I'd like feedback on is whether we like this dual method
approach, and whether we like the names I've picked. As an example, we
used to have:
```
typeid.New("prefix")
```

But now there's:
```
typeid.New[PrefixType]()
// and
typeid.FromPrefix("prefix")
```

Do we like that? Do we prefer to keep the name `New("prefix")` for the
"untyped" version, and introduce a new `NewTyped[Prefix]()` version?

Similar questions happen between, say `FromUUID` and the new
`FromUUIDPrefix`.

## How was it tested?
All tests still pass.
  • Loading branch information
loreto committed Nov 14, 2023
1 parent 68353ae commit a5cef26
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 2 deletions.
2 changes: 1 addition & 1 deletion cli/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func encodeCmd(cmd *cobra.Command, args []string) error {
prefix = args[0]
uuid = args[1]
}
tid, err := typeid.FromUUID(prefix, uuid)
tid, err := typeid.FromUUID[typeid.AnyID](prefix, uuid)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cli/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func newCmd(cmd *cobra.Command, args []string) error {
if len(args) > 0 {
prefix = strings.ToLower(args[0])
}
tid, err := typeid.New(prefix)
tid, err := typeid.WithPrefix(prefix)
if err != nil {
return err
}
Expand Down

0 comments on commit a5cef26

Please sign in to comment.