Sha256: 924ec89d3236999867e60037516fa8054c3e38baf962eb66a95eb0ccd8278852

Contents?: true

Size: 1.88 KB

Versions: 16

Compression:

Stored size: 1.88 KB

Contents

package awsutil

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

// StringValue returns the string representation of a value.
func StringValue(i interface{}) string {
	var buf bytes.Buffer
	stringValue(reflect.ValueOf(i), 0, &buf)
	return buf.String()
}

func stringValue(v reflect.Value, indent int, buf *bytes.Buffer) {
	for v.Kind() == reflect.Ptr {
		v = v.Elem()
	}

	switch v.Kind() {
	case reflect.Struct:
		buf.WriteString("{\n")

		for i := 0; i < v.Type().NumField(); i++ {
			ft := v.Type().Field(i)
			fv := v.Field(i)

			if ft.Name[0:1] == strings.ToLower(ft.Name[0:1]) {
				continue // ignore unexported fields
			}
			if (fv.Kind() == reflect.Ptr || fv.Kind() == reflect.Slice) && fv.IsNil() {
				continue // ignore unset fields
			}

			buf.WriteString(strings.Repeat(" ", indent+2))
			buf.WriteString(ft.Name + ": ")

			if tag := ft.Tag.Get("sensitive"); tag == "true" {
				buf.WriteString("<sensitive>")
			} else {
				stringValue(fv, indent+2, buf)
			}

			buf.WriteString(",\n")
		}

		buf.WriteString("\n" + strings.Repeat(" ", indent) + "}")
	case reflect.Slice:
		nl, id, id2 := "", "", ""
		if v.Len() > 3 {
			nl, id, id2 = "\n", strings.Repeat(" ", indent), strings.Repeat(" ", indent+2)
		}
		buf.WriteString("[" + nl)
		for i := 0; i < v.Len(); i++ {
			buf.WriteString(id2)
			stringValue(v.Index(i), indent+2, buf)

			if i < v.Len()-1 {
				buf.WriteString("," + nl)
			}
		}

		buf.WriteString(nl + id + "]")
	case reflect.Map:
		buf.WriteString("{\n")

		for i, k := range v.MapKeys() {
			buf.WriteString(strings.Repeat(" ", indent+2))
			buf.WriteString(k.String() + ": ")
			stringValue(v.MapIndex(k), indent+2, buf)

			if i < v.Len()-1 {
				buf.WriteString(",\n")
			}
		}

		buf.WriteString("\n" + strings.Repeat(" ", indent) + "}")
	default:
		format := "%v"
		switch v.Interface().(type) {
		case string:
			format = "%q"
		}
		fmt.Fprintf(buf, format, v.Interface())
	}
}

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
ruby_snowflake_client-1.3.7 ext/vendor/github.com/aws/aws-sdk-go-v2/internal/awsutil/string_value.go
ruby_snowflake_client-1.3.6 ext/vendor/github.com/aws/aws-sdk-go-v2/internal/awsutil/string_value.go
ruby_snowflake_client-1.3.5 ext/vendor/github.com/aws/aws-sdk-go-v2/internal/awsutil/string_value.go
ruby_snowflake_client-1.3.4 ext/vendor/github.com/aws/aws-sdk-go-v2/internal/awsutil/string_value.go
ruby_snowflake_client-1.3.4.pre.debug ext/vendor/github.com/aws/aws-sdk-go-v2/internal/awsutil/string_value.go
ruby_snowflake_client-1.3.3.pre.debug ext/vendor/github.com/aws/aws-sdk-go-v2/internal/awsutil/string_value.go
ruby_snowflake_client-1.3.2 ext/vendor/github.com/aws/aws-sdk-go-v2/internal/awsutil/string_value.go
ruby_snowflake_client-1.3.1 ext/vendor/github.com/aws/aws-sdk-go-v2/internal/awsutil/string_value.go
ruby_snowflake_client-1.3.0 ext/vendor/github.com/aws/aws-sdk-go-v2/internal/awsutil/string_value.go
ruby_snowflake_client-1.2.1 ext/vendor/github.com/aws/aws-sdk-go-v2/internal/awsutil/string_value.go
ruby_snowflake_client-1.2.0 ext/vendor/github.com/aws/aws-sdk-go-v2/internal/awsutil/string_value.go
ruby_snowflake_client-1.1.1 ext/vendor/github.com/aws/aws-sdk-go-v2/internal/awsutil/string_value.go
ruby_snowflake_client-1.1.0 ext/vendor/github.com/aws/aws-sdk-go-v2/internal/awsutil/string_value.go
ruby_snowflake_client-1.0.2 ext/vendor/github.com/aws/aws-sdk-go-v2/internal/awsutil/string_value.go
ruby_snowflake_client-1.0.1 ext/vendor/github.com/aws/aws-sdk-go-v2/internal/awsutil/string_value.go
ruby_snowflake_client-1.0.0 ext/vendor/github.com/aws/aws-sdk-go-v2/internal/awsutil/string_value.go