-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(resource): change ID to UUID
Use UUID instead of ID for resources. The value is a UUID so should be called as such to match with other UUID of other entites.
- Loading branch information
Showing
14 changed files
with
126 additions
and
126 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright 2024 Canonical Ltd. | ||
// Licensed under the AGPLv3, see LICENCE file for details. | ||
|
||
package resource | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/juju/errors" | ||
|
||
"github.com/juju/juju/internal/uuid" | ||
) | ||
|
||
// UUID represents a resource unique identifier. | ||
type UUID string | ||
|
||
// NewUUID is a convince function for generating a new resource uuid. | ||
func NewUUID() (UUID, error) { | ||
id, err := uuid.NewUUID() | ||
if err != nil { | ||
return UUID(""), err | ||
} | ||
return UUID(id.String()), nil | ||
} | ||
|
||
// ParseUUID returns a new UUID from the given string. If the string is not a | ||
// valid uuid an error satisfying [errors.NotValid] will be returned. | ||
func ParseUUID(value string) (UUID, error) { | ||
if !uuid.IsValidUUIDString(value) { | ||
return "", fmt.Errorf("id %q %w", value, errors.NotValid) | ||
} | ||
return UUID(value), nil | ||
} | ||
|
||
// String implements the stringer interface for UUID. | ||
func (u UUID) String() string { | ||
return string(u) | ||
} | ||
|
||
// Validate ensures the consistency of the UUID. If the uuid is invalid an error | ||
// satisfying [errors.NotValid] will be returned. | ||
func (u UUID) Validate() error { | ||
if u == "" { | ||
return fmt.Errorf("%wuuid cannot be empty", errors.Hide(errors.NotValid)) | ||
} | ||
if !uuid.IsValidUUIDString(string(u)) { | ||
return fmt.Errorf("uuid %q %w", u, errors.NotValid) | ||
} | ||
return nil | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.