Skip to content

Conversation

@seborama
Copy link
Owner

@seborama seborama commented May 2, 2025

Summary by CodeRabbit

  • New Features
    • Introduced dynamic calculation and evaluation methods for functions, variables, object properties, and methods, enabling runtime evaluation and operator application.
  • Refactor
    • Replaced the internal type classification system with idiomatic Go type switches, simplifying expression evaluation and removing the need for kind identifiers.
    • Updated error messages and output formatting for improved clarity.
  • Bug Fixes
    • Improved handling of undefined values during calculations and evaluations.
  • Tests
    • Removed obsolete test for the deprecated kind identification method.

@coderabbitai
Copy link

coderabbitai bot commented May 2, 2025

Walkthrough

This change removes the custom entryKind type and the associated kind() methods from various types, replacing them with new Calculate methods that implement dynamic evaluation logic for each type. The Tree.Calc method and related control flow are refactored to use Go type switches and direct calls to these new Calculate methods instead of relying on entryKind for dispatch. The update affects types such as Function, Variable, ObjectProperty, ObjectMethod, and Dot[T], and modifies error message formatting in some places. Test code and debug logging related to kind() are also removed.

Changes

File(s) Change Summary
function.go, variable.go Removed kind() methods; added Calculate methods for Function and Variable to perform dynamic evaluation and operator application.
object.go Removed kind() methods from Dot[T], ObjectValue, ObjectProperty, and ObjectMethod; added Calculate methods for dynamic property/method evaluation; updated String() methods; minor comment and logging tweaks.
operator.go Removed kind() method from Operator.
tree.go Removed entryKind type, constants, and all related logic; refactored Tree.Calc and string formatting to use Go type switches and new Calculate methods; removed debug logging and redundant functions.
value_bool.go, value_multivalue.go, value_number.go, value_string.go, value_undefined.go Removed kind() methods from value types; updated error message formatting in value_undefined.go.
value.go Changed import statement to multi-line block.
value_number_test.go Removed test function TestNumber_kind that verified the removed kind() method.

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant Tree
    participant Function/Variable/ObjectProperty/ObjectMethod
    participant treeConfig

    Caller->>Tree: Calc(val, op, cfg)
    Tree->>+Function/Variable/ObjectProperty/ObjectMethod: Calculate(val, op, cfg)
    Function/Variable/ObjectProperty/ObjectMethod->>treeConfig: (if needed) Lookup or evaluate value/body
    Function/Variable/ObjectProperty/ObjectMethod-->>Tree: Result (possibly after operator application)
    Tree-->>Caller: Final result
Loading

Possibly related PRs

  • feat: ObjectMethod + comments #23: Introduces a new ObjectMethod type and related handling for user-defined object methods, using the previous kind()-based dispatch. This PR is directly related as the current changes refactor and extend the approach by replacing kind() with Calculate methods for dynamic evaluation.

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 golangci-lint (1.64.8)

Error: can't load config: can't set severity rule option: no default severity defined
Failed executing command with error: can't load config: can't set severity rule option: no default severity defined

✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 7

🔭 Outside diff range comments (1)
tree.go (1)

221-236: ⚠️ Potential issue

Incorrect %T formatting in accessor error + duplicated logic

Both objectAccessorDotFunctionFn and objectAccessorDotVariableFn build the error message with fmt.Sprintf("%T", val) where %T already expects val.

-return NewUndefinedWithReasonf(
-    "syntax error: object accessor function called on unknown or non-function member: [object: '%T'] [member: '%s']",
-    fmt.Sprintf("%T", val), fn.Name)
+return NewUndefinedWithReasonf(
+    "syntax error: object accessor function called on unknown or non-function member: [object: '%T'] [member: '%s']",
+    val, fn.Name)

Please apply the same change in objectAccessorDotVariableFn.

🧹 Nitpick comments (4)
value_undefined.go (2)

91-97: Clarify error propagation & avoid potential self-reference confusion

The reformulated error message is clearer than the previous kind()-based one – nice improvement.
One minor point: for other values that already represent an Undefined, other.String() will return "undefined" (or a longer reason). That is fine, but if the aim is to pinpoint the non-undefined operand, you might instead want to flag when other is not Undefined.

Nothing blocking, just worth considering if the error text is meant to single out the valid operand.


110-115: IsUndefined returns the inverse of its name

IsUndefined currently returns true when no reason is set (u.reason == "").
This means an Undefined with a reason (the most common case in error paths) reports not undefined.

Although this predates the refactor, it keeps tripping consumers that rely on the helper. Consider flipping the logic or renaming the method to avoid ambiguity.

tree.go (2)

113-118: Debug print left in production path

fmt.Printf("DEBUG - Tree.Calc: entry in Tree %T\n", typedE) will spam stdout during every evaluation.

-            fmt.Printf("DEBUG - Tree.Calc: entry in Tree %T\n", typedE)
+            // fmt.Printf removed – avoid noisy output in production

195-199: Unused //nolint:errcheck directives and false-positive linter error

calculate returns only Value; therefore errcheck is irrelevant and the directives are now flagged as “unused”. Simply drop them:

-//nolint:errcheck // life's too short to check for type assertion success here

If you keep type assertions, prefer explicit ok checks instead of silencing the linter.

Also applies to: 292-293

🧰 Tools
🪛 GitHub Actions: Test and Build

[error] 195-195: Unused nolint directive for linter "errcheck" (nolintlint): //nolint:errcheck // life's too short to check for type assertion success here

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1a61c79 and cf9cadb.

