コード例 #1
0
ファイル: sprintf.go プロジェクト: LukeShu/periwinkle
func (s l10nStringer) L10NString(locale Spec) string {
	args := make([]interface{}, len(s.args))
	for i, arg := range s.args {
		switch v := arg.(type) {
		case Error:
			arg = error(l10nStringerError{v, locale})
		case Stringer:
			arg = fmt.Stringer(l10nStringerStringer{v, locale})
		}
		args[i] = arg
	}
	return fmt.Sprintf(s.catalog.Translate(locale, s.format), args...)
}
コード例 #2
0
ファイル: raw.go プロジェクト: upper/db
package exql

import (
	"fmt"
)

var (
	_ = fmt.Stringer(&Raw{})
)

// Raw represents a value that is meant to be used in a query without escaping.
type Raw struct {
	Value string // Value should not be modified after assigned.
	hash  hash
}

// RawValue creates and returns a new raw value.
func RawValue(v string) *Raw {
	return &Raw{Value: v}
}

// Hash returns a unique identifier for the struct.
func (r *Raw) Hash() string {
	return r.hash.Hash(r)
}

// Compile returns the raw value.
func (r *Raw) Compile(*Template) string {
	return r.Value
}
コード例 #3
0
func TestDeepRender(t *testing.T) {
	// Note that we make some of the fields exportable. This is to avoid a fun case
	// where the first reflect.Value has a read-only bit set, but follow-on values
	// do not, so recursion tests are off by one.
	type testStruct struct {
		Name string
		I    interface{}

		m string
	}

	type myStringSlice []string
	type myStringMap map[string]string
	type myIntType int
	type myStringType string

	s0 := "string0"
	s0P := &s0
	mit := myIntType(42)
	stringer := fmt.Stringer(nil)

	for _, tc := range []struct {
		a interface{}
		s string
	}{
		{nil, `nil`},
		{make(chan int), `(chan int)(PTR)`},
		{&stringer, `(*fmt.Stringer)(nil)`},
		{123, `123`},
		{"hello", `"hello"`},
		{(*testStruct)(nil), `(*assertions.testStruct)(nil)`},
		{(**testStruct)(nil), `(**assertions.testStruct)(nil)`},
		{[]***testStruct(nil), `[]***assertions.testStruct(nil)`},
		{testStruct{Name: "foo", I: &testStruct{Name: "baz"}},
			`assertions.testStruct{Name:"foo", I:(*assertions.testStruct){Name:"baz", I:interface{}(nil), m:""}, m:""}`},
		{[]byte(nil), `[]uint8(nil)`},
		{[]byte{}, `[]uint8{}`},
		{map[string]string(nil), `map[string]string(nil)`},
		{[]*testStruct{
			{Name: "foo"},
			{Name: "bar"},
		}, `[]*assertions.testStruct{(*assertions.testStruct){Name:"foo", I:interface{}(nil), m:""}, ` +
			`(*assertions.testStruct){Name:"bar", I:interface{}(nil), m:""}}`},
		{myStringSlice{"foo", "bar"}, `assertions.myStringSlice{"foo", "bar"}`},
		{myStringMap{"foo": "bar"}, `assertions.myStringMap{"foo":"bar"}`},
		{myIntType(12), `assertions.myIntType(12)`},
		{&mit, `(*assertions.myIntType)(42)`},
		{myStringType("foo"), `assertions.myStringType("foo")`},
		{struct {
			a int
			b string
		}{123, "foo"}, `struct { a int; b string }{a:123, b:"foo"}`},
		{[]string{"foo", "foo", "bar", "baz", "qux", "qux"},
			`[]string{"foo", "foo", "bar", "baz", "qux", "qux"}`},
		{[...]int{1, 2, 3}, `[3]int{1, 2, 3}`},
		{map[string]bool{
			"foo": true,
			"bar": false,
		}, `map[string]bool{"bar":false, "foo":true}`},
		{map[int]string{1: "foo", 2: "bar"}, `map[int]string{1:"foo", 2:"bar"}`},
		{uint32(1337), `1337`},
		{3.14, `3.14`},
		{complex(3, 0.14), `(3+0.14i)`},
		{&s0, `(*string)("string0")`},
		{&s0P, `(**string)("string0")`},
		{[]interface{}{nil, 1, 2, nil}, `[]interface{}{interface{}(nil), 1, 2, interface{}(nil)}`},
	} {
		pass(t, so(DeepRender(tc.a), ShouldEqual, tc.s))
	}

	// Recursive struct.
	s := &testStruct{
		Name: "recursive",
	}
	s.I = s
	pass(t, so(DeepRender(s), ShouldEqual,
		`(*assertions.testStruct){Name:"recursive", I:<REC(*assertions.testStruct)>, m:""}`))

	// Recursive array.
	a := [2]interface{}{}
	a[0] = &a
	a[1] = &a

	pass(t, so(DeepRender(&a), ShouldEqual,
		`(*[2]interface{}){<REC(*[2]interface{})>, <REC(*[2]interface{})>}`))

	// Recursive map.
	m := map[string]interface{}{}
	foo := "foo"
	m["foo"] = m
	m["bar"] = [](*string){&foo, &foo}
	v := []map[string]interface{}{m, m}

	pass(t, so(DeepRender(v), ShouldEqual,
		`[]map[string]interface{}{map[string]interface{}{`+
			`"bar":[]*string{(*string)("foo"), (*string)("foo")}, `+
			`"foo":<REC(map[string]interface{})>}, `+
			`map[string]interface{}{`+
			`"bar":[]*string{(*string)("foo"), (*string)("foo")}, `+
			`"foo":<REC(map[string]interface{})>}}`))
}
コード例 #4
0
ファイル: render_test.go プロジェクト: hgGeorg/mongo
func TestRenderList(t *testing.T) {
	t.Parallel()

	// Note that we make some of the fields exportable. This is to avoid a fun case
	// where the first reflect.Value has a read-only bit set, but follow-on values
	// do not, so recursion tests are off by one.
	type testStruct struct {
		Name string
		I    interface{}

		m string
	}

	type myStringSlice []string
	type myStringMap map[string]string
	type myIntType int
	type myStringType string

	s0 := "string0"
	s0P := &s0
	mit := myIntType(42)
	stringer := fmt.Stringer(nil)

	for i, tc := range []struct {
		a interface{}
		s string
	}{
		{nil, `nil`},
		{make(chan int), `(chan int)(PTR)`},
		{&stringer, `(*fmt.Stringer)(nil)`},
		{123, `123`},
		{"hello", `"hello"`},
		{(*testStruct)(nil), `(*render.testStruct)(nil)`},
		{(**testStruct)(nil), `(**render.testStruct)(nil)`},
		{[]***testStruct(nil), `[]***render.testStruct(nil)`},
		{testStruct{Name: "foo", I: &testStruct{Name: "baz"}},
			`render.testStruct{Name:"foo", I:(*render.testStruct){Name:"baz", I:interface{}(nil), m:""}, m:""}`},
		{[]byte(nil), `[]uint8(nil)`},
		{[]byte{}, `[]uint8{}`},
		{map[string]string(nil), `map[string]string(nil)`},
		{[]*testStruct{
			{Name: "foo"},
			{Name: "bar"},
		}, `[]*render.testStruct{(*render.testStruct){Name:"foo", I:interface{}(nil), m:""}, ` +
			`(*render.testStruct){Name:"bar", I:interface{}(nil), m:""}}`},
		{myStringSlice{"foo", "bar"}, `render.myStringSlice{"foo", "bar"}`},
		{myStringMap{"foo": "bar"}, `render.myStringMap{"foo":"bar"}`},
		{myIntType(12), `render.myIntType(12)`},
		{&mit, `(*render.myIntType)(42)`},
		{myStringType("foo"), `render.myStringType("foo")`},
		{struct {
			a int
			b string
		}{123, "foo"}, `struct { a int; b string }{a:123, b:"foo"}`},
		{[]string{"foo", "foo", "bar", "baz", "qux", "qux"},
			`[]string{"foo", "foo", "bar", "baz", "qux", "qux"}`},
		{[...]int{1, 2, 3}, `[3]int{1, 2, 3}`},
		{map[string]bool{
			"foo": true,
			"bar": false,
		}, `map[string]bool{"bar":false, "foo":true}`},
		{map[int]string{1: "foo", 2: "bar"}, `map[int]string{1:"foo", 2:"bar"}`},
		{uint32(1337), `1337`},
		{3.14, `3.14`},
		{complex(3, 0.14), `(3+0.14i)`},
		{&s0, `(*string)("string0")`},
		{&s0P, `(**string)("string0")`},
		{[]interface{}{nil, 1, 2, nil}, `[]interface{}{interface{}(nil), 1, 2, interface{}(nil)}`},
	} {
		assertRendersLike(t, fmt.Sprintf("Input #%d", i), tc.a, tc.s)
	}
}
コード例 #5
0
ファイル: stringify_logger.go プロジェクト: tgulacsi/kit
// Example:
//	Logger := log.LogfmtLogger(os.Stderr)
//	Logger = log.StringifyLogger{Logger}
type StringifyLogger struct {
	Logger
}

