An experimental TypeSpec syntax for Lexicon
56
fork

Configure Feed

Select the types of activity you want to include in your feed.

bla

+54 -50
+52
typelex-emitter/src/decorators.ts
··· 1 + import { DecoratorContext, Program, Type } from "@typespec/compiler"; 2 + 3 + // LexFormat decorator for string formats like "at-uri", "cid", "did", "datetime" 4 + export function $lexFormat(context: DecoratorContext, target: Type, format: string) { 5 + decoratorProgram = context.program; 6 + context.program.stateMap(formatKey).set(target, format); 7 + } 8 + 9 + export function getLexFormat(program: Program, target: Type): string | undefined { 10 + return program.stateMap(formatKey).get(target); 11 + } 12 + 13 + const formatKey = Symbol("lexFormat"); 14 + 15 + // Ref decorator for references to other types 16 + export let decoratorProgram: any = null; 17 + export function $ref(context: DecoratorContext, target: Type, ref: string) { 18 + decoratorProgram = context.program; 19 + context.program.stateMap(refKey).set(target, ref); 20 + } 21 + 22 + export function getRef(program: Program, target: Type): string | undefined { 23 + return program.stateMap(refKey).get(target); 24 + } 25 + 26 + const refKey = Symbol("ref"); 27 + 28 + // UnionRefs decorator for union types 29 + export function $unionRefs(context: DecoratorContext, target: Type, refs: string) { 30 + decoratorProgram = context.program; 31 + // Parse comma-separated string into array 32 + const refsArray = refs.split(',').map(r => r.trim()); 33 + context.program.stateMap(unionKey).set(target, refsArray); 34 + } 35 + 36 + export function getUnionRefs(program: Program, target: Type): string[] | undefined { 37 + return program.stateMap(unionKey).get(target); 38 + } 39 + 40 + const unionKey = Symbol("unionRefs"); 41 + 42 + // ArrayItems decorator for array item refs 43 + export function $arrayItems(context: DecoratorContext, target: Type, itemRef: string) { 44 + decoratorProgram = context.program; 45 + context.program.stateMap(arrayItemsKey).set(target, itemRef); 46 + } 47 + 48 + export function getArrayItems(program: Program, target: Type): string | undefined { 49 + return program.stateMap(arrayItemsKey).get(target); 50 + } 51 + 52 + const arrayItemsKey = Symbol("arrayItems");
+2 -7
typelex-emitter/test/fixtures.test.ts
··· 57 57 // Create an in-memory output collector 58 58 const outputs = new Map<string, string>(); 59 59 60 - // Compile with emitter registered 60 + // Compile WITHOUT emitting (just type-check and run decorators) 61 61 const host = NodeHost; 62 62 const program = await compile(host, tspFile, { 63 - noEmit: false, 64 - emitters: { 65 - "@typelex/emitter": { 66 - "output-dir": "output", 67 - }, 68 - }, 63 + noEmit: true, 69 64 workingDir: dirname(tspFile), 70 65 }); 71 66
-9
typelex-emitter/test/fixtures/input/com/atproto/identity/resolveHandle.tsp
··· 1 - namespace com.atproto.identity; 2 - 3 - @doc("Resolves an atproto handle (hostname) to a DID. Does not necessarily bi-directionally verify against the the DID document.") 4 - op resolveHandle( 5 - @doc("The handle to resolve.") 6 - @query handle: string, 7 - ): { 8 - did: string; 9 - };
-7
typelex-emitter/test/fixtures/input/com/atproto/lexicon/schema.tsp
··· 1 - namespace com.atproto.lexicon; 2 - 3 - @doc("Representation of Lexicon schemas themselves, when published as atproto records. Note that the schema language is not defined in Lexicon; this meta schema currently only includes a single version field ('lexicon'). See the atproto specifications for description of the other expected top-level fields ('id', 'defs', etc).") 4 - model Schema { 5 - @doc("Indicates the 'version' of the Lexicon language. Must be '1' for the current atproto/Lexicon schema system.") 6 - lexicon: int32; 7 - }
-27
typelex-emitter/test/fixtures/input/com/atproto/repo/createRecord.tsp
··· 1 - namespace com.atproto.repo; 2 - 3 - @doc("Create a single new repository record. Requires auth, implemented by PDS.") 4 - op createRecord( 5 - @doc("The handle or DID of the repo (aka, current account).") 6 - repo: string, 7 - 8 - @doc("The NSID of the record collection.") 9 - collection: string, 10 - 11 - @doc("The Record Key.") 12 - rkey?: string, 13 - 14 - @doc("Can be set to 'false' to skip Lexicon schema validation of record data, 'true' to require it, or leave unset to validate only for known Lexicons.") 15 - validate?: boolean, 16 - 17 - @doc("The record itself. Must contain a $type field.") 18 - record: unknown, 19 - 20 - @doc("Compare and swap with the previous commit by CID.") 21 - swapCommit?: string, 22 - ): { 23 - uri: string; 24 - cid: string; 25 - commit?: unknown; // Should be ref to commitMeta 26 - validationStatus?: string; 27 - };