Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 15, 2026

Formula Engine Specification - Type Safety Fixes ✅

  • Create comprehensive formula specification document (docs/spec/formula.md)
    • Define formula field types and syntax
    • Document expression evaluation engine
    • Specify supported operators and functions
    • Define special variables and context
    • Include error handling and best practices
  • Create formula-specific TypeScript types in @objectql/types
    • Define FormulaConfig interface
    • Define FormulaContext interface
    • Define formula evaluation types
    • Define formula error types
    • Fix strict typing violations
  • Update documentation index to reference new formula spec
  • Add formula spec examples and use cases

Summary

Created a comprehensive formula engine specification for ObjectQL including:

Documentation (docs/spec/formula.md)

  • 1,149 lines of detailed specification
  • Complete syntax guide for JavaScript-style expressions
  • Field references, operators, and functions
  • System variables ($today, $now, $current_user)
  • Relationship traversal via dot notation
  • 50+ practical examples across multiple domains (e-commerce, CRM, project management, HR)
  • Error handling patterns and best practices
  • Performance considerations and limitations

TypeScript Types (packages/foundation/types/src/formula.ts)

  • 450+ lines of type definitions (enhanced with strict typing)
  • Type Safety Improvements:
    • Added FormulaValue type for return values
    • Added FormulaFieldValue type for field values
    • Added FormulaErrorContext interface for error context
    • Added FormulaCustomFunction type for custom functions
    • Replaced all any types with specific types
    • Changed FormulaError to extend ObjectQLError
  • FormulaFieldConfig - Field configuration interface
  • FormulaContext - Runtime evaluation context
  • FormulaEvaluationResult - Result type
  • FormulaError and FormulaErrorType - Comprehensive error handling
  • FormulaMetadata - Introspection support
  • FormulaExecutionStats - Performance monitoring
  • FormulaEngineConfig - Engine configuration

Integration

  • Updated docs/spec/index.md to include formula specification
  • Enhanced packages/foundation/types/src/field.ts with formula properties
  • Fixed type safety violations in field.ts (replaced any[] with unknown[])
  • Exported all formula types from @objectql/types package
  • TypeScript compilation verified ✅

Type Safety Fixes (Addressing Code Review)

  1. FormulaError now extends ObjectQLError instead of Error
  2. ✅ Replaced any in treat_blank_as with FormulaValue type
  3. ✅ Replaced any in example/test case inputs/results with FormulaFieldValue and FormulaValue
  4. ✅ Replaced any in FormulaContext.record with Record<string, FormulaFieldValue>
  5. ✅ Replaced any in FormulaUserContext index signature with unknown
  6. ✅ Replaced any in FormulaEvaluationResult.value with FormulaValue
  7. ✅ Added FormulaErrorContext interface and replaced any context parameter
  8. ✅ Replaced generic Function type with FormulaCustomFunction type
  9. ✅ Fixed filters type in field.ts from any[] to unknown[]
  10. ✅ Fixed treat_blank_as in field.ts to use union of primitive types
Original prompt

创建公式引擎规范


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

@hotlong
Copy link
Contributor

hotlong commented Jan 15, 2026

@copilot 继续

Copy link
Contributor Author

Copilot AI commented Jan 15, 2026

@copilot 继续

Formula engine specification completed ✅

Created comprehensive documentation and TypeScript types:

Documentation (docs/spec/formula.md):

  • 1,149 lines covering complete formula syntax
  • JavaScript-style expressions with field references
  • System variables ($today, $now, $current_user)
  • 50+ examples across e-commerce, CRM, project management, HR domains
  • Error handling patterns and best practices

TypeScript Types (@objectql/types/formula.ts):

  • Complete type definitions for formula engine
  • FormulaFieldConfig, FormulaContext, FormulaEvaluationResult
  • Error handling types and performance monitoring interfaces
  • All types exported and TypeScript compilation verified

All checklist items completed. Ready for review.

Copilot AI changed the title [WIP] Create specification for formula engine Create comprehensive formula engine specification Jan 15, 2026
Copilot AI requested a review from hotlong January 15, 2026 06:00
@huangyiirene huangyiirene requested review from Copilot and removed request for hotlong and huangyiirene January 15, 2026 06:02
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR establishes a comprehensive formula engine specification for ObjectQL, introducing formula fields as read-only calculated fields that derive values using JavaScript-style expressions. The specification provides complete documentation and type definitions for implementing metadata-driven calculations without custom code.

Changes:

  • Created a 1,149-line formula specification document with syntax reference, examples, and best practices
  • Defined TypeScript types for formula configuration, evaluation context, and error handling
  • Updated core type definitions to support formula-specific field properties

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 12 comments.

