jsondiffprinter

package module
v0.0.10 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 9, 2024 License: MIT Imports: 10 Imported by: 7

README

jsondiffprinter

Test Status Go Report Card
GoDoc License

The Go package github.com/breml/jsondiffprinter provides the means to pretty print semantic differences between JSON data based on JSON Patch (RFC 6902).

The package it self does not provide the necessary logic to calculate the differences between JSON documents. For this, it relys on external package and uses the JSON Patch format as interface.

Motivation

This package has been initially built for usage in tfreveal, a tool to pretty print differences in Terraform plans. Given its roots in printing differences in the way Terraform is doing it, this package contains some specialized features targeting this use case. This being said, the package is nevertheless also very useful to print differences between any JSON documents.

Usage as Library

Installation
go get github.com/breml/jsondiffprinter
Usage

Example using the package (source examples/basic/main.go):

package main

import (
	_ "embed"
	"fmt"
	"os"

	"github.com/breml/jsondiffprinter"
)

//go:embed source.json
var source []byte

//go:embed patch.json
var patch []byte

func main() {
	formatter := jsondiffprinter.NewJSONFormatter(os.Stdout)
	err := formatter.Format(source, patch)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
}

To generate the JSON patch an additional package is required. examples/diff/main.go contains a simple example. Packages, that can be used to calculate the diff between two JSON documents as JSON Patch can be found further down. Inspiration on how to integreate with them can be taken from cmd/jd/main.go.

Usage as command

Install jd

Download the latest release from the releases page.

Use jd
jd before.json after.json

Example output:

  {
    "baz": "qux",
-   "foo": "bar",
    "noz": true
  }

For the full list of supported options, run jd --help.

Supported JSON Patch Libraries

jd includes the following libraries to calculate the JSON patch:

Development

In order to reduce the number of 3rd party dependencies, the package github.com/breml/jsondiffprinter does not contain the logic to calculate the differences between two JSON documents. Instead, it relies on external packages that provide this functionality. Since the command jd and the examples do require this functionality, they live in their own modules, that is, they have their own go.mod file.

To simplify the work with the different modules, the go.work file is used, which is also contained in the repository.

Test Data

The test cases for jsondiffprinter are in the testdata directory. They use the txtar format to store the test data. The test cases are generated by the cmd/generate_testdata/main.go script. The task to update the test data is provided in the Taskfile.yml and can be executed by running task generate. This requires the task tool to be installed. Please refer to the official documentation.

Author

Copyright 2024 by Lucas Bremgartner (breml)

License

MIT License

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Format added in v0.0.10

func Format(original any, jsonpatch any, options ...Option) error

Format writes the formatted representation of the jsonpatch applied to the provided original in pretty form.

The argument original can either be of tye []byte or any of the JSON types: map[string]any, []any, bool, float64, string or nil. If an other type is passed, Format will return an error. If the type is []byte, the argument is treated as a marshaled JSON document and is unmarshaled before processing.

The argument jsonpatch can either be of type []byte representing a JSON document following the JSON Patch specification (RFC 6902) or any type, that is marshalable to a JSON document following the before mentioned specification. In the second case is the argument marshaled to JSON before being processed.

Format accepts Options to configure the format and the destination.

Types

type Comparer

type Comparer func(before, after any) ([]byte, error)

A Comparer compares two JSON documents and returns a JSON patch that transforms the first document into the second document.

type Option

type Option func(*formatter)

Option is a function that sets an option on the formatter.

func WithColor

func WithColor(enabled bool) Option

WithColor provides an option for the formatter to enable or disable the color full output.

func WithCommas

func WithCommas(commas bool) Option

WithCommas provides an option for the formatter to enable or disable the commas at the end of the JSON items.

func WithHideUnchanged

func WithHideUnchanged(hideUnchanged bool) Option

WithHideUnchanged provides an option for the formatter to enable or disable the hiding of unchanged items. If enabled, unchanged items will not be printed. But instead a summary will be printed mentioning the number of unchanged items. If disabled, all items will be printed.

func WithIndentation

func WithIndentation(indentation string) Option

WithIndentation provides an option for the formatter to set the indentation string to use when formatting the output.

func WithIndentedDiffMarkers

func WithIndentedDiffMarkers(indentedDiffMarkers bool) Option

WithIndentedDiffMarkers provides an option for the formatter to enable or disable the indentation of the diff markers. If enabled, the diff markers will be indented to match the indentation of the JSON. If disabled, the diff markers will be aligned to the left.

func WithJSONinJSONCompare

func WithJSONinJSONCompare(jsonInJSONComparer Comparer) Option

WithJSONinJSONCompare provides an option for the formatter to set the comparer to use when comparing JSON in JSON. If not set, JSON in JSON diffing is disabled.

func WithPatchSeriesPostProcess added in v0.0.6

func WithPatchSeriesPostProcess(patchSeriesPostProcess PatchSeriesPostProcessor) Option

WithPatchSeriesPostProcess provides an option for the formatter to set the post processor to use when processing the patch series.

func WithTerraformDefaults added in v0.0.10

func WithTerraformDefaults() Option

func WithWriter added in v0.0.10

func WithWriter(w io.Writer) Option

type Patch added in v0.0.7

type Patch = jsonpatch.Patch

type PatchSeriesPostProcessor added in v0.0.6

type PatchSeriesPostProcessor func(diff Patch) Patch

A PatchSeriesPostProcessor processes the JSON patch series before the diff is printed. It can be used to modify the diff before it is printed.

Directories

Path Synopsis
cmd module
examples module
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL