Skip to content

Commit

Permalink
Merge branch 'release/1.11.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
n3m committed Dec 20, 2021
2 parents 5f3baf0 + 9dbcdf0 commit 8c3d8d5
Show file tree
Hide file tree
Showing 23 changed files with 689 additions and 596 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @n3m
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
language: go
go:
- master
script:
- go test -v ./...
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Pretty Print for Golang (Maps, Models and Normal Variables)

## How to install

##### Version: 1.10.3
##### Version: 1.11.0

`go get github.com/n3m/pprnt`

Expand All @@ -19,10 +19,10 @@ Pretty Print is a Golang Package designed to print Maps and Structs as if they w
```go
package main

import "github.com/DrN3MESiS/pprnt"
import "github.com/n3m/pprnt"

func main(){
example := map[string]interface{}{"get": map[string]interface{}{"some":"brother"}}
example := map[string]interface{}{"hell": map[string]interface{}{"yeah":"brother"}}

// Print(interface) :: The function Print() expects an interface to be passed as parameters
// So you can pass any type of variable
Expand All @@ -36,8 +36,8 @@ func main(){
$ go run .
{
"get": {
"some": "brother"
"hell": {
"yeah": "brother"
}
}
```
Expand Down
11 changes: 11 additions & 0 deletions deprecated_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package pprnt

import "testing"

func Test_Deprecated_Print(t *testing.T) {

Deprecated.CleanMap(map[string]interface{}{})

Deprecated.CleanArray([]interface{}{})

}
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ module github.com/n3m/pprnt

go 1.13

require github.com/mattn/go-isatty v0.0.12
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/stretchr/testify v1.7.0
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)
19 changes: 13 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
github.com/fatih/structs v1.1.0 h1:Q7juDM0QtcnhCpeyLGQKyg4TOIghuNXrkL32pHAUMxo=
github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42 h1:vEOn+mP2zCOVzKckCZy6YsCtDblrpj/w7B9nxGNELpg=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
60 changes: 60 additions & 0 deletions helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package pprnt

import (
"fmt"
"reflect"
"strings"
)

var indentation int = 2

var lvl string = strings.Repeat("\t", indentation)

func _CreateLevelString() string {
return strings.Repeat(lvl, _state.Level)
}

func _ProcessArchitecture(arg interface{}, key *string, endChar *string) string {
value := reflect.ValueOf(arg)

if arg == nil {
return _PrintNil(arg, key, endChar)
}

switch reflect.TypeOf(arg).Kind() {

case reflect.Slice, reflect.Array:
return _PrintSliceOrArray(reflect.Indirect(value).Interface(), key, endChar)
case reflect.Map:
return _PrintMap(reflect.Indirect(value).Interface(), key, endChar)
case reflect.Struct:
return _PrintStruct(reflect.Indirect(value).Interface(), key, endChar)
// case reflect.Chan:
// case reflect.Interface:
case reflect.Ptr:
newValue := value.Elem()
newValue = reflect.Indirect(newValue)
return _ProcessArchitecture(newValue.Interface(), key, endChar)
// case reflect.UnsafePointer:
// case reflect.Uintptr:
}

return _PrintPrimitive(reflect.Indirect(value).Interface(), key, endChar)
}

func _PrintPrimitive(arg interface{}, key *string, endChar *string) string {
switch reflect.TypeOf(arg).Kind() {
case reflect.String:
return _PrintString(arg, key, endChar)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return _PrintInt(arg, key, endChar)
case reflect.Bool:
return _PrintBoolean(arg, key, endChar)
case reflect.Float32, reflect.Float64:
return _PrintFloat(arg, key, endChar)
default:

fmt.Printf("[!!] %+v\n", arg)
return fmt.Sprintf("%+v\n", arg)
}
}
44 changes: 0 additions & 44 deletions helpers/main.go

This file was deleted.

22 changes: 22 additions & 0 deletions helpers_color.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package pprnt

const (
//ColorReset ...
ColorReset string = "\033[0m"
//ColorRed ...
ColorRed string = "\033[31m" //Not supported or nil
//ColorGreen ...
ColorGreen string = "\033[32m" //String
//ColorYellow ...
ColorYellow string = "\033[33m" //Keys
//ColorBlue ...
ColorBlue string = "\033[34m" //Bool
//ColorPurple ...
ColorPurple string = "\033[35m" //Int floats
//ColorCyan ...
ColorCyan string = "\033[36m" //Brackets or Corch
//ColorGray ...
ColorGray string = "\033[37m"
//ColorWhite ...
ColorWhite string = "\033[97m"
)
59 changes: 59 additions & 0 deletions helpers_primitive.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package pprnt

