This commit is contained in:
parent
b6f12b7acf
commit
8f8ad3035a
33 changed files with 166 additions and 111 deletions
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
Tool for generating helper functions for storage.Role structs inside of the storage package
|
||||
Tool for generating helper functions for [new-storage.Role] structs inside of the storage package
|
||||
It generates the following functions:
|
||||
- CollapseRolesIntoOne: Collapse a list of roles into one singular role. Each value will be set to the
|
||||
value of the role with the highest priority
|
||||
|
@ -116,7 +116,12 @@ func main() {
|
|||
|
||||
// Build role collapse function
|
||||
outBuilder.WriteString(
|
||||
`func CollapseRolesIntoOne(roles ...models.Role) models.Role {
|
||||
`// CollapseRolesIntoOne takes a list of roles and collapses them down into one.
|
||||
// It ensures to follow the priority of each role.
|
||||
// All results will use [models.DefaultUserRole] as the baseline.
|
||||
// The resulting role will have each entry filled with the value of the highest priority.
|
||||
// If multiple roles have the same priority, the order in which they are applied is not stable.
|
||||
func CollapseRolesIntoOne(roles ...models.Role) models.Role {
|
||||
startingRole := RoleDeepCopy(models.DefaultUserRole)
|
||||
slices.SortFunc(roles, func(a, b models.Role) int { return int(int64(a.Priority)-int64(b.Priority)) })
|
||||
for _, role := range roles {
|
||||
|
@ -143,7 +148,12 @@ func main() {
|
|||
`)
|
||||
|
||||
// Then build the deep copy function
|
||||
outBuilder.WriteString("\nfunc RoleDeepCopy(o models.Role) models.Role {\n")
|
||||
outBuilder.WriteString(`
|
||||
// RoleDeepCopy performs a deep copy of a given role.
|
||||
// Each element will point to a newly stored value.
|
||||
// The new and old role will contain identical information.
|
||||
func RoleDeepCopy(o models.Role) models.Role {
|
||||
`)
|
||||
outBuilder.WriteString(` n := models.Role{}
|
||||
n.Model = o.Model
|
||||
n.Name = o.Name
|
||||
|
@ -165,7 +175,10 @@ func main() {
|
|||
outBuilder.WriteString(" return n\n}\n\n")
|
||||
|
||||
// Build compare function
|
||||
outBuilder.WriteString("func CompareRoles(a, b *models.Role) bool {\n")
|
||||
outBuilder.WriteString(`// Compare two roles for equality.
|
||||
// If a permission is nil in one of the roles, that permission is ignored.
|
||||
func CompareRoles(a, b *models.Role) bool {
|
||||
`)
|
||||
outBuilder.WriteString(" return ")
|
||||
lastName, lastType := "", ""
|
||||
for valName, valType := range nameTypeMap {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue