fix: assertion check in switch (#198)

sleep-stdin-bug
Kacper Bąk 2 years ago committed by GitHub
parent 0cccf2e063
commit 7028357eba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -11,25 +11,25 @@ import (
func PrettyPrint(v interface{}, level int, theme theme.Theme) string { func PrettyPrint(v interface{}, level int, theme theme.Theme) string {
ident := strings.Repeat(" ", level) ident := strings.Repeat(" ", level)
subident := strings.Repeat(" ", level-1) subident := strings.Repeat(" ", level-1)
switch v.(type) { switch v := v.(type) {
case nil: case nil:
return theme.Null("null") return theme.Null("null")
case bool: case bool:
if v.(bool) { if v {
return theme.Boolean("true") return theme.Boolean("true")
} else { } else {
return theme.Boolean("false") return theme.Boolean("false")
} }
case json.Number: case json.Number:
return theme.Number(v.(json.Number).String()) return theme.Number(v.String())
case string: case string:
return theme.String(fmt.Sprintf("%q", v)) return theme.String(fmt.Sprintf("%q", v))
case *dict.Dict: case *dict.Dict:
keys := v.(*dict.Dict).Keys keys := v.Keys
if len(keys) == 0 { if len(keys) == 0 {
return theme.Syntax("{}") return theme.Syntax("{}")
} }
@ -37,7 +37,7 @@ func PrettyPrint(v interface{}, level int, theme theme.Theme) string {
output += "\n" output += "\n"
for i, k := range keys { for i, k := range keys {
key := theme.Key(i, len(keys))(fmt.Sprintf("%q", k)) key := theme.Key(i, len(keys))(fmt.Sprintf("%q", k))
value, _ := v.(*dict.Dict).Get(k) value, _ := v.Get(k)
delim := theme.Syntax(": ") delim := theme.Syntax(": ")
line := ident + key + delim + PrettyPrint(value, level+1, theme) line := ident + key + delim + PrettyPrint(value, level+1, theme)
if i < len(keys)-1 { if i < len(keys)-1 {
@ -49,12 +49,12 @@ func PrettyPrint(v interface{}, level int, theme theme.Theme) string {
return output + subident + theme.Syntax("}") return output + subident + theme.Syntax("}")
case []interface{}: case []interface{}:
slice := v.([]interface{}) slice := v
if len(slice) == 0 { if len(slice) == 0 {
return theme.Syntax("[]") return theme.Syntax("[]")
} }
output := theme.Syntax("[\n") output := theme.Syntax("[\n")
for i, value := range v.([]interface{}) { for i, value := range v {
line := ident + PrettyPrint(value, level+1, theme) line := ident + PrettyPrint(value, level+1, theme)
if i < len(slice)-1 { if i < len(slice)-1 {
line += ",\n" line += ",\n"

@ -6,28 +6,28 @@ import (
) )
func Stringify(v interface{}) string { func Stringify(v interface{}) string {
switch v.(type) { switch v := v.(type) {
case nil: case nil:
return "null" return "null"
case bool: case bool:
if v.(bool) { if v {
return "true" return "true"
} else { } else {
return "false" return "false"
} }
case Number: case Number:
return v.(Number).String() return v.String()
case string: case string:
return fmt.Sprintf("%q", v) return fmt.Sprintf("%q", v)
case *Dict: case *Dict:
result := "{" result := "{"
for i, key := range v.(*Dict).Keys { for i, key := range v.Keys {
line := fmt.Sprintf("%q", key) + ": " + Stringify(v.(*Dict).Values[key]) line := fmt.Sprintf("%q", key) + ": " + Stringify(v.Values[key])
if i < len(v.(*Dict).Keys)-1 { if i < len(v.Keys)-1 {
line += "," line += ","
} }
result += line result += line
@ -36,9 +36,9 @@ func Stringify(v interface{}) string {
case Array: case Array:
result := "[" result := "["
for i, value := range v.(Array) { for i, value := range v {
line := Stringify(value) line := Stringify(value)
if i < len(v.(Array))-1 { if i < len(v)-1 {
line += "," line += ","
} }
result += line result += line

Loading…
Cancel
Save