From 7028357eba76c01713e5b4175ae42375fbd99ee8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kacper=20B=C4=85k?= <56700396+53jk1@users.noreply.github.com> Date: Thu, 28 Apr 2022 00:02:18 +0200 Subject: [PATCH] fix: assertion check in switch (#198) --- pkg/json/pretty_print.go | 14 +++++++------- pkg/json/stringify.go | 16 ++++++++-------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/pkg/json/pretty_print.go b/pkg/json/pretty_print.go index 1b088d6..bf0841a 100644 --- a/pkg/json/pretty_print.go +++ b/pkg/json/pretty_print.go @@ -11,25 +11,25 @@ import ( func PrettyPrint(v interface{}, level int, theme theme.Theme) string { ident := strings.Repeat(" ", level) subident := strings.Repeat(" ", level-1) - switch v.(type) { + switch v := v.(type) { case nil: return theme.Null("null") case bool: - if v.(bool) { + if v { return theme.Boolean("true") } else { return theme.Boolean("false") } case json.Number: - return theme.Number(v.(json.Number).String()) + return theme.Number(v.String()) case string: return theme.String(fmt.Sprintf("%q", v)) case *dict.Dict: - keys := v.(*dict.Dict).Keys + keys := v.Keys if len(keys) == 0 { return theme.Syntax("{}") } @@ -37,7 +37,7 @@ func PrettyPrint(v interface{}, level int, theme theme.Theme) string { output += "\n" for i, k := range keys { key := theme.Key(i, len(keys))(fmt.Sprintf("%q", k)) - value, _ := v.(*dict.Dict).Get(k) + value, _ := v.Get(k) delim := theme.Syntax(": ") line := ident + key + delim + PrettyPrint(value, level+1, theme) if i < len(keys)-1 { @@ -49,12 +49,12 @@ func PrettyPrint(v interface{}, level int, theme theme.Theme) string { return output + subident + theme.Syntax("}") case []interface{}: - slice := v.([]interface{}) + slice := v if len(slice) == 0 { return theme.Syntax("[]") } output := theme.Syntax("[\n") - for i, value := range v.([]interface{}) { + for i, value := range v { line := ident + PrettyPrint(value, level+1, theme) if i < len(slice)-1 { line += ",\n" diff --git a/pkg/json/stringify.go b/pkg/json/stringify.go index 739cc71..1b6b51b 100644 --- a/pkg/json/stringify.go +++ b/pkg/json/stringify.go @@ -6,28 +6,28 @@ import ( ) func Stringify(v interface{}) string { - switch v.(type) { + switch v := v.(type) { case nil: return "null" case bool: - if v.(bool) { + if v { return "true" } else { return "false" } case Number: - return v.(Number).String() + return v.String() case string: return fmt.Sprintf("%q", v) case *Dict: result := "{" - for i, key := range v.(*Dict).Keys { - line := fmt.Sprintf("%q", key) + ": " + Stringify(v.(*Dict).Values[key]) - if i < len(v.(*Dict).Keys)-1 { + for i, key := range v.Keys { + line := fmt.Sprintf("%q", key) + ": " + Stringify(v.Values[key]) + if i < len(v.Keys)-1 { line += "," } result += line @@ -36,9 +36,9 @@ func Stringify(v interface{}) string { case Array: result := "[" - for i, value := range v.(Array) { + for i, value := range v { line := Stringify(value) - if i < len(v.(Array))-1 { + if i < len(v)-1 { line += "," } result += line