func (l StringifyLogger) Log(keyvals ...interface{}) error {
	for i := 1; i < len(keyvals); i += 2 {
		switch keyvals[i].(type) {
		case string, fmt.Stringer, fmt.Formatter:
		case error:
		default:
			keyvals[i] = StringWrap{Value: keyvals[i]}
		}
	}
	return l.Logger.Log(keyvals...)
}

var _ = fmt.Stringer(StringWrap{})

// StringWrap wraps the Value as a fmt.Stringer.
type StringWrap struct {
	Value interface{}
}

// String returns a string representation (%v) of the underlying Value.
func (sw StringWrap) String() string {
	return fmt.Sprintf("%v", sw.Value)
}
コード例 #6
0
ファイル: issue15528.go プロジェクト: achanda/go
	{nil, "<nil> <nil>"},
	{1, "int 1"},
	{int(1), "int 1"},
	{Int(int(2)), "main.Int Int=2"},
	{int(Int(3)), "int 3"},
	{[1]int{2}, "[1]int [2]"},
	{io.Reader(io.ReadWriter(io.ReadWriteSeeker(nil))), "<nil> <nil>"},
	{io.Reader(io.ReadWriter(io.ReadWriteSeeker(&RWS{}))), "*main.RWS rws"},
	{makeRWS(), "*main.RWS rws"},
	{map[string]string{"here": "there"}, "map[string]string map[here:there]"},
	{chan bool(nil), "chan bool <nil>"},
	{unsafe.Pointer(uintptr(0)), "unsafe.Pointer <nil>"},
	{(*byte)(nil), "*uint8 <nil>"},
	{io.Writer((*os.File)(nil)), "*os.File <nil>"},
	{(interface{})(io.Writer((*os.File)(nil))), "*os.File <nil>"},
	{fmt.Stringer(Strunger(((*Int)(nil)))), "*main.Int <nil>"},
}

type Int int

func (i Int) String() string { return fmt.Sprintf("Int=%d", i) }
func (i Int) Strung()        {}

type Strunger interface {
	fmt.Stringer
	Strung()
}

// Test correct construction of static non-empty interface values
var ifaces = [...]struct {
	x fmt.Stringer