How to use json-merge-patch - 10 common examples

To help you get started, we’ve selected a few json-merge-patch examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github amtrack / sfdx-browserforce-plugin / src / plugins / security / certificate-and-key-management / index.ts View on Github external
public diff(state, definition) {
    const response = {
      certificates: [],
      importFromKeystore: []
    };
    if (state && definition && state.certificates && definition.certificates) {
      for (const cert of definition.certificates) {
        const existingCert = state.certificates.find(
          c => c.name === cert.DeveloperName
        );
        if (existingCert) {
          // move id from state to definition to be retained and used
          cert.id = existingCert.id;
          delete existingCert.id;
        }
        response.certificates.push(jsonMergePatch.generate(existingCert, cert));
      }
    }
    if (definition && definition.importFromKeystore) {
      response.importFromKeystore = definition.importFromKeystore;
    }
    return removeEmptyValues(response);
  }
github argoproj / argo-workflows / ui / src / app / cron-workflows / components / cron-workflow-summary-panel.tsx View on Github external
onSubmit={(value: CronWorkflow) => {
                            // magic - we get the latest from the server and then apply the changes from the rendered version to this
                            const original = props.cronWorkflow;
                            const patch = jsonMergePatch.generate(original, value) || {};
                            return services.cronWorkflows
                                .get(props.cronWorkflow.metadata.name, props.cronWorkflow.metadata.namespace)
                                .then(latest => jsonMergePatch.apply(latest, patch))
                                .then(patched => services.cronWorkflows.update(patched, props.cronWorkflow.metadata.name, props.cronWorkflow.metadata.namespace))
                                .then(updated => props.onChange(updated));
                        }}
                    />
github amtrack / sfdx-browserforce-plugin / src / plugins / customer-portal / availableCustomObjects / index.ts View on Github external
if (state && definition) {
      for (const availableCustomObject of definition) {
        const oldCustomObject = state.find(customObject => {
          if (availableCustomObject.namespacePrefix === undefined) {
            availableCustomObject.namespacePrefix = null;
          }
          return (
            customObject.name === availableCustomObject.name &&
            customObject.namespacePrefix ===
              availableCustomObject.namespacePrefix
          );
        });
        // move id of existing object to new object to be retained and used
        availableCustomObject.id = oldCustomObject.id;
        delete oldCustomObject.id;
        const diff = jsonMergePatch.generate(
          oldCustomObject,
          availableCustomObject
        );
        if (diff.available !== undefined) {
          response.push(diff);
        }
      }
    }
    return response;
  }
github mozilla-lockwise / lockbox-datastore / lib / items.js View on Github external
// apply read-only values
  source = source || {};
  destination.id = source.id || UUID();
  destination.last_used = source.last_used || null;
  destination.disabled = source.disabled || false;
  destination.created = source.created || new Date().toISOString();
  // always assume the item is modified
  destination.modified = new Date().toISOString();

  // generate history patch (to go backward)
  let history = [];
  if (source && source.entry) {
    history = (source.history || []).slice(0, HISTORY_MAX - 1);
    let dstEntry = destination.entry,
        srcEntry = source.entry;
    let patch = jsonmergepatch.generate(dstEntry, srcEntry);
    if (undefined !== patch) {
      history.unshift({
        created: new Date().toISOString(),
        patch
      });
    }
  }
  destination.history = history;

  return destination;
}
github amtrack / sfdx-browserforce-plugin / src / plugins / customer-portal / portals / index.ts View on Github external
const membershipResponse = [];
          for (const member of portal.portalProfileMemberships) {
            // move id of existing member to new member to be retained and used
            const sourceMember = sourcePortal.portalProfileMemberships.find(
              m => m.name === member.name
            );
            if (sourceMember) {
              member.id = sourceMember.id;
              delete sourceMember.id;
            } else {
              throw new Error(
                `Could not find portal profile membership for '${member.name}'`
              );
            }
            const membershipDiff = semanticallyCleanObject(
              removeNullValues(jsonMergePatch.generate(sourceMember, member))
            );
            if (membershipDiff) {
              membershipResponse.push(membershipDiff);
            }
          }
          delete sourcePortal.portalProfileMemberships;
          delete portal.portalProfileMemberships;
          if (membershipResponse.length) {
            portal['portalProfileMemberships'] = membershipResponse;
          }
        }
        const diff = semanticallyCleanObject(
          removeNullValues(jsonMergePatch.generate(sourcePortal, portal))
        );
        if (diff) {
          response.push(diff);
github amtrack / sfdx-browserforce-plugin / src / plugins / company-settings / critical-updates / index.ts View on Github external
const response = [];
    for (const stateItem of state) {
      const targetMatch = definition.find(
        item =>
          multimatch(
            [stateItem.name],
            Array.isArray(item.name) ? item.name : [item.name]
          ).length > 0
      );
      if (targetMatch) {
        const newDefinition = Object.assign({}, targetMatch);
        // replace the pattern by the real name
        newDefinition.name = stateItem.name;
        // copy comment to state for diffing
        stateItem['comment'] = newDefinition.comment;
        const diff = jsonMergePatch.generate(stateItem, newDefinition);
        if (diff) {
          response.push(newDefinition);
        }
      }
    }
    return response;
  }
