From 8620b17cdce700e81929226fcaa0adba0240f147 Mon Sep 17 00:00:00 2001 From: Nils Stinnesbeck Date: Thu, 23 Mar 2023 18:44:47 +0100 Subject: [PATCH] re-worked module: * only exporting Print as it can handle both slices and structs * using 'any' now instead of 'interface{}' * updated used modules * removed Year from LICENSE file * changed my Name in LICENSE file --- LICENSE | 2 +- go.mod | 8 ++++-- go.sum | 16 ++--------- main.go | 55 -------------------------------------- prettify.go | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 86 insertions(+), 72 deletions(-) delete mode 100644 main.go create mode 100644 prettify.go diff --git a/LICENSE b/LICENSE index 3e0a1fd..b65abe2 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright 2020 Nils Jakobi +Copyright Nils Stinnesbeck Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: diff --git a/go.mod b/go.mod index c4b4f64..70664de 100644 --- a/go.mod +++ b/go.mod @@ -1,9 +1,13 @@ module git.stinnesbeck.com/nils/prettify -go 1.14 +go 1.20 + +require github.com/TylerBrock/colorjson v0.0.0-20200706003622-8a50f05110d2 require ( - github.com/TylerBrock/colorjson v0.0.0-20200706003622-8a50f05110d2 github.com/fatih/color v1.15.0 // indirect github.com/hokaccha/go-prettyjson v0.0.0-20211117102719-0474bc63780f // indirect + github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-isatty v0.0.18 // indirect + golang.org/x/sys v0.6.0 // indirect ) diff --git a/go.sum b/go.sum index 6b2ceb3..cebb14c 100644 --- a/go.sum +++ b/go.sum @@ -1,26 +1,14 @@ github.com/TylerBrock/colorjson v0.0.0-20200706003622-8a50f05110d2 h1:ZBbLwSJqkHBuFDA6DUhhse0IGJ7T5bemHyNILUjvOq4= github.com/TylerBrock/colorjson v0.0.0-20200706003622-8a50f05110d2/go.mod h1:VSw57q4QFiWDbRnjdX8Cb3Ow0SFncRw+bA/ofY6Q83w= -github.com/fatih/color v1.9.0 h1:8xPHl4/q1VyqGIPif1F+1V3Y3lSmrq01EabUW3CoW5s= -github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU= github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs= github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw= -github.com/hokaccha/go-prettyjson v0.0.0-20190818114111-108c894c2c0e h1:0aewS5NTyxftZHSnFaJmWE5oCCrj4DyEXkAiMa1iZJM= -github.com/hokaccha/go-prettyjson v0.0.0-20190818114111-108c894c2c0e/go.mod h1:pFlLw2CfqZiIBOx6BuCeRLCrfxBJipTY0nIOF/VbGcI= github.com/hokaccha/go-prettyjson v0.0.0-20211117102719-0474bc63780f h1:7LYC+Yfkj3CTRcShK0KOL/w6iTiKyqqBA9a41Wnggw8= github.com/hokaccha/go-prettyjson v0.0.0-20211117102719-0474bc63780f/go.mod h1:pFlLw2CfqZiIBOx6BuCeRLCrfxBJipTY0nIOF/VbGcI= -github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA= -github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= -github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= -github.com/mattn/go-isatty v0.0.11 h1:FxPOTFNqGkuDUGi3H/qkUbQO4ZiBa2brKq5r0l8TGeM= -github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE= github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= -github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng= -github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= -golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4= -golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98= +github.com/mattn/go-isatty v0.0.18/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/main.go b/main.go deleted file mode 100644 index 3676f19..0000000 --- a/main.go +++ /dev/null @@ -1,55 +0,0 @@ -package prettify - -import ( - "encoding/json" - "fmt" - - "github.com/TylerBrock/colorjson" -) - -// Print will beautify the output of body -func Print(body interface{}) { - b, err := json.Marshal(body) - if err != nil { - panic(err) - } - var obj map[string]interface{} - json.Unmarshal(b, &obj) - - // Make a custom formatter with indent set - f := colorjson.NewFormatter() - f.Indent = 2 - - // Marshal the Colorized JSON - s, err := f.Marshal(obj) - if err != nil { - panic(err) - } - - // Print out string to stdout - fmt.Println(string(s)) -} - -// PrintArray will beautify the output of an interface array (bodyArray) -func PrintArray(bodyArray interface{}) { - b, err := json.Marshal(bodyArray) - if err != nil { - panic(err) - } - var obj []map[string]interface{} - json.Unmarshal(b, &obj) - - // Make a custom formatter with indent set - f := colorjson.NewFormatter() - f.Indent = 2 - - for i := range obj { - // Marshal the Colorized JSON - s, err := f.Marshal(obj[i]) - if err != nil { - panic(err) - } - // Print out string to stdout - fmt.Println(string(s)) - } -} diff --git a/prettify.go b/prettify.go new file mode 100644 index 0000000..1af7dee --- /dev/null +++ b/prettify.go @@ -0,0 +1,77 @@ +package prettify + +import ( + "encoding/json" + "fmt" + "reflect" + + "github.com/TylerBrock/colorjson" +) + +// Print will beautify the output of v +func Print(v any) { + typeOfVar := reflect.TypeOf(v) + switch typeOfVar.Kind() { + case reflect.Slice: + pprintArray(v) + case reflect.Struct: + pprint(v) + } +} + +// pprint will beautify the output of body +func pprint(body any) error { + b, err := json.Marshal(body) + if err != nil { + return err + } + var obj map[string]any + json.Unmarshal(b, &obj) + + // Make a custom formatter with indent set + f := colorjson.NewFormatter() + f.Indent = 2 + + // Marshal the Colorized JSON + s, err := f.Marshal(obj) + if err != nil { + return err + } + + // Print out string to stdout + fmt.Printf("%s\n", s) + return nil +} + +// pprintArray will beautify the output of bodyArray +func pprintArray(bodyArray any) error { + b, err := json.Marshal(bodyArray) + if err != nil { + return err + } + var obj []map[string]any + json.Unmarshal(b, &obj) + + // Make a custom formatter with indent set + f := colorjson.NewFormatter() + f.Indent = 2 + + for i := range obj { + // Marshal the Colorized JSON + s, err := f.Marshal(obj[i]) + if err != nil { + return err + } + + // Print out string to stdout + fmt.Printf("%s", s) + + // include a comma between elements + if i < len(obj)-1 { + fmt.Printf(",") + } + // include newline at the end of all elements + fmt.Printf("\n") + } + return nil +}