2020-10-22 20:55:32 +02:00
|
|
|
package notation
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"sort"
|
2020-11-22 19:14:45 +01:00
|
|
|
"strings"
|
2020-10-22 20:55:32 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func withType(o opts) (opts, bool, bool) {
|
|
|
|
if o&types == 0 && o&allTypes == 0 {
|
|
|
|
return o, false, false
|
|
|
|
}
|
|
|
|
|
|
|
|
if o&skipTypes != 0 && o&allTypes == 0 {
|
|
|
|
return o &^ skipTypes, false, false
|
|
|
|
}
|
|
|
|
|
|
|
|
return o, true, o&allTypes != 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func reflectPrimitive(o opts, r reflect.Value, v interface{}, suppressType ...string) node {
|
|
|
|
s := fmt.Sprint(v)
|
|
|
|
if s[0] == '(' && s[len(s)-1] == ')' {
|
|
|
|
s = s[1 : len(s)-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
_, t, a := withType(o)
|
|
|
|
if !t {
|
|
|
|
return nodeOf(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
tn := reflectType(r.Type())
|
|
|
|
if a {
|
|
|
|
return nodeOf(tn, "(", s, ")")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, suppress := range suppressType {
|
|
|
|
if tn.parts[0] != suppress {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
return nodeOf(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nodeOf(tn, "(", s, ")")
|
|
|
|
}
|
|
|
|
|
2020-11-10 00:56:25 +01:00
|
|
|
func reflectNil(o opts, groupUnnamedType bool, r reflect.Value) node {
|
2020-10-22 20:55:32 +02:00
|
|
|
if _, _, a := withType(o); !a {
|
|
|
|
return nodeOf("nil")
|
|
|
|
}
|
|
|
|
|
2020-11-10 00:56:25 +01:00
|
|
|
rt := r.Type()
|
|
|
|
if groupUnnamedType && rt.Name() == "" {
|
|
|
|
return nodeOf("(", reflectType(rt), ")(nil)")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nodeOf(reflectType(rt), "(nil)")
|
2020-10-22 20:55:32 +02:00
|
|
|
}
|
|
|
|
|
2020-11-22 23:22:20 +01:00
|
|
|
func reflectItems(o opts, p *pending, prefix string, r reflect.Value) node {
|
2020-11-10 00:56:25 +01:00
|
|
|
typ := r.Type()
|
|
|
|
var items wrapper
|
|
|
|
if typ.Elem().Name() == "uint8" {
|
|
|
|
items = wrapper{sep: " ", mode: line}
|
|
|
|
for i := 0; i < r.Len(); i++ {
|
|
|
|
items.items = append(
|
|
|
|
items.items,
|
|
|
|
nodeOf(fmt.Sprintf("%02x", r.Index(i).Uint())),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
items = wrapper{sep: ", ", suffix: ","}
|
|
|
|
itemOpts := o | skipTypes
|
|
|
|
for i := 0; i < r.Len(); i++ {
|
|
|
|
items.items = append(
|
|
|
|
items.items,
|
2020-11-22 23:22:20 +01:00
|
|
|
reflectValue(itemOpts, p, r.Index(i)),
|
2020-11-10 00:56:25 +01:00
|
|
|
)
|
|
|
|
}
|
2020-10-22 20:55:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, t, _ := withType(o); !t {
|
|
|
|
return nodeOf(prefix, "{", items, "}")
|
|
|
|
}
|
|
|
|
|
2020-11-10 00:56:25 +01:00
|
|
|
return nodeOf(reflectType(typ), "{", items, "}")
|
2020-10-22 20:55:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func reflectHidden(o opts, hidden string, r reflect.Value) node {
|
|
|
|
if r.IsNil() {
|
2020-11-10 00:56:25 +01:00
|
|
|
return reflectNil(o, true, r)
|
2020-10-22 20:55:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, t, _ := withType(o); !t {
|
|
|
|
return nodeOf(hidden)
|
|
|
|
}
|
|
|
|
|
|
|
|
return reflectType(r.Type())
|
|
|
|
}
|
|
|
|
|
2020-11-22 23:22:20 +01:00
|
|
|
func reflectArray(o opts, p *pending, r reflect.Value) node {
|
|
|
|
return reflectItems(o, p, fmt.Sprintf("[%d]", r.Len()), r)
|
2020-10-22 20:55:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func reflectChan(o opts, r reflect.Value) node {
|
|
|
|
return reflectHidden(o, "chan", r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func reflectFunc(o opts, r reflect.Value) node {
|
|
|
|
return reflectHidden(o, "func()", r)
|
|
|
|
}
|
|
|
|
|
2020-11-22 23:22:20 +01:00
|
|
|
func reflectInterface(o opts, p *pending, r reflect.Value) node {
|
2020-10-22 20:55:32 +02:00
|
|
|
if r.IsNil() {
|
2020-11-10 00:56:25 +01:00
|
|
|
return reflectNil(o, false, r)
|
2020-10-22 20:55:32 +02:00
|
|
|
}
|
|
|
|
|
2020-11-22 23:22:20 +01:00
|
|
|
e := reflectValue(o, p, r.Elem())
|
2020-10-22 20:55:32 +02:00
|
|
|
if _, t, _ := withType(o); !t {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
return nodeOf(
|
|
|
|
reflectType(r.Type()),
|
|
|
|
"(",
|
|
|
|
wrapper{items: []node{e}},
|
|
|
|
")",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-11-22 23:22:20 +01:00
|
|
|
func reflectMap(o opts, p *pending, r reflect.Value) node {
|
2020-10-22 20:55:32 +02:00
|
|
|
if r.IsNil() {
|
2020-11-10 00:56:25 +01:00
|
|
|
return reflectNil(o, true, r)
|
2020-10-22 20:55:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
nkeys []node
|
|
|
|
skeys []string
|
|
|
|
)
|
|
|
|
|
2020-11-22 19:14:45 +01:00
|
|
|
// TODO: simplify this when no sorting is required
|
2020-10-22 20:55:32 +02:00
|
|
|
items := wrapper{sep: ", ", suffix: ","}
|
|
|
|
itemOpts := o | skipTypes
|
|
|
|
keys := r.MapKeys()
|
|
|
|
sv := make(map[string]reflect.Value)
|
|
|
|
sn := make(map[string]node)
|
|
|
|
for _, key := range keys {
|
|
|
|
var b bytes.Buffer
|
2020-11-22 23:22:20 +01:00
|
|
|
nk := reflectValue(itemOpts, p, key)
|
2020-10-22 20:55:32 +02:00
|
|
|
nkeys = append(nkeys, nk)
|
|
|
|
wr := writer{w: &b}
|
|
|
|
fprint(&wr, 0, nk)
|
|
|
|
skey := b.String()
|
|
|
|
skeys = append(skeys, skey)
|
|
|
|
sv[skey] = key
|
|
|
|
sn[skey] = nk
|
|
|
|
}
|
|
|
|
|
2020-11-10 00:56:25 +01:00
|
|
|
if o&randomMaps == 0 {
|
|
|
|
sort.Strings(skeys)
|
|
|
|
}
|
|
|
|
|
2020-10-22 20:55:32 +02:00
|
|
|
for _, skey := range skeys {
|
|
|
|
items.items = append(
|
|
|
|
items.items,
|
|
|
|
nodeOf(
|
|
|
|
sn[skey],
|
|
|
|
": ",
|
2020-11-22 23:22:20 +01:00
|
|
|
reflectValue(itemOpts, p, r.MapIndex(sv[skey])),
|
2020-10-22 20:55:32 +02:00
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, t, _ := withType(o); !t {
|
|
|
|
return nodeOf("map{", items, "}")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nodeOf(reflectType(r.Type()), "{", items, "}")
|
|
|
|
}
|
|
|
|
|
2020-11-22 23:22:20 +01:00
|
|
|
func reflectPointer(o opts, p *pending, r reflect.Value) node {
|
2020-10-22 20:55:32 +02:00
|
|
|
if r.IsNil() {
|
2020-11-10 00:56:25 +01:00
|
|
|
return reflectNil(o, true, r)
|
2020-10-22 20:55:32 +02:00
|
|
|
}
|
|
|
|
|
2020-11-22 23:22:20 +01:00
|
|
|
e := reflectValue(o, p, r.Elem())
|
2020-10-22 20:55:32 +02:00
|
|
|
if _, t, _ := withType(o); !t {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
return nodeOf("*", e)
|
|
|
|
}
|
|
|
|
|
2020-11-22 23:22:20 +01:00
|
|
|
func reflectList(o opts, p *pending, r reflect.Value) node {
|
2020-10-22 20:55:32 +02:00
|
|
|
if r.IsNil() {
|
2020-11-10 00:56:25 +01:00
|
|
|
return reflectNil(o, true, r)
|
2020-10-22 20:55:32 +02:00
|
|
|
}
|
|
|
|
|
2020-11-22 23:22:20 +01:00
|
|
|
return reflectItems(o, p, "[]", r)
|
2020-10-22 20:55:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func reflectString(o opts, r reflect.Value) node {
|
2020-11-22 19:14:45 +01:00
|
|
|
sv := r.String()
|
|
|
|
b := []byte(sv)
|
2020-10-22 20:55:32 +02:00
|
|
|
e := make([]byte, 0, len(b))
|
|
|
|
for _, c := range b {
|
|
|
|
switch c {
|
|
|
|
case '\\':
|
|
|
|
e = append(e, '\\', '\\')
|
|
|
|
case '"':
|
|
|
|
e = append(e, '\\', '"')
|
|
|
|
case '\b':
|
|
|
|
e = append(e, '\\', 'b')
|
|
|
|
case '\f':
|
|
|
|
e = append(e, '\\', 'f')
|
|
|
|
case '\n':
|
|
|
|
e = append(e, '\\', 'n')
|
|
|
|
case '\r':
|
|
|
|
e = append(e, '\\', 'r')
|
|
|
|
case '\t':
|
|
|
|
e = append(e, '\\', 't')
|
|
|
|
case '\v':
|
|
|
|
e = append(e, '\\', 'v')
|
|
|
|
default:
|
|
|
|
e = append(e, c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-22 19:14:45 +01:00
|
|
|
s := str{val: fmt.Sprintf("\"%s\"", string(e))}
|
|
|
|
if !strings.Contains(sv, "`") && strings.Contains(sv, "\n") {
|
|
|
|
s.raw = fmt.Sprintf("`%s`", sv)
|
|
|
|
}
|
|
|
|
|
|
|
|
n := nodeOf(s)
|
2020-10-22 20:55:32 +02:00
|
|
|
_, t, a := withType(o)
|
|
|
|
if !t {
|
2020-11-22 19:14:45 +01:00
|
|
|
return n
|
2020-10-22 20:55:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
tn := reflectType(r.Type())
|
|
|
|
if !a && tn.parts[0] == "string" {
|
2020-11-22 19:14:45 +01:00
|
|
|
return n
|
2020-10-22 20:55:32 +02:00
|
|
|
}
|
|
|
|
|
2020-11-22 19:14:45 +01:00
|
|
|
return nodeOf(tn, "(", wrapper{items: []node{n}}, ")")
|
2020-10-22 20:55:32 +02:00
|
|
|
}
|
|
|
|
|
2020-11-22 23:22:20 +01:00
|
|
|
func reflectStruct(o opts, p *pending, r reflect.Value) node {
|
2020-11-10 00:56:25 +01:00
|
|
|
wr := wrapper{sep: ", ", suffix: ","}
|
2020-10-22 20:55:32 +02:00
|
|
|
|
|
|
|
fieldOpts := o | skipTypes
|
|
|
|
rt := r.Type()
|
|
|
|
for i := 0; i < r.NumField(); i++ {
|
|
|
|
name := rt.Field(i).Name
|
|
|
|
wr.items = append(
|
|
|
|
wr.items,
|
|
|
|
nodeOf(
|
|
|
|
name,
|
|
|
|
": ",
|
|
|
|
reflectValue(
|
|
|
|
fieldOpts,
|
2020-11-22 23:22:20 +01:00
|
|
|
p,
|
2020-10-22 20:55:32 +02:00
|
|
|
r.FieldByName(name),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, t, _ := withType(o); !t {
|
|
|
|
return nodeOf("{", wr, "}")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nodeOf(reflectType(rt), "{", wr, "}")
|
|
|
|
}
|
|
|
|
|
|
|
|
func reflectUnsafePointer(o opts, r reflect.Value) node {
|
|
|
|
if r.IsNil() {
|
2020-11-10 00:56:25 +01:00
|
|
|
return reflectNil(o, false, r)
|
2020-10-22 20:55:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, _, a := withType(o); !a {
|
|
|
|
return nodeOf("pointer")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nodeOf(reflectType(r.Type()), "(pointer)")
|
|
|
|
}
|
|
|
|
|
2020-11-22 23:22:20 +01:00
|
|
|
func checkPending(p *pending, r reflect.Value) (applyRef func(node) node, ref node, isPending bool) {
|
|
|
|
applyRef = func(n node) node { return n }
|
|
|
|
switch r.Kind() {
|
|
|
|
case reflect.Slice, reflect.Map:
|
|
|
|
case reflect.Ptr:
|
|
|
|
if r.IsNil() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var nr nodeRef
|
|
|
|
key := valueKey{typ: r.Type(), ptr: r.Pointer()}
|
|
|
|
nr, isPending = p.values[key]
|
|
|
|
if isPending {
|
|
|
|
nr.refCount++
|
|
|
|
p.values[key] = nr
|
|
|
|
ref = nodeOf("r", nr.id)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
nr = nodeRef{id: p.idCounter}
|
|
|
|
p.idCounter++
|
|
|
|
p.values[key] = nr
|
|
|
|
applyRef = func(n node) node {
|
|
|
|
nr = p.values[key]
|
|
|
|
if nr.refCount > 0 {
|
|
|
|
n.parts = append(
|
|
|
|
[]interface{}{"r", nr.id, "="},
|
|
|
|
n.parts...,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(p.values, key)
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func reflectValue(o opts, p *pending, r reflect.Value) node {
|
|
|
|
applyRef, ref, isPending := checkPending(p, r)
|
|
|
|
if isPending {
|
|
|
|
return ref
|
|
|
|
}
|
|
|
|
|
|
|
|
var n node
|
2020-10-22 20:55:32 +02:00
|
|
|
switch r.Kind() {
|
|
|
|
case reflect.Bool:
|
2020-11-22 23:22:20 +01:00
|
|
|
n = reflectPrimitive(o, r, r.Bool(), "bool")
|
2020-10-22 20:55:32 +02:00
|
|
|
case
|
|
|
|
reflect.Int,
|
|
|
|
reflect.Int8,
|
|
|
|
reflect.Int16,
|
|
|
|
reflect.Int32,
|
|
|
|
reflect.Int64:
|
2020-11-22 23:22:20 +01:00
|
|
|
n = reflectPrimitive(o, r, r.Int(), "int")
|
2020-10-22 20:55:32 +02:00
|
|
|
case
|
|
|
|
reflect.Uint,
|
|
|
|
reflect.Uint8,
|
|
|
|
reflect.Uint16,
|
|
|
|
reflect.Uint32,
|
|
|
|
reflect.Uint64,
|
|
|
|
reflect.Uintptr:
|
2020-11-22 23:22:20 +01:00
|
|
|
n = reflectPrimitive(o, r, r.Uint())
|
2020-10-22 20:55:32 +02:00
|
|
|
case reflect.Float32, reflect.Float64:
|
2020-11-22 23:22:20 +01:00
|
|
|
n = reflectPrimitive(o, r, r.Float())
|
2020-10-22 20:55:32 +02:00
|
|
|
case reflect.Complex64, reflect.Complex128:
|
2020-11-22 23:22:20 +01:00
|
|
|
n = reflectPrimitive(o, r, r.Complex())
|
2020-10-22 20:55:32 +02:00
|
|
|
case reflect.Array:
|
2020-11-22 23:22:20 +01:00
|
|
|
n = reflectArray(o, p, r)
|
2020-10-22 20:55:32 +02:00
|
|
|
case reflect.Chan:
|
2020-11-22 23:22:20 +01:00
|
|
|
n = reflectChan(o, r)
|
2020-10-22 20:55:32 +02:00
|
|
|
case reflect.Func:
|
2020-11-22 23:22:20 +01:00
|
|
|
n = reflectFunc(o, r)
|
2020-10-22 20:55:32 +02:00
|
|
|
case reflect.Interface:
|
2020-11-22 23:22:20 +01:00
|
|
|
n = reflectInterface(o, p, r)
|
2020-10-22 20:55:32 +02:00
|
|
|
case reflect.Map:
|
2020-11-22 23:22:20 +01:00
|
|
|
n = reflectMap(o, p, r)
|
2020-10-22 20:55:32 +02:00
|
|
|
case reflect.Ptr:
|
2020-11-22 23:22:20 +01:00
|
|
|
n = reflectPointer(o, p, r)
|
2020-10-22 20:55:32 +02:00
|
|
|
case reflect.Slice:
|
2020-11-22 23:22:20 +01:00
|
|
|
n = reflectList(o, p, r)
|
2020-10-22 20:55:32 +02:00
|
|
|
case reflect.String:
|
2020-11-22 23:22:20 +01:00
|
|
|
n = reflectString(o, r)
|
2020-10-22 20:55:32 +02:00
|
|
|
case reflect.UnsafePointer:
|
2020-11-22 23:22:20 +01:00
|
|
|
n = reflectUnsafePointer(o, r)
|
2020-10-22 20:55:32 +02:00
|
|
|
default:
|
2020-11-22 23:22:20 +01:00
|
|
|
n = reflectStruct(o, p, r)
|
2020-10-22 20:55:32 +02:00
|
|
|
}
|
2020-11-22 23:22:20 +01:00
|
|
|
|
|
|
|
return applyRef(n)
|
2020-10-22 20:55:32 +02:00
|
|
|
}
|