feat(training): add helper methods to TrainingID type
- String() - MarshalJSON() - UnmarshalJSON()
This commit is contained in:
parent
67b12fc7af
commit
6df8747065
1 changed files with 22 additions and 1 deletions
|
|
@ -1,6 +1,10 @@
|
|||
package training
|
||||
|
||||
import "github.com/google/uuid"
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type TrainingID uuid.UUID
|
||||
|
||||
|
|
@ -8,6 +12,23 @@ func NewTrainingID() TrainingID {
|
|||
return TrainingID(uuid.Must(uuid.NewV7()))
|
||||
}
|
||||
|
||||
func (id TrainingID) String() string {
|
||||
return uuid.UUID(id).String()
|
||||
}
|
||||
|
||||
func (id TrainingID) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(uuid.UUID(id))
|
||||
}
|
||||
|
||||
func (id *TrainingID) UnmarshalJSON(data []byte) error {
|
||||
var u uuid.UUID
|
||||
if err := json.Unmarshal(data, &u); err != nil {
|
||||
return err
|
||||
}
|
||||
*id = TrainingID(u)
|
||||
return nil
|
||||
}
|
||||
|
||||
type Training struct {
|
||||
ID TrainingID
|
||||
Name string
|
||||
|
|
|
|||
Reference in a new issue