github.com/goccy/go-reflect@v1.2.1-0.20220925055700-4646ad15ec8a/tostring_test.go (about)

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Formatting of reflection types and values for debugging.
     6  // Not defined as methods so they do not need to be linked into most binaries;
     7  // the functions are not used by the library itself, only in tests.
     8  
     9  package reflect_test
    10  
    11  import (
    12  	"strconv"
    13  
    14  	. "github.com/goccy/go-reflect"
    15  )
    16  
    17  // valueToString returns a textual representation of the reflection value val.
    18  // For debugging only.
    19  func valueToString(val Value) string {
    20  	var str string
    21  	if !val.IsValid() {
    22  		return "<zero Value>"
    23  	}
    24  	typ := val.Type()
    25  	switch val.Kind() {
    26  	case Int, Int8, Int16, Int32, Int64:
    27  		return strconv.FormatInt(val.Int(), 10)
    28  	case Uint, Uint8, Uint16, Uint32, Uint64, Uintptr:
    29  		return strconv.FormatUint(val.Uint(), 10)
    30  	case Float32, Float64:
    31  		return strconv.FormatFloat(val.Float(), 'g', -1, 64)
    32  	case Complex64, Complex128:
    33  		c := val.Complex()
    34  		return strconv.FormatFloat(real(c), 'g', -1, 64) + "+" + strconv.FormatFloat(imag(c), 'g', -1, 64) + "i"
    35  	case String:
    36  		return val.String()
    37  	case Bool:
    38  		if val.Bool() {
    39  			return "true"
    40  		} else {
    41  			return "false"
    42  		}
    43  	case Ptr:
    44  		v := val
    45  		str = typ.String() + "("
    46  		if v.IsNil() {
    47  			str += "0"
    48  		} else {
    49  			str += "&" + valueToString(v.Elem())
    50  		}
    51  		str += ")"
    52  		return str
    53  	case Array, Slice:
    54  		v := val
    55  		str += typ.String()
    56  		str += "{"
    57  		for i := 0; i < v.Len(); i++ {
    58  			if i > 0 {
    59  				str += ", "
    60  			}
    61  			str += valueToString(v.Index(i))
    62  		}
    63  		str += "}"
    64  		return str
    65  	case Map:
    66  		t := typ
    67  		str = t.String()
    68  		str += "{"
    69  		str += "<can't iterate on maps>"
    70  		str += "}"
    71  		return str
    72  	case Chan:
    73  		str = typ.String()
    74  		return str
    75  	case Struct:
    76  		t := typ
    77  		v := val
    78  		str += t.String()
    79  		str += "{"
    80  		for i, n := 0, v.NumField(); i < n; i++ {
    81  			if i > 0 {
    82  				str += ", "
    83  			}
    84  			str += valueToString(v.Field(i))
    85  		}
    86  		str += "}"
    87  		return str
    88  	case Interface:
    89  		return typ.String() + "(" + valueToString(val.Elem()) + ")"
    90  	case Func:
    91  		v := val
    92  		return typ.String() + "(" + strconv.FormatUint(uint64(v.Pointer()), 10) + ")"
    93  	default:
    94  		panic("valueToString: can't print type " + typ.String())
    95  	}
    96  }