Skip to content

Instantly share code, notes, and snippets.

@zored
Created July 19, 2021 21:25
Show Gist options
  • Save zored/f43fde2275220f565ab972eac4306f2b to your computer and use it in GitHub Desktop.
Save zored/f43fde2275220f565ab972eac4306f2b to your computer and use it in GitHub Desktop.
Output compex structs as JSON for diff (for example in Jetbrains Goland)
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"math/rand"
"path/filepath"
)
func main() {
expected, actual := "expected", "actual"
// 👇 copy and paste this code to output files and debug them.
// (note that **/rob-only-* is in my ~/.gitignoreglobal)
// TODO DEBUG DIFF REMOVE ME
(func(expected, actual interface{}) {
contents := map[string][]byte{}
id := rand.Uint32()
for name, v := range map[string]interface{}{
"expected": expected,
"actual": actual,
} {
contents[name], _ = json.MarshalIndent(v, "", " ")
}
if bytes.Compare(contents["expected"], contents["actual"]) != 0 {
paths := []string{}
for name, j := range contents {
fileName := fmt.Sprintf("rob-only-%d-%s.json", id, name)
_ = ioutil.WriteFile(fileName, j, 0777)
abs, _ := filepath.Abs(fileName)
paths = append(paths, abs)
}
panic(fmt.Sprintf("CONTENTS ARE DIFFERENT:\n%s\n%s", paths[0], paths[1])))
}
})(expected, actual)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment