Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deep nested diff of value #1407

Merged
merged 0 commits into from
Aug 20, 2024
Merged

Deep nested diff of value #1407

merged 0 commits into from
Aug 20, 2024

Conversation

brentdur
Copy link

Features and Changes

This changes the diff viewer to show value as nested JSON if the value is JSON instead of showing it as a string.

Testing

  • Make a change to an environment rule that uses the JSON type

Screenshots

Before:
image

After:
image

@@ -97,11 +110,15 @@ export default function DraftModal({
title: `Rules - ${env.id}`,
a: JSON.stringify(
feature.environmentSettings?.[env.id]?.rules || [],
null,
nestedJSONReplacer,
Copy link
Member

@jdorn jdorn Jul 4, 2023

Choose a reason for hiding this comment

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

I don't know if using a replacer here is the right approach. Replacers are applied recursively, so if the feature value is a JSON string that itself has a key called value, it will try to recursively expand it. There are only 2 places where values are specified in a rule, so explicitly JSON parsing those might be safer.

Something like this:

function jsonParseSafe(value: string): unknown {
  try {
    return JSON.parse(value);
  } catch (e) {
    // If parse error, return the original value
    return value;
  }
}

function expandValues(rules: FeatureRule[]) {
  return rules.map((rule) => {
    // Multiple values (A/B test rules)
    if (rule.type === "experiment") {
      return {
        ...rule,
        values: rule.values.map((variation) => ({
          ...variation,
          value: jsonParseSafe(variation.value),
        })),
      };
    }
    // Single value (Force and Rollout rules)
    else {
      return {
        ...rule,
        value: jsonParseSafe(rule.value),
      };
    }
  });
}

// When doing the diff
a: JSON.stringify(
  expandValues(feature.environmentSettings?.[env.id]?.rules || []),
  null,
  2
)

@lukesonnet lukesonnet merged commit 01d91c1 into growthbook:main Aug 20, 2024
5 checks passed
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.

[Feature] Improve diff changes view to highlight only changes in nested keys
3 participants