mirror of
https://github.com/steveiliop56/tinyauth.git
synced 2025-11-06 09:05:44 +00:00
feat: parse apps acl flags and env dynamically
This commit is contained in:
@@ -7,6 +7,60 @@ import (
|
||||
"github.com/stoewer/go-strcase"
|
||||
)
|
||||
|
||||
func ParsePath(parts []string, idx int, t reflect.Type) []string {
|
||||
if idx >= len(parts) {
|
||||
return []string{}
|
||||
}
|
||||
|
||||
if t.Kind() == reflect.Map {
|
||||
mapName := strings.ToLower(parts[idx])
|
||||
|
||||
if idx+1 >= len(parts) {
|
||||
return []string{mapName}
|
||||
}
|
||||
|
||||
elemType := t.Elem()
|
||||
keyEndIdx := idx + 1
|
||||
|
||||
if elemType.Kind() == reflect.Struct {
|
||||
for i := idx + 1; i < len(parts); i++ {
|
||||
found := false
|
||||
|
||||
for j := 0; j < elemType.NumField(); j++ {
|
||||
field := elemType.Field(j)
|
||||
if strings.EqualFold(parts[i], field.Name) {
|
||||
keyEndIdx = i
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if found {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
keyParts := parts[idx+1 : keyEndIdx]
|
||||
keyName := strings.ToLower(strings.Join(keyParts, "_"))
|
||||
|
||||
rest := ParsePath(parts, keyEndIdx, elemType)
|
||||
return append([]string{mapName, keyName}, rest...)
|
||||
}
|
||||
|
||||
if t.Kind() == reflect.Struct {
|
||||
for i := 0; i < t.NumField(); i++ {
|
||||
field := t.Field(i)
|
||||
if strings.EqualFold(parts[idx], field.Name) {
|
||||
rest := ParsePath(parts, idx+1, field.Type)
|
||||
return append([]string{strings.ToLower(field.Name)}, rest...)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return []string{}
|
||||
}
|
||||
|
||||
func normalizeKeys[T any](input map[string]string, root string, sep string) map[string]string {
|
||||
knownKeys := getKnownKeys[T]()
|
||||
normalized := make(map[string]string)
|
||||
@@ -74,3 +128,44 @@ func getKnownKeys[T any]() []string {
|
||||
|
||||
return keys
|
||||
}
|
||||
|
||||
func normalizeACLKeys[T any](input map[string]string, root string, sep string) map[string]string {
|
||||
normalized := make(map[string]string)
|
||||
var t T
|
||||
rootType := reflect.TypeOf(t)
|
||||
|
||||
for k, v := range input {
|
||||
parts := strings.Split(strings.ToLower(k), sep)
|
||||
|
||||
if len(parts) < 2 {
|
||||
continue
|
||||
}
|
||||
|
||||
if parts[0] != "tinyauth" {
|
||||
continue
|
||||
}
|
||||
|
||||
if parts[1] != root {
|
||||
continue
|
||||
}
|
||||
|
||||
if len(parts) > 2 {
|
||||
parsedParts := ParsePath(parts[2:], 0, rootType)
|
||||
|
||||
if len(parsedParts) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
final := "tinyauth"
|
||||
final += "." + root
|
||||
|
||||
for _, part := range parsedParts {
|
||||
final += "." + strcase.LowerCamelCase(part)
|
||||
}
|
||||
|
||||
normalized[final] = v
|
||||
}
|
||||
}
|
||||
|
||||
return normalized
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user