Show a summary per file
File Description
docs/spec/formula.md Comprehensive specification covering expression syntax, operators, system variables, built-in functions, advanced patterns, error handling, and 50+ domain-specific examples
packages/foundation/types/src/formula.ts Complete TypeScript type definitions including FormulaFieldConfig, FormulaContext, FormulaEvaluationResult, and error handling types
packages/foundation/types/src/field.ts Enhanced FieldConfig interface with formula-specific properties (data_type, format, precision, blank_as_zero, treat_blank_as)
packages/foundation/types/src/index.ts Added export statement to make formula types available from @objectql/types package
docs/spec/index.md Updated specification index to include formula fields in the Core Data Layer section

Comment on lines 251 to 261
export class FormulaError extends Error {
constructor(
public type: FormulaErrorType,
public message: string,
public expression?: string,
public context?: any
) {
super(message);
this.name = 'FormulaError';
}
}
Copy link

Copilot AI Jan 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The FormulaError class extends the standard Error class instead of using the ObjectQLError defined in @objectql/types. According to the coding guidelines, all errors should use ObjectQLError. Consider refactoring this to extend ObjectQLError or integrate with the existing error handling system.

Copilot generated this review using guidance from repository custom instructions.
public type: FormulaErrorType,
public message: string,
public expression?: string,
public context?: any
Copy link

Copilot AI Jan 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The context parameter is typed as 'any', which violates the strict typing requirement. Consider defining a specific interface for the context or using a more specific type.

Copilot generated this review using guidance from repository custom instructions.
/** Treat blank/null as zero in formula calculations. */
blank_as_zero?: boolean;
/** Default value for null/undefined referenced fields in formulas. */
treat_blank_as?: any;
Copy link

Copilot AI Jan 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The treat_blank_as property is typed as 'any', which violates the strict typing constraint. Consider using a union type of possible default values or defining a more specific type that matches the supported data types.

Copilot generated this review using guidance from repository custom instructions.
blank_as_zero?: boolean;

/** Default value to use when referenced fields are null/undefined */
treat_blank_as?: any;
Copy link

Copilot AI Jan 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The treat_blank_as property is typed as 'any', which violates the strict typing constraint. Consider using a union type of possible default values or defining a more specific type that matches FormulaDataType.

Copilot generated this review using guidance from repository custom instructions.
description: string;

/** Input field values */
inputs: Record<string, any>;
Copy link

Copilot AI Jan 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The inputs Record uses 'any' for values, which violates strict typing. Consider defining a union type for valid input values or using a more specific type constraint.

Copilot generated this review using guidance from repository custom instructions.
input: Record<string, any>;

/** Expected output value */
expected: any;
Copy link

Copilot AI Jan 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The expected property is typed as 'any'. Consider constraining this to match FormulaDataType return values.

Copilot generated this review using guidance from repository custom instructions.
*/
export interface FormulaContext {
/** Current record data with all field values */
record: Record<string, any>;
Copy link

Copilot AI Jan 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The record property uses 'any' for field values. Consider defining a more specific type or constraint for field values.

Copilot generated this review using guidance from repository custom instructions.
role?: string;

/** Additional user properties */
[key: string]: any;
Copy link

Copilot AI Jan 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The index signature uses 'any' type. Consider using 'unknown' for better type safety or defining a more specific constraint for additional user properties.

Copilot generated this review using guidance from repository custom instructions.
*/
export interface FormulaEvaluationResult {
/** Computed value */
value: any;
Copy link

Copilot AI Jan 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The value property is typed as 'any'. Consider using a union type based on FormulaDataType for better type safety.

Copilot generated this review using guidance from repository custom instructions.
enable_monitoring?: boolean;

/** Custom function library */
custom_functions?: Record<string, Function>;
Copy link

Copilot AI Jan 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using the generic 'Function' type is not type-safe. Consider defining a more specific function signature or using a constrained generic type for custom functions.

Copilot generated this review using guidance from repository custom instructions.
@huangyiirene
Copy link
Contributor

@copilot fix

Copy link
Contributor Author

Copilot AI commented Jan 15, 2026

@copilot fix

Fixed all type safety violations in commit 3a86cdf:

Changes Made:

  1. FormulaError now extends ObjectQLError instead of Error
  2. ✅ Added FormulaValue and FormulaFieldValue types to replace all any types
  3. ✅ Added FormulaErrorContext interface for type-safe error context
  4. ✅ Added FormulaCustomFunction type to replace generic Function type
  5. ✅ Updated all Record types to use unknown for index signatures
  6. ✅ Fixed types in both formula.ts and field.ts

TypeScript compilation verified with strict mode. All 12 reviewer comments addressed.

Copilot AI requested a review from huangyiirene January 15, 2026 06:26
@hotlong hotlong marked this pull request as ready for review January 15, 2026 06:31
@hotlong hotlong merged commit b6cc0aa into main Jan 15, 2026
7 checks passed
@hotlong hotlong deleted the copilot/create-formula-engine-specification branch January 15, 2026 06:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants