@rbxts/reflector
Version:
Reflector: Metadata Reflection API for Roblox-TS
133 lines (132 loc) • 5.39 kB
TypeScript
/// <reference types="@rbxts/compiler-types" />
export type PropertyKey = string | symbol;
export type MetadataStore = Map<string, Map<string, any>>;
/**
* Represents a Reflector class that provides
* reflection capabiltiies.
*
* @class Reflector
* @classdesc The Reflector class allows introspetion and dynamic manipulation of objects and classes.
*/
export default class Reflector {
/**
* Defines metadata for the specified class with the given key and value.
*
* @static
* @param {string} metadataKey - The key identifying the metadata.
* @param {*} metadataValue - The value of the metadata to set.
* @returns {void}
*
* @example
* // Define metadata for a class method
* class MyClass {
* (at)Reflector.defineMetadata('description', 'This is a method')
* myMethod() { ... }
* }
*/
static defineMetadata(metadataKey: string, metadataValue: any): (...args: unknown[]) => void;
/**
* Defines metadata for the specified object or class with the given key, value, and property key.
*
* @static
* @param {*} target - The object or class to define metadata for.
* @param {string} metadataKey - The key identifying the metadata.
* @param {*} metadataValue - The value of the metadata to set.
* @param {PropertyKey?} propertyKey - The key identifying the property to associate the metadata with.
* @returns {void}
*
* @example
* // Define metadata for an object property
* const myObject = {}
*
* Reflector.defineMetadata(myObject, "description", "This is a property", "myProperty")
*/
static defineMetadata(target: any, metadataKey: string, metadataValue: any, propertyKey?: PropertyKey): void;
/**
* Retrieves an array of keys for the metadata associated with the specific object or class.
*
* @static
* @param {*} target - The object or class to define metadata for.
* @param {PropertyKey?} propertyKey - The key identifying the property to associate the metadata with.
* @returns {Array<PropertyKey>|undefined}
*
* @example
* // Define metadata for class property
* class MyClass { }
*
* Reflector.defineMetadata(MyClass, "version", "1.0");
* Reflector.defineMetadata(MyClass, "author", "John Doe");
*
* const metadataKeys = Reflector.getMetadataKeys(MyClass);
* // ^ metadataKeys: ['version', 'author']
*
* // Retrieve metadata keys for an object
* const myObject = {}
*
* Reflector.defineMetadata(myObject, "description", "This is an object");
* Reflector.defineMetadata(myObject, "createdBy", "Jane Smith");
*
* const metadataKeys = Reflector.getMetadataKeys(myObject);
* // ^ metadataKeys: ['description', 'createdBy']
*/
static getMetadataKeys(target: any, propertyKey?: PropertyKey): string[] | undefined;
/**
* Checks if the specified object or class has metadata associated with the given key.
*
* @static
* @param {*} target - The object or class to define metadata for.
* @param {string} metadataKey - The key identifying the metadata.
* @param {PropertyKey?} propertyKey - The key identifying the property to associate the metadata with.
* @returns {boolean} True if metadata exists for the key, false otherwise.
*
* @example
* // Check if a class has specific metadata
* class MyClass {}
*
* Reflector.defineMetadata(MyClass, 'version', '1.0');
*
* const hasVersionMetadata = Reflector.hasMetadata(MyClass, 'version');
* ^ // hasVersionMetadata: true
*
* const hasAuthorMetadata = Reflector.hasMetadata(MyClass, 'author');
* // hasAuthorMetadata: false
*
* // Check if an object has specific metadata
* const myObject = {};
*
* Reflector.defineMetadata(myObject, 'description', 'This is an object');
*
* const hasDescriptionMetadata = Reflector.hasMetadata(myObject, 'description');
* // hasDescriptionMetadata: true
*
* const hasCreatedByMetadata = Reflector.hasMetadata(myObject, 'createdBy');
* // hasCreatedByMetadata: false
*/
static hasMetadata(target: any, metadataKey: string, propertyKey?: PropertyKey): boolean;
/**
* Retrieves the metadata value associated with the given key on the specified object or class.
*
* @static
* @param {*} target - The object or class to retrieve metadata from.
* @param {string} metadataKey - The key identifying the metadata.
* @param {PropertyKey} propertyKey - The key identifying the property to associate the metadata with.
* @returns {*} The value of the metadata associated with the key, or undefined if not found.
*/
static getMetadata<T>(target: any, metadataKey: string, propertyKey?: PropertyKey): T | undefined;
/**
* @internal
* @private
* @static
* @param {Target} target
* @returns {*} {(MetadataStore | undefined)}
*/
private static getMetadataStore;
/**
* @internal
* @private
* @static
* @param {Target} target
* @returns {*} {MetadataStore}
*/
private static createOrGetMetadataStore;
}