github garden-io / garden / garden-service / src / plugins / kubernetes / helm / build.ts View on Github external
// update the local helm repo and retry
      log.debug("Updating Helm repo...")
      await helm(namespace, context, log, ...["repo", "update"])
      log.debug("Fetching chart (after updating)...")
      await fetchChart(namespace, context, log, module)
    }
  }

  const chartPath = await getChartPath(module)

  // create the values.yml file (merge the configured parameters into the default values)
  log.debug("Preparing chart...")
  const chartValues = safeLoad(await helm(namespace, context, log, "inspect", "values", chartPath)) || {}

  // Merge with the base module's values, if applicable
  const specValues = baseModule ? jsonMerge(baseModule.spec.values, module.spec.values) : module.spec.values

  const mergedValues = jsonMerge(chartValues, specValues)

  const valuesPath = getValuesPath(chartPath)
  log.silly(`Writing chart values to ${valuesPath}`)
  await dumpYaml(valuesPath, mergedValues)

  return { fresh: true }
}
github remy / jsonbin / lib / routes / api.js View on Github external
const path = urlToArray(req.path);
  const value = undefsafe(user.store, path.join('.'));

  if (value === undefined) {
    return res.status(404).json(null);
  }

  const parent = path.length ?
    undefsafe(user.store, path.join('.')) :
    user.store;

  // merge
  if (Array.isArray(parent)) {
    parent.push(req.body);
  } else {
    const result = jsonmergepatch.apply(parent, req.body);
    undefsafe(user.store, path.join('.'), result);
  }

  user.dirty('storeJson', { method: 'PATCH', path: path.join('.') });
  user.save().then(user => {
    res.status(200).json(undefsafe(user.store, path.join('.')));
  }).catch(e => {
    next(422);
  });
});
github garden-io / garden / garden-service / src / plugins / kubernetes / helm / build.ts View on Github external
await helm(namespace, context, log, ...["repo", "update"])
      log.debug("Fetching chart (after updating)...")
      await fetchChart(namespace, context, log, module)
    }
  }

  const chartPath = await getChartPath(module)

  // create the values.yml file (merge the configured parameters into the default values)
  log.debug("Preparing chart...")
  const chartValues = safeLoad(await helm(namespace, context, log, "inspect", "values", chartPath)) || {}

  // Merge with the base module's values, if applicable
  const specValues = baseModule ? jsonMerge(baseModule.spec.values, module.spec.values) : module.spec.values

  const mergedValues = jsonMerge(chartValues, specValues)

  const valuesPath = getValuesPath(chartPath)
  log.silly(`Writing chart values to ${valuesPath}`)
  await dumpYaml(valuesPath, mergedValues)

  return { fresh: true }
}
github amtrack / sfdx-browserforce-plugin / src / plugins / customer-portal / portals / index.ts View on Github external
}
            const membershipDiff = semanticallyCleanObject(
              removeNullValues(jsonMergePatch.generate(sourceMember, member))
            );
            if (membershipDiff) {
              membershipResponse.push(membershipDiff);
            }
          }
          delete sourcePortal.portalProfileMemberships;
          delete portal.portalProfileMemberships;
          if (membershipResponse.length) {
            portal['portalProfileMemberships'] = membershipResponse;
          }
        }
        const diff = semanticallyCleanObject(
          removeNullValues(jsonMergePatch.generate(sourcePortal, portal))
        );
        if (diff) {
          response.push(diff);
        }
      }
    }
    return response;
  }

json-merge-patch

Implementation of JSON Merge Patch (RFC 7396)

MIT
Latest version published 3 years ago

Package Health Score

53 / 100
Full package analysis