json_diff

package module
v0.0.0-...-76d3ac3 Latest Latest
Warning

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

Go to latest
Published: Dec 11, 2024 License: BSD-3-Clause Imports: 7 Imported by: 0

README

JSON Diff

This is a library that displays the differences between two Golang structures in a clear and colorful way.

When we use Pulumi to manage a kubernetes cluster, we often check the differences caused by the current operation. I like this feature very much, which allows me to clearly see the deletion, modification, and addition of a resource. You can use this library to achieve something similar.

Getting Started!

go get github.com/guguducken/json-diff

Example

Code

package main

import (
	"fmt"

	jsondiff "github.com/guguducken/json-diff"
	corev1 "k8s.io/api/core/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/utils/pointer"
)

func main() {
	origin := &corev1.Secret{
		TypeMeta: metav1.TypeMeta{
			APIVersion: "v1",
			Kind:       "Secret",
		},
		Immutable: pointer.Bool(true),
		Data: map[string][]byte{
			"JSON_DIFF_ACCESS_KEY": []byte("YWFhYWFhYWFhYQ=="),
			"JSON_DIFF_SECRET_KEY": []byte("YmJiYmJiYmJiYgo="),
		},
		StringData: map[string]string{
			"JSON_DIFF_ACCESS_KEY": "WVdGaFlXRmhZV0ZoWVE9PQ==",
			"JSON_DIFF_SECRET_KEY": "WW1KaVltSmlZbUppWWdvPQ==",
		},
		Type: "Opaque",
	}

	modified := origin.DeepCopy()
	modified.Data["JSON_DIFF_NAME"] = []byte("Z2l0aHViLmNvbS9ndWd1ZHVja2VuL2pzb24tZGlmZgo=")
	*modified.Immutable = false
	modified.ObjectMeta = metav1.ObjectMeta{
		Namespace: "default",
		Name:      "json-diff",
	}
	modified.StringData = nil

	result, err := jsondiff.GenColorDiff(origin, modified)
	if err != nil {
		panic(err)
	}
	fmt.Println(result)
}

Result

 {
     "data": {
+        "JSON_DIFF_NAME": "WjJsMGFIVmlMbU52YlM5bmRXZDFaSFZqYTJWdUwycHpiMjR0WkdsbVpnbz0="
     },
~    "immutable": "true" => "false",
     "metadata": {
+        "name": "json-diff",
+        "namespace": "default"
     },
-    "stringData": {
-        "JSON_DIFF_ACCESS_KEY": "WVdGaFlXRmhZV0ZoWVE9PQ==",
-        "JSON_DIFF_SECRET_KEY": "WW1KaVltSmlZbUppWWdvPQ=="
-    }
 }

image

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DisableColor = false
)

Functions

func GenColorDiff

func GenColorDiff[T any](old, now T) (string, error)

func Green

func Green(text string) string

func Red

func Red(text string) string

func Yellow

func Yellow(text string) string

Types

type ColorCode

type ColorCode string
const (
	ResetCode  ColorCode = "\u001B[0m"
	RedCode    ColorCode = "\u001B[31m"
	GreenCode  ColorCode = "\u001B[32m"
	YellowCode ColorCode = "\u001B[38;5;136m"
)

func (ColorCode) Coloring

func (c ColorCode) Coloring(text string) string

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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