Refactor: migrate POSIX scaffolding to CBOR and JSONC
This commit is contained in:
@@ -26,7 +26,7 @@ func (service *Service) ensureBootstrapPOSIXSkeleton(
|
||||
return fmt.Errorf("create POSIX root: %w", err)
|
||||
}
|
||||
|
||||
if err := writeJSONFile(filepath.Join(rootPath, "settings.json"), map[string]any{
|
||||
if err := writeCBORFile(filepath.Join(rootPath, posixSettingsFileName), map[string]any{
|
||||
"installation": map[string]any{
|
||||
"id": installation.ID,
|
||||
"name": installation.Name,
|
||||
@@ -42,17 +42,17 @@ func (service *Service) ensureBootstrapPOSIXSkeleton(
|
||||
"slug": organization.Slug,
|
||||
},
|
||||
}); err != nil {
|
||||
return fmt.Errorf("write tenant settings.json: %w", err)
|
||||
return fmt.Errorf("write tenant %s: %w", posixSettingsFileName, err)
|
||||
}
|
||||
|
||||
if err := writeJSONFile(filepath.Join(rootPath, "layout.json"), map[string]any{
|
||||
if err := writeCBORFile(filepath.Join(rootPath, posixLayoutFileName), map[string]any{
|
||||
"version": 1,
|
||||
"type": "tenant-layout",
|
||||
"home": map[string]any{
|
||||
"defaultProjectSlug": project.Slug,
|
||||
},
|
||||
}); err != nil {
|
||||
return fmt.Errorf("write tenant layout.json: %w", err)
|
||||
return fmt.Errorf("write tenant %s: %w", posixLayoutFileName, err)
|
||||
}
|
||||
|
||||
if err := os.MkdirAll(filepath.Join(rootPath, "catalog", "packs"), 0o755); err != nil {
|
||||
@@ -63,6 +63,67 @@ func (service *Service) ensureBootstrapPOSIXSkeleton(
|
||||
return fmt.Errorf("create catalog standalone root: %w", err)
|
||||
}
|
||||
|
||||
corePackPath := filepath.Join(rootPath, "catalog", "packs", "pack-core")
|
||||
corePackEntriesPath := filepath.Join(corePackPath, "entries")
|
||||
coreAppEntryPath := filepath.Join(corePackEntriesPath, "app-shell")
|
||||
standaloneAppPath := filepath.Join(rootPath, "catalog", "standalone", "app-shell")
|
||||
|
||||
for _, dirPath := range []string{corePackPath, corePackEntriesPath, coreAppEntryPath, standaloneAppPath} {
|
||||
if err := os.MkdirAll(dirPath, 0o755); err != nil {
|
||||
return fmt.Errorf("create catalog directory %s: %w", dirPath, err)
|
||||
}
|
||||
}
|
||||
|
||||
if err := writeJSONCFile(filepath.Join(corePackPath, posixManifestFileName), map[string]any{
|
||||
"id": "pack-core",
|
||||
"slug": "core",
|
||||
"type": "pack",
|
||||
"name": "Core Pack",
|
||||
"version": "0.1.0",
|
||||
"description": "First-party shared package of starter Moku surfaces.",
|
||||
"entries": []map[string]any{{
|
||||
"slug": "app-shell",
|
||||
"path": "entries/app-shell",
|
||||
}},
|
||||
"capabilities": []string{"first-party", "bootstrap", "catalog-pack"},
|
||||
}); err != nil {
|
||||
return fmt.Errorf("write catalog pack %s: %w", posixManifestFileName, err)
|
||||
}
|
||||
|
||||
if err := writeJSONCFile(filepath.Join(coreAppEntryPath, posixManifestFileName), map[string]any{
|
||||
"id": "app-shell",
|
||||
"slug": "app-shell",
|
||||
"type": "app",
|
||||
"name": "App Shell",
|
||||
"version": "0.1.0",
|
||||
"description": "Primary shell surface for navigating the installation.",
|
||||
"source": "pack-core",
|
||||
"runtime": map[string]any{
|
||||
"kind": "route",
|
||||
"path": "/v1/app-shell",
|
||||
},
|
||||
"capabilities": []string{"shell", "navigation", "workspace"},
|
||||
}); err != nil {
|
||||
return fmt.Errorf("write catalog entry %s: %w", posixManifestFileName, err)
|
||||
}
|
||||
|
||||
if err := writeJSONCFile(filepath.Join(standaloneAppPath, posixManifestFileName), map[string]any{
|
||||
"id": "app-shell",
|
||||
"slug": "app-shell",
|
||||
"type": "app",
|
||||
"name": "App Shell",
|
||||
"version": "0.1.0",
|
||||
"description": "Standalone registration for the primary shell surface.",
|
||||
"source": "standalone",
|
||||
"runtime": map[string]any{
|
||||
"kind": "route",
|
||||
"path": "/v1/app-shell",
|
||||
},
|
||||
"capabilities": []string{"shell", "navigation", "standalone"},
|
||||
}); err != nil {
|
||||
return fmt.Errorf("write standalone entry %s: %w", posixManifestFileName, err)
|
||||
}
|
||||
|
||||
departmentPath := filepath.Join(rootPath, "departments", slugDir("department", department.Slug))
|
||||
teamPath := filepath.Join(departmentPath, "teams", slugDir("team", team.Slug))
|
||||
projectPath := filepath.Join(rootPath, "projects", slugDir("project", project.Slug))
|
||||
@@ -89,107 +150,107 @@ func (service *Service) ensureBootstrapPOSIXSkeleton(
|
||||
}
|
||||
}
|
||||
|
||||
if err := writeJSONFile(filepath.Join(departmentPath, "settings.json"), map[string]any{
|
||||
if err := writeCBORFile(filepath.Join(departmentPath, posixSettingsFileName), map[string]any{
|
||||
"id": department.ID,
|
||||
"name": department.Name,
|
||||
"slug": department.Slug,
|
||||
"type": "department",
|
||||
}); err != nil {
|
||||
return fmt.Errorf("write department settings.json: %w", err)
|
||||
return fmt.Errorf("write department %s: %w", posixSettingsFileName, err)
|
||||
}
|
||||
|
||||
if err := writeJSONFile(filepath.Join(departmentPath, "users.json"), map[string]any{
|
||||
if err := writeCBORFile(filepath.Join(departmentPath, posixUsersFileName), map[string]any{
|
||||
"owners": []map[string]string{{
|
||||
"id": admin.ID,
|
||||
"email": admin.Email,
|
||||
"displayName": admin.DisplayName,
|
||||
}},
|
||||
}); err != nil {
|
||||
return fmt.Errorf("write department users.json: %w", err)
|
||||
return fmt.Errorf("write department %s: %w", posixUsersFileName, err)
|
||||
}
|
||||
|
||||
if err := writeJSONFile(filepath.Join(teamPath, "settings.json"), map[string]any{
|
||||
if err := writeCBORFile(filepath.Join(teamPath, posixSettingsFileName), map[string]any{
|
||||
"id": team.ID,
|
||||
"name": team.Name,
|
||||
"slug": team.Slug,
|
||||
"type": "team",
|
||||
}); err != nil {
|
||||
return fmt.Errorf("write team settings.json: %w", err)
|
||||
return fmt.Errorf("write team %s: %w", posixSettingsFileName, err)
|
||||
}
|
||||
|
||||
if err := writeJSONFile(filepath.Join(teamPath, "users.json"), map[string]any{
|
||||
if err := writeCBORFile(filepath.Join(teamPath, posixUsersFileName), map[string]any{
|
||||
"owners": []map[string]string{{
|
||||
"id": admin.ID,
|
||||
"email": admin.Email,
|
||||
"displayName": admin.DisplayName,
|
||||
}},
|
||||
}); err != nil {
|
||||
return fmt.Errorf("write team users.json: %w", err)
|
||||
return fmt.Errorf("write team %s: %w", posixUsersFileName, err)
|
||||
}
|
||||
|
||||
if err := writeJSONFile(filepath.Join(projectPath, "settings.json"), map[string]any{
|
||||
if err := writeCBORFile(filepath.Join(projectPath, posixSettingsFileName), map[string]any{
|
||||
"id": project.ID,
|
||||
"name": project.Name,
|
||||
"slug": project.Slug,
|
||||
"type": "project",
|
||||
}); err != nil {
|
||||
return fmt.Errorf("write project settings.json: %w", err)
|
||||
return fmt.Errorf("write project %s: %w", posixSettingsFileName, err)
|
||||
}
|
||||
|
||||
if err := writeJSONFile(filepath.Join(projectPath, "home.json"), map[string]any{
|
||||
if err := writeCBORFile(filepath.Join(projectPath, posixHomeFileName), map[string]any{
|
||||
"type": "project-home",
|
||||
"title": project.Name,
|
||||
}); err != nil {
|
||||
return fmt.Errorf("write project home.json: %w", err)
|
||||
return fmt.Errorf("write project %s: %w", posixHomeFileName, err)
|
||||
}
|
||||
|
||||
if err := writeJSONFile(filepath.Join(projectPath, "acl.json"), map[string]any{
|
||||
if err := writeCBORFile(filepath.Join(projectPath, posixACLFileName), map[string]any{
|
||||
"version": 1,
|
||||
"inherits": true,
|
||||
"rules": []any{},
|
||||
}); err != nil {
|
||||
return fmt.Errorf("write project acl.json: %w", err)
|
||||
return fmt.Errorf("write project %s: %w", posixACLFileName, err)
|
||||
}
|
||||
|
||||
if err := writeJSONFile(filepath.Join(usersPath, "settings.json"), map[string]any{
|
||||
if err := writeCBORFile(filepath.Join(usersPath, posixSettingsFileName), map[string]any{
|
||||
"primaryAdminId": admin.ID,
|
||||
}); err != nil {
|
||||
return fmt.Errorf("write users settings.json: %w", err)
|
||||
return fmt.Errorf("write users %s: %w", posixSettingsFileName, err)
|
||||
}
|
||||
|
||||
if err := writeJSONFile(filepath.Join(usersPath, "data.json"), map[string]any{
|
||||
if err := writeCBORFile(filepath.Join(usersPath, posixDataFileName), map[string]any{
|
||||
"admins": []map[string]string{{
|
||||
"id": admin.ID,
|
||||
"email": admin.Email,
|
||||
"displayName": admin.DisplayName,
|
||||
}},
|
||||
}); err != nil {
|
||||
return fmt.Errorf("write users data.json: %w", err)
|
||||
return fmt.Errorf("write users %s: %w", posixDataFileName, err)
|
||||
}
|
||||
|
||||
if err := writeJSONFile(filepath.Join(personalHomePath, "settings.json"), map[string]any{
|
||||
if err := writeCBORFile(filepath.Join(personalHomePath, posixSettingsFileName), map[string]any{
|
||||
"type": "personal",
|
||||
"name": personalName,
|
||||
"slug": personalSlug,
|
||||
}); err != nil {
|
||||
return fmt.Errorf("write personal settings.json: %w", err)
|
||||
return fmt.Errorf("write personal %s: %w", posixSettingsFileName, err)
|
||||
}
|
||||
|
||||
if err := writeJSONFile(filepath.Join(personalHomePath, "layout.json"), map[string]any{
|
||||
if err := writeCBORFile(filepath.Join(personalHomePath, posixLayoutFileName), map[string]any{
|
||||
"version": 1,
|
||||
"type": "personal-layout",
|
||||
"home": map[string]any{
|
||||
"defaultProjectSlug": project.Slug,
|
||||
},
|
||||
}); err != nil {
|
||||
return fmt.Errorf("write personal layout.json: %w", err)
|
||||
return fmt.Errorf("write personal %s: %w", posixLayoutFileName, err)
|
||||
}
|
||||
|
||||
if err := writeJSONFile(filepath.Join(personalHomePath, "home.json"), map[string]any{
|
||||
if err := writeCBORFile(filepath.Join(personalHomePath, posixHomeFileName), map[string]any{
|
||||
"type": "personal-home",
|
||||
"title": personalHomeTitle(personalName),
|
||||
}); err != nil {
|
||||
return fmt.Errorf("write personal home.json: %w", err)
|
||||
return fmt.Errorf("write personal %s: %w", posixHomeFileName, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
@@ -57,11 +57,11 @@ func (service *Service) createProjectFolderOnDisk(projectSlug, parentFolderID, n
|
||||
return "", "", fmt.Errorf("create project hierarchy folder: %w", err)
|
||||
}
|
||||
folderID := uuid.NewString()
|
||||
if err := writeJSONFile(filepath.Join(folderDir, "folder.json"), map[string]any{"id": folderID, "name": trimmedName, "slug": folderSlug, "type": "folder"}); err != nil {
|
||||
return "", "", fmt.Errorf("write project folder.json: %w", err)
|
||||
if err := writeCBORFile(filepath.Join(folderDir, posixFolderFileName), map[string]any{"id": folderID, "name": trimmedName, "slug": folderSlug, "type": "folder"}); err != nil {
|
||||
return "", "", fmt.Errorf("write project %s: %w", posixFolderFileName, err)
|
||||
}
|
||||
if err := writeJSONFile(filepath.Join(folderDir, "acl.json"), map[string]any{"version": 1, "inherits": true, "rules": []any{}}); err != nil {
|
||||
return "", "", fmt.Errorf("write project acl.json: %w", err)
|
||||
if err := writeCBORFile(filepath.Join(folderDir, posixACLFileName), map[string]any{"version": 1, "inherits": true, "rules": []any{}}); err != nil {
|
||||
return "", "", fmt.Errorf("write project %s: %w", posixACLFileName, err)
|
||||
}
|
||||
return filepath.ToSlash(filepath.Join(containerProjectionPath, folderName)), folderSlug, nil
|
||||
}
|
||||
@@ -150,13 +150,13 @@ func (service *Service) renameProjectFolderOnDisk(projectSlug, folderID, name st
|
||||
return "", "", fmt.Errorf("rename project folder: %w", err)
|
||||
}
|
||||
}
|
||||
folderPayload := readJSONFileMap(filepath.Join(destinationDir, "folder.json"))
|
||||
folderPayload := readStructuredFileMap(filepath.Join(destinationDir, posixFolderFileName))
|
||||
folderMetadataID, _ := folderPayload["id"].(string)
|
||||
if strings.TrimSpace(folderMetadataID) == "" {
|
||||
folderMetadataID = uuid.NewString()
|
||||
}
|
||||
if err := writeJSONFile(filepath.Join(destinationDir, "folder.json"), map[string]any{"id": folderMetadataID, "name": trimmedName, "slug": folderSlug, "type": "folder"}); err != nil {
|
||||
return "", "", fmt.Errorf("write renamed project folder.json: %w", err)
|
||||
if err := writeCBORFile(filepath.Join(destinationDir, posixFolderFileName), map[string]any{"id": folderMetadataID, "name": trimmedName, "slug": folderSlug, "type": "folder"}); err != nil {
|
||||
return "", "", fmt.Errorf("write renamed project %s: %w", posixFolderFileName, err)
|
||||
}
|
||||
return folderProjectionPath, renamedProjectionPath, nil
|
||||
}
|
||||
@@ -210,7 +210,7 @@ func (service *Service) moveProjectFolderOnDisk(projectSlug, folderID, parentFol
|
||||
if samePath(currentParentDir, parentDir) {
|
||||
return folderProjectionPath, folderProjectionPath, nil
|
||||
}
|
||||
folderPayload := readJSONFileMap(filepath.Join(folderDir, "folder.json"))
|
||||
folderPayload := readStructuredFileMap(filepath.Join(folderDir, posixFolderFileName))
|
||||
folderMetadataID, _ := folderPayload["id"].(string)
|
||||
if strings.TrimSpace(folderMetadataID) == "" {
|
||||
folderMetadataID = uuid.NewString()
|
||||
@@ -244,8 +244,8 @@ func (service *Service) moveProjectFolderOnDisk(projectSlug, folderID, parentFol
|
||||
folderPayload["name"] = folderName
|
||||
folderPayload["slug"] = folderSlug
|
||||
folderPayload["type"] = "folder"
|
||||
if err := writeJSONFile(filepath.Join(destinationDir, "folder.json"), folderPayload); err != nil {
|
||||
return "", "", fmt.Errorf("write moved project folder.json: %w", err)
|
||||
if err := writeCBORFile(filepath.Join(destinationDir, posixFolderFileName), folderPayload); err != nil {
|
||||
return "", "", fmt.Errorf("write moved project %s: %w", posixFolderFileName, err)
|
||||
}
|
||||
movedProjectionPath := filepath.ToSlash(filepath.Join(parentChildrenProjectionPath, folderDirName))
|
||||
return folderProjectionPath, movedProjectionPath, nil
|
||||
|
||||
@@ -9,6 +9,22 @@ import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"unicode"
|
||||
|
||||
"github.com/fxamacker/cbor/v2"
|
||||
"github.com/tailscale/hujson"
|
||||
)
|
||||
|
||||
const (
|
||||
posixSettingsFileName = "settings.cbor"
|
||||
posixLayoutFileName = "layout.cbor"
|
||||
posixHomeFileName = "home.cbor"
|
||||
posixUsersFileName = "users.cbor"
|
||||
posixACLFileName = "acl.cbor"
|
||||
posixFolderFileName = "folder.cbor"
|
||||
posixItemFileName = "item.cbor"
|
||||
posixDataFileName = "data.cbor"
|
||||
posixSchemaFileName = "schema.json"
|
||||
posixManifestFileName = "manifest.jsonc"
|
||||
)
|
||||
|
||||
func samePath(left, right string) bool {
|
||||
@@ -125,14 +141,93 @@ func writeJSONFile(path string, payload any) error {
|
||||
return os.WriteFile(path, data, 0o644)
|
||||
}
|
||||
|
||||
func readJSONFileMap(path string) map[string]any {
|
||||
func writeJSONCFile(path string, payload any) error {
|
||||
parentDir := filepath.Dir(path)
|
||||
if err := os.MkdirAll(parentDir, 0o755); err != nil {
|
||||
return err
|
||||
}
|
||||
data, err := json.MarshalIndent(payload, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
content := append([]byte("// This file is JSONC. Comments and trailing commas are allowed.\n"), data...)
|
||||
content = append(content, '\n')
|
||||
return os.WriteFile(path, content, 0o644)
|
||||
}
|
||||
|
||||
func writeCBORFile(path string, payload any) error {
|
||||
parentDir := filepath.Dir(path)
|
||||
if err := os.MkdirAll(parentDir, 0o755); err != nil {
|
||||
return err
|
||||
}
|
||||
data, err := cbor.Marshal(payload)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return os.WriteFile(path, data, 0o644)
|
||||
}
|
||||
|
||||
func readStructuredFileMap(path string) map[string]any {
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return map[string]any{}
|
||||
}
|
||||
var payload map[string]any
|
||||
if err := json.Unmarshal(data, &payload); err != nil || payload == nil {
|
||||
var payload any
|
||||
switch strings.ToLower(filepath.Ext(path)) {
|
||||
case ".cbor":
|
||||
err = cbor.Unmarshal(data, &payload)
|
||||
case ".json":
|
||||
err = json.Unmarshal(data, &payload)
|
||||
case ".jsonc":
|
||||
payload, err = decodeJSONCToAny(data)
|
||||
default:
|
||||
return map[string]any{}
|
||||
}
|
||||
return payload
|
||||
if err != nil || payload == nil {
|
||||
return map[string]any{}
|
||||
}
|
||||
normalized, ok := normalizeStructuredValue(payload).(map[string]any)
|
||||
if !ok || normalized == nil {
|
||||
return map[string]any{}
|
||||
}
|
||||
return normalized
|
||||
}
|
||||
|
||||
func decodeJSONCToAny(data []byte) (any, error) {
|
||||
ast, err := hujson.Parse(data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ast.Standardize()
|
||||
standardized := ast.Pack()
|
||||
var payload any
|
||||
if err := json.Unmarshal(standardized, &payload); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return payload, nil
|
||||
}
|
||||
|
||||
func normalizeStructuredValue(value any) any {
|
||||
switch typed := value.(type) {
|
||||
case map[string]any:
|
||||
normalized := make(map[string]any, len(typed))
|
||||
for key, child := range typed {
|
||||
normalized[key] = normalizeStructuredValue(child)
|
||||
}
|
||||
return normalized
|
||||
case map[any]any:
|
||||
normalized := make(map[string]any, len(typed))
|
||||
for key, child := range typed {
|
||||
normalized[fmt.Sprint(key)] = normalizeStructuredValue(child)
|
||||
}
|
||||
return normalized
|
||||
case []any:
|
||||
normalized := make([]any, len(typed))
|
||||
for index, child := range typed {
|
||||
normalized[index] = normalizeStructuredValue(child)
|
||||
}
|
||||
return normalized
|
||||
default:
|
||||
return value
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,14 +58,14 @@ func (service *Service) createProjectTreeItemOnDisk(projectSlug, parentFolderPat
|
||||
return "", fmt.Errorf("create project item: %w", err)
|
||||
}
|
||||
itemID := uuid.NewString()
|
||||
if err := writeJSONFile(filepath.Join(itemDir, "item.json"), map[string]any{"id": itemID, "name": trimmedName, "slug": itemSlug, "type": canonicalItemType}); err != nil {
|
||||
return "", fmt.Errorf("write project item.json: %w", err)
|
||||
if err := writeCBORFile(filepath.Join(itemDir, posixItemFileName), map[string]any{"id": itemID, "name": trimmedName, "slug": itemSlug, "type": canonicalItemType}); err != nil {
|
||||
return "", fmt.Errorf("write project %s: %w", posixItemFileName, err)
|
||||
}
|
||||
if err := writeJSONFile(filepath.Join(itemDir, "schema.json"), defaultProjectTreeItemSchema(canonicalItemType)); err != nil {
|
||||
return "", fmt.Errorf("write project schema.json: %w", err)
|
||||
if err := writeJSONFile(filepath.Join(itemDir, posixSchemaFileName), defaultProjectTreeItemSchema(canonicalItemType)); err != nil {
|
||||
return "", fmt.Errorf("write project %s: %w", posixSchemaFileName, err)
|
||||
}
|
||||
if err := writeJSONFile(filepath.Join(itemDir, "data.json"), defaultProjectTreeItemData(canonicalItemType, trimmedName)); err != nil {
|
||||
return "", fmt.Errorf("write project data.json: %w", err)
|
||||
if err := writeCBORFile(filepath.Join(itemDir, posixDataFileName), defaultProjectTreeItemData(canonicalItemType, trimmedName)); err != nil {
|
||||
return "", fmt.Errorf("write project %s: %w", posixDataFileName, err)
|
||||
}
|
||||
return filepath.ToSlash(filepath.Join(containerProjectionPath, itemDirName)), nil
|
||||
}
|
||||
@@ -140,7 +140,7 @@ func (service *Service) moveProjectTreeItemOnDisk(projectSlug, itemPath, parentF
|
||||
}
|
||||
currentParentDir := filepath.Dir(itemDir)
|
||||
currentBase := filepath.Base(itemDir)
|
||||
itemPayload := readJSONFileMap(filepath.Join(itemDir, "item.json"))
|
||||
itemPayload := readStructuredFileMap(filepath.Join(itemDir, posixItemFileName))
|
||||
itemID, _ := itemPayload["id"].(string)
|
||||
if strings.TrimSpace(itemID) == "" {
|
||||
itemID = uuid.NewString()
|
||||
@@ -182,8 +182,8 @@ func (service *Service) moveProjectTreeItemOnDisk(projectSlug, itemPath, parentF
|
||||
itemPayload["name"] = itemName
|
||||
itemPayload["slug"] = itemSlug
|
||||
itemPayload["type"] = canonicalItemType
|
||||
if err := writeJSONFile(filepath.Join(destinationDir, "item.json"), itemPayload); err != nil {
|
||||
return "", "", fmt.Errorf("write moved project item.json: %w", err)
|
||||
if err := writeCBORFile(filepath.Join(destinationDir, posixItemFileName), itemPayload); err != nil {
|
||||
return "", "", fmt.Errorf("write moved project %s: %w", posixItemFileName, err)
|
||||
}
|
||||
return itemProjectionPath, movedProjectionPath, nil
|
||||
}
|
||||
|
||||
@@ -11,18 +11,26 @@ import (
|
||||
|
||||
func (service *Service) readProjectFolderOrder(projectSlug, rootProjectionPath string) map[string][]string {
|
||||
settingsPath := service.projectSettingsPath(projectSlug)
|
||||
settingsPayload := readJSONFileMap(settingsPath)
|
||||
settingsPayload := readStructuredFileMap(settingsPath)
|
||||
folderOrderPayload, _ := settingsPayload["folderOrder"].(map[string]any)
|
||||
if folderOrderPayload == nil { return map[string][]string{} }
|
||||
if folderOrderPayload == nil {
|
||||
return map[string][]string{}
|
||||
}
|
||||
scopePayload, _ := folderOrderPayload[projectFolderOrderScope(rootProjectionPath)].(map[string]any)
|
||||
if scopePayload == nil { return map[string][]string{} }
|
||||
if scopePayload == nil {
|
||||
return map[string][]string{}
|
||||
}
|
||||
byParentPayload, _ := scopePayload["byParent"].(map[string]any)
|
||||
if byParentPayload == nil { return map[string][]string{} }
|
||||
if byParentPayload == nil {
|
||||
return map[string][]string{}
|
||||
}
|
||||
order := make(map[string][]string, len(byParentPayload))
|
||||
for key, raw := range byParentPayload {
|
||||
for _, id := range stringSliceValue(raw) {
|
||||
trimmedID := strings.TrimSpace(id)
|
||||
if trimmedID == "" || slicesContains(order[key], trimmedID) { continue }
|
||||
if trimmedID == "" || slicesContains(order[key], trimmedID) {
|
||||
continue
|
||||
}
|
||||
order[key] = append(order[key], trimmedID)
|
||||
}
|
||||
}
|
||||
@@ -31,37 +39,53 @@ func (service *Service) readProjectFolderOrder(projectSlug, rootProjectionPath s
|
||||
|
||||
func (service *Service) writeProjectFolderOrder(projectSlug, rootProjectionPath string, folderOrder map[string][]string) error {
|
||||
settingsPath := service.projectSettingsPath(projectSlug)
|
||||
settingsPayload := readJSONFileMap(settingsPath)
|
||||
if settingsPayload == nil { settingsPayload = map[string]any{} }
|
||||
settingsPayload := readStructuredFileMap(settingsPath)
|
||||
if settingsPayload == nil {
|
||||
settingsPayload = map[string]any{}
|
||||
}
|
||||
folderOrderPayload, _ := settingsPayload["folderOrder"].(map[string]any)
|
||||
if folderOrderPayload == nil { folderOrderPayload = map[string]any{} }
|
||||
if folderOrderPayload == nil {
|
||||
folderOrderPayload = map[string]any{}
|
||||
}
|
||||
scopeKey := projectFolderOrderScope(rootProjectionPath)
|
||||
scopePayload, _ := folderOrderPayload[scopeKey].(map[string]any)
|
||||
if scopePayload == nil { scopePayload = map[string]any{} }
|
||||
if scopePayload == nil {
|
||||
scopePayload = map[string]any{}
|
||||
}
|
||||
byParentPayload := map[string]any{}
|
||||
for key, ids := range folderOrder {
|
||||
if len(ids) == 0 { continue }
|
||||
if len(ids) == 0 {
|
||||
continue
|
||||
}
|
||||
copied := make([]string, 0, len(ids))
|
||||
for _, id := range ids {
|
||||
trimmedID := strings.TrimSpace(id)
|
||||
if trimmedID == "" || slicesContains(copied, trimmedID) { continue }
|
||||
if trimmedID == "" || slicesContains(copied, trimmedID) {
|
||||
continue
|
||||
}
|
||||
copied = append(copied, trimmedID)
|
||||
}
|
||||
if len(copied) > 0 { byParentPayload[key] = copied }
|
||||
if len(copied) > 0 {
|
||||
byParentPayload[key] = copied
|
||||
}
|
||||
}
|
||||
scopePayload["byParent"] = byParentPayload
|
||||
folderOrderPayload[scopeKey] = scopePayload
|
||||
settingsPayload["folderOrder"] = folderOrderPayload
|
||||
if err := writeJSONFile(settingsPath, settingsPayload); err != nil { return fmt.Errorf("write project settings.json: %w", err) }
|
||||
if err := writeCBORFile(settingsPath, settingsPayload); err != nil {
|
||||
return fmt.Errorf("write project %s: %w", posixSettingsFileName, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (service *Service) projectSettingsPath(projectSlug string) string {
|
||||
return filepath.Join(strings.TrimSpace(service.posixRoot), "projects", slugDir("project", projectSlug), "settings.json")
|
||||
return filepath.Join(strings.TrimSpace(service.posixRoot), "projects", slugDir("project", projectSlug), posixSettingsFileName)
|
||||
}
|
||||
|
||||
func projectFolderOrderScope(rootProjectionPath string) string {
|
||||
if strings.HasSuffix(rootProjectionPath, "/tree") { return projectFolderOrderTree }
|
||||
if strings.HasSuffix(rootProjectionPath, "/tree") {
|
||||
return projectFolderOrderTree
|
||||
}
|
||||
return projectFolderOrderHierarchy
|
||||
}
|
||||
|
||||
@@ -70,19 +94,33 @@ func applyProjectHierarchyFolderOrdering(folders []ProjectHierarchyFolderRecord,
|
||||
}
|
||||
|
||||
func applyProjectHierarchyFolderOrderingForParent(folders []ProjectHierarchyFolderRecord, parentID string, folderOrder map[string][]string) []ProjectHierarchyFolderRecord {
|
||||
if len(folders) == 0 { return folders }
|
||||
if len(folders) == 0 {
|
||||
return folders
|
||||
}
|
||||
nextFolders := make([]ProjectHierarchyFolderRecord, len(folders))
|
||||
copy(nextFolders, folders)
|
||||
for index := range nextFolders { nextFolders[index].Children = applyProjectHierarchyFolderOrderingForParent(nextFolders[index].Children, nextFolders[index].ID, folderOrder) }
|
||||
for index := range nextFolders {
|
||||
nextFolders[index].Children = applyProjectHierarchyFolderOrderingForParent(nextFolders[index].Children, nextFolders[index].ID, folderOrder)
|
||||
}
|
||||
orderIDs := folderOrder[projectFolderOrderParentKey(parentID)]
|
||||
if len(orderIDs) == 0 { return nextFolders }
|
||||
if len(orderIDs) == 0 {
|
||||
return nextFolders
|
||||
}
|
||||
rankByID := make(map[string]int, len(orderIDs))
|
||||
for index, id := range orderIDs { if _, exists := rankByID[id]; !exists { rankByID[id] = index } }
|
||||
for index, id := range orderIDs {
|
||||
if _, exists := rankByID[id]; !exists {
|
||||
rankByID[id] = index
|
||||
}
|
||||
}
|
||||
sort.SliceStable(nextFolders, func(left, right int) bool {
|
||||
leftRank, leftOrdered := rankByID[nextFolders[left].ID]
|
||||
rightRank, rightOrdered := rankByID[nextFolders[right].ID]
|
||||
if leftOrdered && rightOrdered { return leftRank < rightRank }
|
||||
if leftOrdered != rightOrdered { return leftOrdered }
|
||||
if leftOrdered && rightOrdered {
|
||||
return leftRank < rightRank
|
||||
}
|
||||
if leftOrdered != rightOrdered {
|
||||
return leftOrdered
|
||||
}
|
||||
return false
|
||||
})
|
||||
return nextFolders
|
||||
@@ -91,32 +129,47 @@ func applyProjectHierarchyFolderOrderingForParent(folders []ProjectHierarchyFold
|
||||
func removeFolderOrder(folderOrder map[string][]string, folderID string) {
|
||||
removeFolderOrderReference(folderOrder, folderID)
|
||||
trimmedFolderID := strings.TrimSpace(folderID)
|
||||
if trimmedFolderID == "" { return }
|
||||
if trimmedFolderID == "" {
|
||||
return
|
||||
}
|
||||
delete(folderOrder, projectFolderOrderParentKey(trimmedFolderID))
|
||||
}
|
||||
|
||||
func removeFolderOrderReference(folderOrder map[string][]string, folderID string) {
|
||||
trimmedFolderID := strings.TrimSpace(folderID)
|
||||
if trimmedFolderID == "" { return }
|
||||
if trimmedFolderID == "" {
|
||||
return
|
||||
}
|
||||
for key, ids := range folderOrder {
|
||||
nextIDs := ids[:0]
|
||||
for _, id := range ids {
|
||||
if strings.TrimSpace(id) == trimmedFolderID { continue }
|
||||
if strings.TrimSpace(id) == trimmedFolderID {
|
||||
continue
|
||||
}
|
||||
nextIDs = append(nextIDs, id)
|
||||
}
|
||||
if len(nextIDs) == 0 { delete(folderOrder, key); continue }
|
||||
if len(nextIDs) == 0 {
|
||||
delete(folderOrder, key)
|
||||
continue
|
||||
}
|
||||
folderOrder[key] = append([]string(nil), nextIDs...)
|
||||
}
|
||||
}
|
||||
|
||||
func insertFolderOrder(folderOrder map[string][]string, parentID, folderID string, index int) {
|
||||
trimmedFolderID := strings.TrimSpace(folderID)
|
||||
if trimmedFolderID == "" { return }
|
||||
if trimmedFolderID == "" {
|
||||
return
|
||||
}
|
||||
removeFolderOrderReference(folderOrder, trimmedFolderID)
|
||||
parentKey := projectFolderOrderParentKey(parentID)
|
||||
children := append([]string(nil), folderOrder[parentKey]...)
|
||||
if index < 0 { index = 0 }
|
||||
if index > len(children) { index = len(children) }
|
||||
if index < 0 {
|
||||
index = 0
|
||||
}
|
||||
if index > len(children) {
|
||||
index = len(children)
|
||||
}
|
||||
children = slicesInsert(children, index, trimmedFolderID)
|
||||
folderOrder[parentKey] = children
|
||||
}
|
||||
@@ -126,17 +179,24 @@ func seedFolderOrderParent(folderOrder map[string][]string, folders []ProjectHie
|
||||
trimmedParentID := strings.TrimSpace(parentID)
|
||||
if trimmedParentID != "" {
|
||||
parent, found := findProjectHierarchyFolder(folders, trimmedParentID)
|
||||
if !found { return }
|
||||
if !found {
|
||||
return
|
||||
}
|
||||
children = parent.Children
|
||||
}
|
||||
parentKey := projectFolderOrderParentKey(trimmedParentID)
|
||||
seeded := make([]string, 0, len(children))
|
||||
for _, child := range children {
|
||||
childID := strings.TrimSpace(child.ID)
|
||||
if childID == "" || slicesContains(seeded, childID) { continue }
|
||||
if childID == "" || slicesContains(seeded, childID) {
|
||||
continue
|
||||
}
|
||||
seeded = append(seeded, childID)
|
||||
}
|
||||
if len(seeded) == 0 { delete(folderOrder, parentKey); return }
|
||||
if len(seeded) == 0 {
|
||||
delete(folderOrder, parentKey)
|
||||
return
|
||||
}
|
||||
folderOrder[parentKey] = seeded
|
||||
}
|
||||
|
||||
@@ -146,23 +206,35 @@ func folderOrderChildren(folderOrder map[string][]string, parentID string) []str
|
||||
|
||||
func projectFolderOrderParentKey(parentID string) string {
|
||||
trimmedParentID := strings.TrimSpace(parentID)
|
||||
if trimmedParentID == "" { return projectFolderOrderRootKey }
|
||||
if trimmedParentID == "" {
|
||||
return projectFolderOrderRootKey
|
||||
}
|
||||
return trimmedParentID
|
||||
}
|
||||
|
||||
func stringSliceValue(value any) []string {
|
||||
items, ok := value.([]any)
|
||||
if !ok {
|
||||
if typed, ok := value.([]string); ok { return typed }
|
||||
if typed, ok := value.([]string); ok {
|
||||
return typed
|
||||
}
|
||||
return nil
|
||||
}
|
||||
result := make([]string, 0, len(items))
|
||||
for _, item := range items { if text, ok := item.(string); ok { result = append(result, text) } }
|
||||
for _, item := range items {
|
||||
if text, ok := item.(string); ok {
|
||||
result = append(result, text)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func slicesContains(values []string, value string) bool {
|
||||
for _, existing := range values { if existing == value { return true } }
|
||||
for _, existing := range values {
|
||||
if existing == value {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -174,25 +246,35 @@ func slicesInsert(values []string, index int, value string) []string {
|
||||
}
|
||||
|
||||
func buildProjectHierarchyFolderTree(rows []projectHierarchyFolderRow, rootParentPath string) []ProjectHierarchyFolderRecord {
|
||||
if len(rows) == 0 { return nil }
|
||||
if len(rows) == 0 {
|
||||
return nil
|
||||
}
|
||||
nodesByPath := make(map[string]*ProjectHierarchyFolderRecord, len(rows))
|
||||
childrenByParent := make(map[string][]string)
|
||||
for _, row := range rows {
|
||||
folderID := strings.TrimSpace(row.ID)
|
||||
if folderID == "" { folderID = row.Path }
|
||||
if folderID == "" {
|
||||
folderID = row.Path
|
||||
}
|
||||
label := strings.TrimSpace(row.Label)
|
||||
if label == "" { label = fallbackFolderLabel(row.Path) }
|
||||
if label == "" {
|
||||
label = fallbackFolderLabel(row.Path)
|
||||
}
|
||||
nodesByPath[row.Path] = &ProjectHierarchyFolderRecord{ID: folderID, Path: row.Path, Label: label, Children: []ProjectHierarchyFolderRecord{}}
|
||||
childrenByParent[row.ParentPath] = append(childrenByParent[row.ParentPath], row.Path)
|
||||
}
|
||||
var build func(parentPath string) []ProjectHierarchyFolderRecord
|
||||
build = func(parentPath string) []ProjectHierarchyFolderRecord {
|
||||
childPaths := childrenByParent[parentPath]
|
||||
if len(childPaths) == 0 { return nil }
|
||||
if len(childPaths) == 0 {
|
||||
return nil
|
||||
}
|
||||
folders := make([]ProjectHierarchyFolderRecord, 0, len(childPaths))
|
||||
for _, childPath := range childPaths {
|
||||
node := nodesByPath[childPath]
|
||||
if node == nil { continue }
|
||||
if node == nil {
|
||||
continue
|
||||
}
|
||||
folder := ProjectHierarchyFolderRecord{ID: node.ID, Path: node.Path, Label: node.Label, Children: build(filepath.ToSlash(filepath.Join(childPath, "children")))}
|
||||
folders = append(folders, folder)
|
||||
}
|
||||
@@ -202,15 +284,25 @@ func buildProjectHierarchyFolderTree(rows []projectHierarchyFolderRow, rootParen
|
||||
}
|
||||
|
||||
func buildProjectTreeNodeTree(rows []projectTreeNodeRow, rootParentPath string) []ProjectTreeNodeRecord {
|
||||
if len(rows) == 0 { return nil }
|
||||
if len(rows) == 0 {
|
||||
return nil
|
||||
}
|
||||
nodesByPath := make(map[string]*ProjectTreeNodeRecord, len(rows))
|
||||
childrenByParent := make(map[string][]string)
|
||||
for _, row := range rows {
|
||||
nodeKind := normalizeProjectTreeNodeKind(row.Kind)
|
||||
nodeID := strings.TrimSpace(row.ID)
|
||||
if nodeID == "" { nodeID = row.Path }
|
||||
if nodeID == "" {
|
||||
nodeID = row.Path
|
||||
}
|
||||
label := strings.TrimSpace(row.Label)
|
||||
if label == "" { if nodeKind == "item" { label = fallbackItemLabel(row.Path) } else { label = fallbackFolderLabel(row.Path) } }
|
||||
if label == "" {
|
||||
if nodeKind == "item" {
|
||||
label = fallbackItemLabel(row.Path)
|
||||
} else {
|
||||
label = fallbackFolderLabel(row.Path)
|
||||
}
|
||||
}
|
||||
nodesByPath[row.Path] = &ProjectTreeNodeRecord{ID: nodeID, Path: row.Path, Label: label, Kind: nodeKind, ItemType: normalizeProjectTreeItemType(row.ItemType), Children: []ProjectTreeNodeRecord{}}
|
||||
parentKey := normalizeProjectTreeParentPath(nodeKind, row.ParentPath)
|
||||
childrenByParent[parentKey] = append(childrenByParent[parentKey], row.Path)
|
||||
@@ -218,13 +310,19 @@ func buildProjectTreeNodeTree(rows []projectTreeNodeRow, rootParentPath string)
|
||||
var build func(parentPath string) []ProjectTreeNodeRecord
|
||||
build = func(parentPath string) []ProjectTreeNodeRecord {
|
||||
childPaths := childrenByParent[parentPath]
|
||||
if len(childPaths) == 0 { return nil }
|
||||
if len(childPaths) == 0 {
|
||||
return nil
|
||||
}
|
||||
nodes := make([]ProjectTreeNodeRecord, 0, len(childPaths))
|
||||
for _, childPath := range childPaths {
|
||||
node := nodesByPath[childPath]
|
||||
if node == nil { continue }
|
||||
if node == nil {
|
||||
continue
|
||||
}
|
||||
nextNode := ProjectTreeNodeRecord{ID: node.ID, Path: node.Path, Label: node.Label, Kind: node.Kind, ItemType: node.ItemType}
|
||||
if node.Kind == "folder" { nextNode.Children = build(node.Path) }
|
||||
if node.Kind == "folder" {
|
||||
nextNode.Children = build(node.Path)
|
||||
}
|
||||
nodes = append(nodes, nextNode)
|
||||
}
|
||||
return nodes
|
||||
@@ -233,7 +331,9 @@ func buildProjectTreeNodeTree(rows []projectTreeNodeRow, rootParentPath string)
|
||||
}
|
||||
|
||||
func normalizeProjectTreeParentPath(kind, parentPath string) string {
|
||||
if kind == "folder" && strings.HasSuffix(parentPath, "/children") { return filepath.ToSlash(filepath.Dir(parentPath)) }
|
||||
if kind == "folder" && strings.HasSuffix(parentPath, "/children") {
|
||||
return filepath.ToSlash(filepath.Dir(parentPath))
|
||||
}
|
||||
return parentPath
|
||||
}
|
||||
|
||||
@@ -253,19 +353,35 @@ func applyProjectTreeNodeOrdering(nodes []ProjectTreeNodeRecord, folderOrder map
|
||||
}
|
||||
|
||||
func applyProjectTreeNodeOrderingForParent(nodes []ProjectTreeNodeRecord, parentID string, folderOrder map[string][]string) []ProjectTreeNodeRecord {
|
||||
if len(nodes) == 0 { return nodes }
|
||||
if len(nodes) == 0 {
|
||||
return nodes
|
||||
}
|
||||
nextNodes := make([]ProjectTreeNodeRecord, len(nodes))
|
||||
copy(nextNodes, nodes)
|
||||
for index := range nextNodes { if nextNodes[index].Kind == "folder" { nextNodes[index].Children = applyProjectTreeNodeOrderingForParent(nextNodes[index].Children, nextNodes[index].ID, folderOrder) } }
|
||||
for index := range nextNodes {
|
||||
if nextNodes[index].Kind == "folder" {
|
||||
nextNodes[index].Children = applyProjectTreeNodeOrderingForParent(nextNodes[index].Children, nextNodes[index].ID, folderOrder)
|
||||
}
|
||||
}
|
||||
orderIDs := folderOrder[projectFolderOrderParentKey(parentID)]
|
||||
if len(orderIDs) == 0 { return nextNodes }
|
||||
if len(orderIDs) == 0 {
|
||||
return nextNodes
|
||||
}
|
||||
rankByID := make(map[string]int, len(orderIDs))
|
||||
for index, id := range orderIDs { if _, exists := rankByID[id]; !exists { rankByID[id] = index } }
|
||||
for index, id := range orderIDs {
|
||||
if _, exists := rankByID[id]; !exists {
|
||||
rankByID[id] = index
|
||||
}
|
||||
}
|
||||
sort.SliceStable(nextNodes, func(left, right int) bool {
|
||||
leftRank, leftOrdered := rankByID[nextNodes[left].ID]
|
||||
rightRank, rightOrdered := rankByID[nextNodes[right].ID]
|
||||
if leftOrdered && rightOrdered { return leftRank < rightRank }
|
||||
if leftOrdered != rightOrdered { return leftOrdered }
|
||||
if leftOrdered && rightOrdered {
|
||||
return leftRank < rightRank
|
||||
}
|
||||
if leftOrdered != rightOrdered {
|
||||
return leftOrdered
|
||||
}
|
||||
return false
|
||||
})
|
||||
return nextNodes
|
||||
@@ -273,39 +389,57 @@ func applyProjectTreeNodeOrderingForParent(nodes []ProjectTreeNodeRecord, parent
|
||||
|
||||
func findProjectHierarchyFolder(folders []ProjectHierarchyFolderRecord, folderID string) (ProjectHierarchyFolderRecord, bool) {
|
||||
for _, folder := range folders {
|
||||
if folder.ID == folderID { return folder, true }
|
||||
if child, ok := findProjectHierarchyFolder(folder.Children, folderID); ok { return child, true }
|
||||
if folder.ID == folderID {
|
||||
return folder, true
|
||||
}
|
||||
if child, ok := findProjectHierarchyFolder(folder.Children, folderID); ok {
|
||||
return child, true
|
||||
}
|
||||
}
|
||||
return ProjectHierarchyFolderRecord{}, false
|
||||
}
|
||||
|
||||
func findProjectHierarchyFolderByPath(folders []ProjectHierarchyFolderRecord, folderPath string) (ProjectHierarchyFolderRecord, bool) {
|
||||
for _, folder := range folders {
|
||||
if folder.Path == folderPath { return folder, true }
|
||||
if child, ok := findProjectHierarchyFolderByPath(folder.Children, folderPath); ok { return child, true }
|
||||
if folder.Path == folderPath {
|
||||
return folder, true
|
||||
}
|
||||
if child, ok := findProjectHierarchyFolderByPath(folder.Children, folderPath); ok {
|
||||
return child, true
|
||||
}
|
||||
}
|
||||
return ProjectHierarchyFolderRecord{}, false
|
||||
}
|
||||
|
||||
func findProjectTreeNode(nodes []ProjectTreeNodeRecord, nodeID string) (ProjectTreeNodeRecord, bool) {
|
||||
for _, node := range nodes {
|
||||
if node.ID == nodeID { return node, true }
|
||||
if child, ok := findProjectTreeNode(node.Children, nodeID); ok { return child, true }
|
||||
if node.ID == nodeID {
|
||||
return node, true
|
||||
}
|
||||
if child, ok := findProjectTreeNode(node.Children, nodeID); ok {
|
||||
return child, true
|
||||
}
|
||||
}
|
||||
return ProjectTreeNodeRecord{}, false
|
||||
}
|
||||
|
||||
func findProjectTreeNodeByPath(nodes []ProjectTreeNodeRecord, nodePath string) (ProjectTreeNodeRecord, bool) {
|
||||
for _, node := range nodes {
|
||||
if node.Path == nodePath { return node, true }
|
||||
if child, ok := findProjectTreeNodeByPath(node.Children, nodePath); ok { return child, true }
|
||||
if node.Path == nodePath {
|
||||
return node, true
|
||||
}
|
||||
if child, ok := findProjectTreeNodeByPath(node.Children, nodePath); ok {
|
||||
return child, true
|
||||
}
|
||||
}
|
||||
return ProjectTreeNodeRecord{}, false
|
||||
}
|
||||
|
||||
func findProjectTreeFolderByPath(nodes []ProjectTreeNodeRecord, folderPath string) (ProjectTreeNodeRecord, bool) {
|
||||
node, ok := findProjectTreeNodeByPath(nodes, folderPath)
|
||||
if !ok || node.Kind != "folder" { return ProjectTreeNodeRecord{}, false }
|
||||
if !ok || node.Kind != "folder" {
|
||||
return ProjectTreeNodeRecord{}, false
|
||||
}
|
||||
return node, true
|
||||
}
|
||||
|
||||
@@ -314,16 +448,24 @@ func seedProjectTreeOrderParent(folderOrder map[string][]string, nodes []Project
|
||||
trimmedParentID := strings.TrimSpace(parentID)
|
||||
if trimmedParentID != "" {
|
||||
parent, found := findProjectTreeNode(nodes, trimmedParentID)
|
||||
if !found || parent.Kind != "folder" { return }
|
||||
if !found || parent.Kind != "folder" {
|
||||
return
|
||||
}
|
||||
children = parent.Children
|
||||
}
|
||||
parentKey := projectFolderOrderParentKey(parentID)
|
||||
if len(folderOrder[parentKey]) > 0 { return }
|
||||
if len(folderOrder[parentKey]) > 0 {
|
||||
return
|
||||
}
|
||||
orderedIDs := make([]string, 0, len(children))
|
||||
for _, child := range children {
|
||||
trimmedChildID := strings.TrimSpace(child.ID)
|
||||
if trimmedChildID == "" || slicesContains(orderedIDs, trimmedChildID) { continue }
|
||||
if trimmedChildID == "" || slicesContains(orderedIDs, trimmedChildID) {
|
||||
continue
|
||||
}
|
||||
orderedIDs = append(orderedIDs, trimmedChildID)
|
||||
}
|
||||
if len(orderedIDs) > 0 { folderOrder[parentKey] = orderedIDs }
|
||||
if len(orderedIDs) > 0 {
|
||||
folderOrder[parentKey] = orderedIDs
|
||||
}
|
||||
}
|
||||
|
||||
@@ -232,7 +232,7 @@ func (service *Service) getProjectHierarchyFoldersByRootPath(
|
||||
COALESCE(folder_meta.resource_name, directories.resource_name, '')
|
||||
FROM posix_nodes AS directories
|
||||
LEFT JOIN posix_nodes AS folder_meta
|
||||
ON folder_meta.path = directories.path || '/folder.json'
|
||||
ON folder_meta.path = directories.path || '/folder.cbor'
|
||||
AND folder_meta.node_kind = 'file'::posix_node_kind
|
||||
WHERE directories.node_kind = 'directory'::posix_node_kind
|
||||
AND directories.logical_type = 'hierarchy_folder'
|
||||
@@ -286,12 +286,11 @@ func (service *Service) getProjectTreeNodesByRootPath(
|
||||
COALESCE(node_meta.content_json->>'type', directories.content_json->>'type', '')
|
||||
FROM posix_nodes AS directories
|
||||
LEFT JOIN posix_nodes AS node_meta
|
||||
ON node_meta.path = directories.path || CASE
|
||||
WHEN directories.logical_type = 'hierarchy_folder' THEN '/folder.json'
|
||||
WHEN directories.logical_type = 'item' THEN '/item.json'
|
||||
ELSE ''
|
||||
END
|
||||
AND node_meta.node_kind = 'file'::posix_node_kind
|
||||
ON node_meta.node_kind = 'file'::posix_node_kind
|
||||
AND (
|
||||
(directories.logical_type = 'hierarchy_folder' AND node_meta.path = directories.path || '/folder.cbor')
|
||||
OR (directories.logical_type = 'item' AND node_meta.path = directories.path || '/item.cbor')
|
||||
)
|
||||
WHERE directories.node_kind = 'directory'::posix_node_kind
|
||||
AND directories.logical_type IN ('hierarchy_folder', 'item')
|
||||
AND directories.project_slug = $1
|
||||
|
||||
@@ -7,6 +7,9 @@ import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/fxamacker/cbor/v2"
|
||||
"github.com/tailscale/hujson"
|
||||
)
|
||||
|
||||
type fakeRow struct {
|
||||
@@ -104,25 +107,28 @@ func TestEnsureBootstrapPOSIXSkeletonInitializesEmptyRoot(t *testing.T) {
|
||||
}
|
||||
|
||||
requiredPaths := []string{
|
||||
filepath.Join(rootPath, "settings.json"),
|
||||
filepath.Join(rootPath, "layout.json"),
|
||||
filepath.Join(rootPath, posixSettingsFileName),
|
||||
filepath.Join(rootPath, posixLayoutFileName),
|
||||
filepath.Join(rootPath, "catalog", "packs"),
|
||||
filepath.Join(rootPath, "catalog", "standalone"),
|
||||
filepath.Join(rootPath, "departments", "department-primary-department", "settings.json"),
|
||||
filepath.Join(rootPath, "departments", "department-primary-department", "users.json"),
|
||||
filepath.Join(rootPath, "departments", "department-primary-department", "teams", "team-primary-team", "settings.json"),
|
||||
filepath.Join(rootPath, "departments", "department-primary-department", "teams", "team-primary-team", "users.json"),
|
||||
filepath.Join(rootPath, "projects", "project-primary-project", "settings.json"),
|
||||
filepath.Join(rootPath, "projects", "project-primary-project", "home.json"),
|
||||
filepath.Join(rootPath, "projects", "project-primary-project", "acl.json"),
|
||||
filepath.Join(rootPath, "catalog", "packs", "pack-core", posixManifestFileName),
|
||||
filepath.Join(rootPath, "catalog", "packs", "pack-core", "entries", "app-shell", posixManifestFileName),
|
||||
filepath.Join(rootPath, "catalog", "standalone", "app-shell", posixManifestFileName),
|
||||
filepath.Join(rootPath, "departments", "department-primary-department", posixSettingsFileName),
|
||||
filepath.Join(rootPath, "departments", "department-primary-department", posixUsersFileName),
|
||||
filepath.Join(rootPath, "departments", "department-primary-department", "teams", "team-primary-team", posixSettingsFileName),
|
||||
filepath.Join(rootPath, "departments", "department-primary-department", "teams", "team-primary-team", posixUsersFileName),
|
||||
filepath.Join(rootPath, "projects", "project-primary-project", posixSettingsFileName),
|
||||
filepath.Join(rootPath, "projects", "project-primary-project", posixHomeFileName),
|
||||
filepath.Join(rootPath, "projects", "project-primary-project", posixACLFileName),
|
||||
filepath.Join(rootPath, "projects", "project-primary-project", "children"),
|
||||
filepath.Join(rootPath, "projects", "project-primary-project", "tree"),
|
||||
filepath.Join(rootPath, "users", "settings.json"),
|
||||
filepath.Join(rootPath, "users", "data.json"),
|
||||
filepath.Join(rootPath, "users", posixSettingsFileName),
|
||||
filepath.Join(rootPath, "users", posixDataFileName),
|
||||
filepath.Join(rootPath, "users", "personals"),
|
||||
filepath.Join(rootPath, "users", "personals", "personal-ronald", "settings.json"),
|
||||
filepath.Join(rootPath, "users", "personals", "personal-ronald", "layout.json"),
|
||||
filepath.Join(rootPath, "users", "personals", "personal-ronald", "home.json"),
|
||||
filepath.Join(rootPath, "users", "personals", "personal-ronald", posixSettingsFileName),
|
||||
filepath.Join(rootPath, "users", "personals", "personal-ronald", posixLayoutFileName),
|
||||
filepath.Join(rootPath, "users", "personals", "personal-ronald", posixHomeFileName),
|
||||
filepath.Join(rootPath, "users", "personals", "personal-ronald", "tree"),
|
||||
}
|
||||
|
||||
@@ -132,10 +138,10 @@ func TestEnsureBootstrapPOSIXSkeletonInitializesEmptyRoot(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
settingsPayload := readJSONFileForTest[map[string]any](t, filepath.Join(rootPath, "settings.json"))
|
||||
settingsPayload := readStructuredFileForTest[map[string]any](t, filepath.Join(rootPath, posixSettingsFileName))
|
||||
installationPayload, ok := settingsPayload["installation"].(map[string]any)
|
||||
if !ok {
|
||||
t.Fatalf("settings.json missing installation object: %#v", settingsPayload)
|
||||
t.Fatalf("%s missing installation object: %#v", posixSettingsFileName, settingsPayload)
|
||||
}
|
||||
if installationPayload["name"] != "MangoPig" {
|
||||
t.Fatalf("expected installation name MangoPig, got %#v", installationPayload["name"])
|
||||
@@ -144,31 +150,53 @@ func TestEnsureBootstrapPOSIXSkeletonInitializesEmptyRoot(t *testing.T) {
|
||||
t.Fatalf("expected installation to be bootstrapped, got %#v", installationPayload["isBootstrapped"])
|
||||
}
|
||||
|
||||
layoutPayload := readJSONFileForTest[map[string]any](t, filepath.Join(rootPath, "layout.json"))
|
||||
layoutPayload := readStructuredFileForTest[map[string]any](t, filepath.Join(rootPath, posixLayoutFileName))
|
||||
homePayload, ok := layoutPayload["home"].(map[string]any)
|
||||
if !ok {
|
||||
t.Fatalf("layout.json missing home object: %#v", layoutPayload)
|
||||
t.Fatalf("%s missing home object: %#v", posixLayoutFileName, layoutPayload)
|
||||
}
|
||||
if homePayload["defaultProjectSlug"] != "primary-project" {
|
||||
t.Fatalf("expected default project slug primary-project, got %#v", homePayload["defaultProjectSlug"])
|
||||
}
|
||||
|
||||
projectSettings := readJSONFileForTest[map[string]any](t, filepath.Join(rootPath, "projects", "project-primary-project", "settings.json"))
|
||||
packManifest := readStructuredFileForTest[map[string]any](t, filepath.Join(rootPath, "catalog", "packs", "pack-core", posixManifestFileName))
|
||||
if packManifest["type"] != "pack" {
|
||||
t.Fatalf("expected pack manifest type pack, got %#v", packManifest["type"])
|
||||
}
|
||||
if packManifest["slug"] != "core" {
|
||||
t.Fatalf("expected pack manifest slug core, got %#v", packManifest["slug"])
|
||||
}
|
||||
|
||||
entryManifest := readStructuredFileForTest[map[string]any](t, filepath.Join(rootPath, "catalog", "packs", "pack-core", "entries", "app-shell", posixManifestFileName))
|
||||
runtimePayload, ok := entryManifest["runtime"].(map[string]any)
|
||||
if !ok {
|
||||
t.Fatalf("%s missing runtime object: %#v", posixManifestFileName, entryManifest)
|
||||
}
|
||||
if runtimePayload["path"] != "/v1/app-shell" {
|
||||
t.Fatalf("expected app-shell runtime path /v1/app-shell, got %#v", runtimePayload["path"])
|
||||
}
|
||||
|
||||
standaloneManifest := readStructuredFileForTest[map[string]any](t, filepath.Join(rootPath, "catalog", "standalone", "app-shell", posixManifestFileName))
|
||||
if standaloneManifest["source"] != "standalone" {
|
||||
t.Fatalf("expected standalone manifest source standalone, got %#v", standaloneManifest["source"])
|
||||
}
|
||||
|
||||
projectSettings := readStructuredFileForTest[map[string]any](t, filepath.Join(rootPath, "projects", "project-primary-project", posixSettingsFileName))
|
||||
if projectSettings["type"] != "project" {
|
||||
t.Fatalf("expected project settings type project, got %#v", projectSettings["type"])
|
||||
}
|
||||
|
||||
projectACL := readJSONFileForTest[map[string]any](t, filepath.Join(rootPath, "projects", "project-primary-project", "acl.json"))
|
||||
projectACL := readStructuredFileForTest[map[string]any](t, filepath.Join(rootPath, "projects", "project-primary-project", posixACLFileName))
|
||||
if projectACL["inherits"] != true {
|
||||
t.Fatalf("expected project acl to inherit by default, got %#v", projectACL["inherits"])
|
||||
}
|
||||
|
||||
usersSettings := readJSONFileForTest[map[string]any](t, filepath.Join(rootPath, "users", "settings.json"))
|
||||
usersSettings := readStructuredFileForTest[map[string]any](t, filepath.Join(rootPath, "users", posixSettingsFileName))
|
||||
if usersSettings["primaryAdminId"] != "admin-1" {
|
||||
t.Fatalf("expected primary admin id admin-1, got %#v", usersSettings["primaryAdminId"])
|
||||
}
|
||||
|
||||
personalSettings := readJSONFileForTest[map[string]any](t, filepath.Join(rootPath, "users", "personals", "personal-ronald", "settings.json"))
|
||||
personalSettings := readStructuredFileForTest[map[string]any](t, filepath.Join(rootPath, "users", "personals", "personal-ronald", posixSettingsFileName))
|
||||
if personalSettings["type"] != "personal" {
|
||||
t.Fatalf("expected personal settings type personal, got %#v", personalSettings["type"])
|
||||
}
|
||||
@@ -179,7 +207,7 @@ func TestEnsureBootstrapPOSIXSkeletonInitializesEmptyRoot(t *testing.T) {
|
||||
t.Fatalf("expected personal slug ronald, got %#v", personalSettings["slug"])
|
||||
}
|
||||
|
||||
personalHome := readJSONFileForTest[map[string]any](t, filepath.Join(rootPath, "users", "personals", "personal-ronald", "home.json"))
|
||||
personalHome := readStructuredFileForTest[map[string]any](t, filepath.Join(rootPath, "users", "personals", "personal-ronald", posixHomeFileName))
|
||||
if personalHome["type"] != "personal-home" {
|
||||
t.Fatalf("expected personal home type personal-home, got %#v", personalHome["type"])
|
||||
}
|
||||
@@ -217,8 +245,8 @@ func TestCreateProjectHierarchyFolderOnDiskCreatesExpectedFolderShape(t *testing
|
||||
|
||||
createdFolderPath := filepath.Join(rootPath, "projects", "project-primary-project", "children", "folder-design-system")
|
||||
for _, path := range []string{
|
||||
filepath.Join(createdFolderPath, "folder.json"),
|
||||
filepath.Join(createdFolderPath, "acl.json"),
|
||||
filepath.Join(createdFolderPath, posixFolderFileName),
|
||||
filepath.Join(createdFolderPath, posixACLFileName),
|
||||
filepath.Join(createdFolderPath, "children"),
|
||||
} {
|
||||
if _, err := os.Stat(path); err != nil {
|
||||
@@ -226,7 +254,7 @@ func TestCreateProjectHierarchyFolderOnDiskCreatesExpectedFolderShape(t *testing
|
||||
}
|
||||
}
|
||||
|
||||
folderPayload := readJSONFileForTest[map[string]any](t, filepath.Join(createdFolderPath, "folder.json"))
|
||||
folderPayload := readStructuredFileForTest[map[string]any](t, filepath.Join(createdFolderPath, posixFolderFileName))
|
||||
if strings.TrimSpace(asStringForTest(folderPayload["id"])) == "" {
|
||||
t.Fatalf("expected created folder to have stable id, got %#v", folderPayload["id"])
|
||||
}
|
||||
@@ -278,8 +306,8 @@ func TestCreateProjectTreeFolderOnDiskCreatesExpectedFolderShape(t *testing.T) {
|
||||
|
||||
createdFolderPath := filepath.Join(rootPath, "projects", "project-primary-project", "tree", "folder-docs")
|
||||
for _, path := range []string{
|
||||
filepath.Join(createdFolderPath, "folder.json"),
|
||||
filepath.Join(createdFolderPath, "acl.json"),
|
||||
filepath.Join(createdFolderPath, posixFolderFileName),
|
||||
filepath.Join(createdFolderPath, posixACLFileName),
|
||||
filepath.Join(createdFolderPath, "children"),
|
||||
} {
|
||||
if _, err := os.Stat(path); err != nil {
|
||||
@@ -345,7 +373,7 @@ func TestRenameProjectHierarchyFolderOnDiskRenamesFolderShape(t *testing.T) {
|
||||
t.Fatalf("expected nested child folder to move with renamed parent: %v", err)
|
||||
}
|
||||
|
||||
folderPayload := readJSONFileForTest[map[string]any](t, filepath.Join(renamedFolderPath, "folder.json"))
|
||||
folderPayload := readStructuredFileForTest[map[string]any](t, filepath.Join(renamedFolderPath, posixFolderFileName))
|
||||
if strings.TrimSpace(asStringForTest(folderPayload["id"])) == "" {
|
||||
t.Fatalf("expected renamed folder to preserve stable id, got %#v", folderPayload["id"])
|
||||
}
|
||||
@@ -392,7 +420,7 @@ func TestRenameProjectTreeFolderOnDiskRenamesFolderShape(t *testing.T) {
|
||||
t.Fatalf("unexpected renamed path: %s", renamedPath)
|
||||
}
|
||||
|
||||
folderPayload := readJSONFileForTest[map[string]any](t, filepath.Join(rootPath, filepath.FromSlash(renamedPath), "folder.json"))
|
||||
folderPayload := readStructuredFileForTest[map[string]any](t, filepath.Join(rootPath, filepath.FromSlash(renamedPath), posixFolderFileName))
|
||||
if folderPayload["name"] != "Specifications" {
|
||||
t.Fatalf("expected renamed folder name Specifications, got %#v", folderPayload["name"])
|
||||
}
|
||||
@@ -454,7 +482,7 @@ func TestMoveProjectHierarchyFolderOnDiskMovesFolderToNewParent(t *testing.T) {
|
||||
t.Fatalf("expected nested child folder to move with moved parent: %v", err)
|
||||
}
|
||||
|
||||
folderPayload := readJSONFileForTest[map[string]any](t, filepath.Join(movedFolderPath, "folder.json"))
|
||||
folderPayload := readStructuredFileForTest[map[string]any](t, filepath.Join(movedFolderPath, posixFolderFileName))
|
||||
if strings.TrimSpace(asStringForTest(folderPayload["id"])) == "" {
|
||||
t.Fatalf("expected moved folder to preserve stable id, got %#v", folderPayload["id"])
|
||||
}
|
||||
@@ -505,7 +533,7 @@ func TestMoveProjectTreeFolderOnDiskMovesFolderToNewParent(t *testing.T) {
|
||||
t.Fatalf("unexpected moved path: %s", movedPath)
|
||||
}
|
||||
|
||||
folderPayload := readJSONFileForTest[map[string]any](t, filepath.Join(rootPath, filepath.FromSlash(movedPath), "folder.json"))
|
||||
folderPayload := readStructuredFileForTest[map[string]any](t, filepath.Join(rootPath, filepath.FromSlash(movedPath), posixFolderFileName))
|
||||
if folderPayload["name"] != "Docs" {
|
||||
t.Fatalf("expected moved folder name Docs, got %#v", folderPayload["name"])
|
||||
}
|
||||
@@ -622,7 +650,7 @@ func asStringForTest(value any) string {
|
||||
return text
|
||||
}
|
||||
|
||||
func readJSONFileForTest[T any](t *testing.T, path string) T {
|
||||
func readStructuredFileForTest[T any](t *testing.T, path string) T {
|
||||
t.Helper()
|
||||
|
||||
data, err := os.ReadFile(path)
|
||||
@@ -630,9 +658,37 @@ func readJSONFileForTest[T any](t *testing.T, path string) T {
|
||||
t.Fatalf("read %s: %v", path, err)
|
||||
}
|
||||
|
||||
var raw any
|
||||
switch strings.ToLower(filepath.Ext(path)) {
|
||||
case ".cbor":
|
||||
if err := cbor.Unmarshal(data, &raw); err != nil {
|
||||
t.Fatalf("unmarshal %s: %v", path, err)
|
||||
}
|
||||
case ".json":
|
||||
if err := json.Unmarshal(data, &raw); err != nil {
|
||||
t.Fatalf("unmarshal %s: %v", path, err)
|
||||
}
|
||||
case ".jsonc":
|
||||
ast, err := hujson.Parse(data)
|
||||
if err != nil {
|
||||
t.Fatalf("parse jsonc %s: %v", path, err)
|
||||
}
|
||||
ast.Standardize()
|
||||
if err := json.Unmarshal(ast.Pack(), &raw); err != nil {
|
||||
t.Fatalf("unmarshal standardized %s: %v", path, err)
|
||||
}
|
||||
default:
|
||||
t.Fatalf("unsupported structured test file %s", path)
|
||||
}
|
||||
|
||||
normalizedBytes, err := json.Marshal(normalizeStructuredValue(raw))
|
||||
if err != nil {
|
||||
t.Fatalf("normalize %s: %v", path, err)
|
||||
}
|
||||
|
||||
var payload T
|
||||
if err := json.Unmarshal(data, &payload); err != nil {
|
||||
t.Fatalf("unmarshal %s: %v", path, err)
|
||||
if err := json.Unmarshal(normalizedBytes, &payload); err != nil {
|
||||
t.Fatalf("decode normalized %s: %v", path, err)
|
||||
}
|
||||
|
||||
return payload
|
||||
|
||||
Reference in New Issue
Block a user