diff

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2022 License: EUPL-1.2 Imports: 5 Imported by: 0

README

Kumori Manifest Diff

GO package to compare kumori manifests.

Description

This module compares two JSON documents provided in gabs format. Returns three slices:

  • Changed: leafs found in both documents but with different values.
  • Added: leafs found in the second document but not in the first one.
  • Removed: leafs found in the first document but not in the second one.

Each element in those slices is a slice of strings defining the route to that leaf in the document.

Table of contents

Usage

package main

import (
  "strings"

  "github.com/Jeffail/gabs/v2"
  diff "gitlab.com/kumori-systems/community/libraries/manifest-diff"
)

func main() {
  json1, err := gabs.ParseJSON([]byte(`{
    "outter":{
      "same":{
        "value1":1,
        "value2":"string",
        "value3":true
      },
      "changed":{
        "value1":1,
        "value2":"string",
        "value3":true
      },
      "missing1":{
        "value":1
      },
      "array":{
        "value1":20,
        "array1":[
          30, 40
        ]
      }
    }
  }`))
  if err != nil {
    panic(err)
  }
  json2, err := gabs.ParseJSON([]byte(`{
    "outter":{
      "same":{
        "value1":1,
        "value2":"string",
        "value3":true
      },
      "changed":{
        "value1":2,
        "value2":"changed",
        "value3":false
      },
      "missing2":{
        "value":1
      },
      "array":{
        "value1":20,
        "array1":[
          30, 40
        ]
      }
    }
  }`))
  if err != nil {
    panic(err)
  }

  changed, added, removed := diff.Diff(json1, json2)

  for i, elem := range changed {
    fmt.Println("Changed:", elem)
  }

  for i, elem := range added {
    fmt.Println("Added:", elem)
  }

  for i, elem := range removed {
    fmt.Println("Removed:", elem)
  }

}

License

Copyright 2022 Kumori systems S.L.

Licensed under the EUPL, Version 1.2 or – as soon they will be approved by the European Commission - subsequent versions of the EUPL (the "Licence"); You may not use this work except in compliance with the Licence. You may obtain a copy of the Licence at:

https://joinup.ec.europa.eu/software/page/eupl

Unless required by applicable law or agreed to in writing, software distributed under the Licence is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the Licence for the specific language governing permissions and limitations under the Licence.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Diff

func Diff(
	old *gabs.Container,
	new *gabs.Container,
	skip map[string]interface{},
) (changed [][]string, added [][]string, removed [][]string)

Diff calculates the differences between two JSON documents (in gabs format) An element will not be compared if contains a key and value indicated in the skip map. If the value in the skip map is "nil", the element is skipped if contains the key without evaluating the value.

func GetOrderedKeys

func GetOrderedKeys(themap *map[string]interface{}) []string

GetOrderedKeys returns an ordered array with the keys of a given map

Types

type Element

type Element struct {
	Path      []string
	Container *gabs.Container
}

Jump to

Keyboard shortcuts

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