import (
"fmt"
)

const formatString string = "\"%+v\""
const formatInt string = "%d"
const formatBoolean string = "%t"
const formatNil string = "%s"
const formatFloat string = "%+v"

func _PrintString(elem interface{}, key *string, endChar *string) string {
return _DoPrintPrimitive(formatString, key, endChar, elem)
}

func _PrintInt(elem interface{}, key *string, endChar *string) string {
return _DoPrintPrimitive(formatInt, key, endChar, elem)
}

func _PrintBoolean(elem interface{}, key *string, endChar *string) string {
return _DoPrintPrimitive(formatBoolean, key, endChar, elem)
}

func _PrintNil(elem interface{}, key *string, endChar *string) string {
return _DoPrintPrimitive(formatNil, key, endChar, "<nil>")
}

func _PrintFloat(elem interface{}, key *string, endChar *string) string {
return _DoPrintPrimitive(formatFloat, key, endChar, elem)
}

func _DoPrintPrimitive(format string, key *string, endChar *string, elem interface{}) string {
lvlStr := _CreateLevelString()

strToPrint := ""

if key != nil {
strToPrint += lvlStr + "\"" + *key + "\"" + ": "
key = nil
} else {
strToPrint += lvlStr
}

strToPrint += fmt.Sprintf(format, elem)

if _state.Level > 0 {
strToPrint += ","
}

if endChar != nil {
strToPrint += *endChar
} else {
strToPrint += "\n"
}

fmt.Print(strToPrint)
return fmt.Sprint(strToPrint)
}
114 changes: 114 additions & 0 deletions helpers_special.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package pprnt

import (
"fmt"
"reflect"
)

func _PrintSpecialInitSetup(str, lvlStr string, key *string, initSetupChar string) (string, *string) {
if key != nil {
str += lvlStr + "\"" + *key + "\"" + ": "
key = nil
} else {
str += lvlStr
}

str += initSetupChar + "\n"
fmt.Print(str)

return str, key
}

func _PrintSpecialEndSetup(str, lvlStr string, endChar *string, endSetupChar string) string {
finalStrToPrint := lvlStr + endSetupChar
if _state.Level > 0 {
finalStrToPrint += ","
}

if endChar != nil {
finalStrToPrint += *endChar
} else {
finalStrToPrint += "\n"
}

return finalStrToPrint
}

func _PrintSliceOrArray(arg interface{}, key *string, endChar *string) string {
str := ""
lvlStr := _CreateLevelString()

// Initial Setup
str, key = _PrintSpecialInitSetup(str, lvlStr, key, "[")
_state.Level++

// Processing each element
items := reflect.ValueOf(arg)
items = reflect.Indirect(items)
for i := 0; i < items.Len(); i++ {
item := items.Index(i)
item = reflect.Indirect(item)

str += _ProcessArchitecture(item.Interface(), key, endChar)
}

// Final Setup
_state.Level--
finalStrToPrint := _PrintSpecialEndSetup(str, lvlStr, endChar, "]")
str += finalStrToPrint
fmt.Print(finalStrToPrint)
return str
}

func _PrintMap(arg interface{}, key *string, endChar *string) string {
lvlStr := _CreateLevelString()
str := ""

// Initial Setup
str, _ = _PrintSpecialInitSetup(str, lvlStr, key, "{")
_state.Level++

// Processing each element
mapObj := reflect.ValueOf(arg)
for _, key := range mapObj.MapKeys() {
value := mapObj.MapIndex(key).Interface()

str += _ProcessArchitecture(value,
GetStringAdddress(key.String()),
endChar)
}

// Final Setup
_state.Level--
finalStrToPrint := _PrintSpecialEndSetup(str, lvlStr, endChar, "}")
str += finalStrToPrint
fmt.Print(finalStrToPrint)
return str
}

func _PrintStruct(arg interface{}, key *string, endChar *string) string {

lvlStr := _CreateLevelString()
str := ""

// Initial Setup
str, _ = _PrintSpecialInitSetup(str, lvlStr, key, "{")
_state.Level++

// Processing each element
structValues := reflect.ValueOf(arg)
structValues = reflect.Indirect(structValues)

for j := 0; j < structValues.NumField(); j++ {
str += _ProcessArchitecture(structValues.Field(j).Interface(),
GetStringAdddress(structValues.Type().Field(j).Name),
endChar)
}

// Final Setup
_state.Level--
finalStrToPrint := _PrintSpecialEndSetup(str, lvlStr, endChar, "}")
str += finalStrToPrint
fmt.Print(finalStrToPrint)
return str
}
Loading

0 comments on commit 8c3d8d5

Please sign in to comment.