📒 Files selected for processing (12)
  • function.go (1 hunks)
  • object.go (4 hunks)
  • operator.go (0 hunks)
  • tree.go (9 hunks)
  • value.go (1 hunks)
  • value_bool.go (0 hunks)
  • value_multivalue.go (0 hunks)
  • value_number.go (0 hunks)
  • value_number_test.go (0 hunks)
  • value_string.go (0 hunks)
  • value_undefined.go (1 hunks)
  • variable.go (1 hunks)
💤 Files with no reviewable changes (6)
  • value_number.go
  • value_string.go
  • value_number_test.go
  • value_multivalue.go
  • value_bool.go
  • operator.go
🧰 Additional context used
🧬 Code Graph Analysis (4)
value_undefined.go (3)
value_bool.go (1)
  • Bool (5-8)
value_string.go (1)
  • String (5-8)
value.go (1)
  • Value (7-13)
object.go (5)
variable.go (1)
  • Variable (3-5)
value.go (1)
  • Value (7-13)
value_undefined.go (4)
  • NewUndefinedWithReasonf (24-28)
  • Undefined (16-18)
  • Undefined (91-93)
  • Undefined (95-97)
function.go (1)
  • NewFunction (25-31)
tree_config.go (3)
  • WithFunctions (142-146)
  • WithVariables (134-138)
  • WithObjects (152-156)
function.go (4)
operator.go (1)
  • Operator (3-3)
tree_config.go (3)
  • WithFunctions (142-146)
  • WithVariables (134-138)
  • WithObjects (152-156)
value_undefined.go (3)
  • Undefined (16-18)
  • Undefined (91-93)
  • Undefined (95-97)
value.go (1)
  • Value (7-13)
variable.go (3)
operator.go (1)
  • Operator (3-3)
value_undefined.go (3)
  • Undefined (16-18)
  • Undefined (91-93)
  • Undefined (95-97)
value.go (1)
  • Value (7-13)
🪛 GitHub Actions: Test and Build
function.go

[error] 49-49: Error return value is not checked (errcheck) at val = calculate(val.(Value), op, rhsVal)

variable.go

[error] 25-25: Error return value is not checked (errcheck) at val = calculate(val.(Value), op, rhsVal)

tree.go

[error] 190-190: Error return value is not checked (errcheck) at val = calculate(val.(Value), op, rhsVal)


[error] 195-195: Unused nolint directive for linter "errcheck" (nolintlint): //nolint:errcheck // life's too short to check for type assertion success here


[error] 292-292: Unused nolint directive for linter "errcheck" (nolintlint): //nolint:errcheck // life's too short to check for type assertion success here

🔇 Additional comments (1)
value.go (1)

3-5: Import change looks good

The switch from a single-line to a multi-line import is syntactically correct and more future-proof should further imports be required.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

♻️ Duplicate comments (1)
function.go (1)

33-53: ⚠️ Potential issue

Propagate calculate errors to callers (recurring issue)

The error from calculate on line 50 is silently discarded, failing errcheck and masking run-time issues, as also noted in previous reviews.

Apply this diff to properly handle type assertion and calculation errors:

-	//nolint:errcheck // life's too short to check for type assertion success here
-	val = calculate(val.(Value), op, rhsVal)
-
-	return val
+	lhs, ok := val.(Value)
+	if !ok {
+		return NewUndefinedWithReasonf(
+			"internal error: left-hand side to function '%s' is not a Value (got %T)",
+			f.Name, val,
+		)
+	}
+
+	result := calculate(lhs, op, rhsVal)
+	if u, ok := result.(Undefined); ok {
+		return NewUndefinedWithReasonf("function '%s': %s", f.Name, u.reason)
+	}
+	return result

This improves error handling by:

  1. Checking type assertion success
  2. Propagating errors from calculation
  3. Adding context about which function caused the error
🧹 Nitpick comments (1)
tree.go (1)

149-150: Consider refactoring Dot[T] specializations

The TODO comment suggests Dot[Member] no longer makes sense as implementations of Calculate have diverged. This would be an opportunity to evaluate whether generic Dot[T] is still appropriate or if separate implementations would be clearer.

Consider refactoring to separate concrete types like DotFunction and DotVariable if the implementations are significantly different.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between cf9cadb and 17a3c0a.

📒 Files selected for processing (4)
  • function.go (1 hunks)
  • object.go (3 hunks)
  • tree.go (6 hunks)
  • variable.go (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • variable.go
  • object.go
🧰 Additional context used
🧬 Code Graph Analysis (1)
function.go (3)
tree_config.go (3)
  • WithFunctions (142-146)
  • WithVariables (134-138)
  • WithObjects (152-156)
value_undefined.go (3)
  • Undefined (16-18)
  • Undefined (91-93)
  • Undefined (95-97)
value.go (1)
  • Value (7-13)
🔇 Additional comments (2)
tree.go (2)

291-315: Fixed string formatting in Tree.String() method

The previous tree formatting issue mentioned in past reviews has been resolved in the type switch implementation. The code now correctly handles each type with appropriate formatting verbs.

The refactoring to use Go type switches instead of enum-based switch makes the code more idiomatic and maintainable.


176-194: LGTM: New Tree.Calculate method implementation

The new Calculate method follows a consistent pattern with other implementations, properly handling edge cases and preserving error context.

@seborama seborama enabled auto-merge May 2, 2025 21:52
@seborama seborama merged commit e42687d into main May 2, 2025
3 checks passed
@seborama seborama deleted the removeEntryType_addCalculate branch May 2, 2025 21:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants