-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
191 additions
and
26 deletions.
There are no files selected for viewing
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,60 @@ | ||
/** | ||
* Copyright (c) 2023 Gitpod GmbH. All rights reserved. | ||
* Licensed under the GNU Affero General Public License (AGPL). | ||
* See License.AGPL.txt in the project root for license information. | ||
*/ | ||
|
||
export type ObjectId = { | ||
kind: ObjectKind; | ||
value: string; | ||
}; | ||
export type ObjectKind = keyof typeof ObjectKindNames; | ||
const ObjectKindNames = { | ||
installation: "inst", | ||
user: "user", | ||
organization: "org", | ||
project: "proj", | ||
workspace: "ws", | ||
}; | ||
export const ObjectKindByShortName: ReadonlyMap<string, ObjectKind> = new Map( | ||
Object.keys(ObjectKindNames).map((k) => { | ||
return [ObjectKindNames[k as ObjectKind], k as ObjectKind]; | ||
}), | ||
); | ||
|
||
export namespace ObjectId { | ||
const SEPARATOR = "_"; | ||
export function create(kind: ObjectKind, value: string): ObjectId { | ||
switch (kind) { | ||
case "installation": | ||
return { kind, value }; | ||
case "user": | ||
return { kind, value }; | ||
case "organization": | ||
return { kind, value }; | ||
case "project": | ||
return { kind, value }; | ||
case "workspace": | ||
return { kind, value }; | ||
} | ||
} | ||
export function isObjectKind(str: string): str is ObjectKind { | ||
return !!ObjectKindNames[str as ObjectKind]; | ||
} | ||
export function toString(id: ObjectId): string { | ||
const prefix = ObjectKindNames[id.kind]; | ||
return prefix + SEPARATOR + id.value; | ||
} | ||
export function tryParse(str: string): ObjectId { | ||
const parts = str.split(SEPARATOR); | ||
if (parts.length < 2) { | ||
throw new Error(`Unable to parse ObjectId`); | ||
} | ||
const kind = ObjectKindByShortName.get(parts[0]); | ||
if (!kind) { | ||
throw new Error(`Unable to parse ObjectId: unknown objectKind!`); | ||
} | ||
const value = parts.slice(1).join(); | ||
return { kind, value }; | ||
} | ||
} |
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