Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not support String() implement function? #48

Open
rsgok opened this issue Jul 11, 2022 · 2 comments
Open

Not support String() implement function? #48

rsgok opened this issue Jul 11, 2022 · 2 comments

Comments

@rsgok
Copy link

rsgok commented Jul 11, 2022

Seems like litter not accept costumed String() implement func

Example comparing go-spew

package main

import (
	"github.com/davecgh/go-spew/spew"
	"github.com/sanity-io/litter"
)

func main() {
	TT()
}

type A struct {
	Tag string
	Num int
	InA InA
}

type InA struct {
	Id   int
	Type string
}

func (t InA) String() string {
	return t.Type
}

func TT() {
	a := A{
		Tag: "tag-A",
		Num: 1,
		InA: InA{
			Id:   100,
			Type: "test",
		},
	}
	spew.Dump(a)
	litter.Dump(a)
}

Output compare

(main.A) {
 Tag: (string) (len=5) "tag-A",
 Num: (int) 1,
 InA: (main.InA) test
}
main.A{
  Tag: "tag-A",
  Num: 1,
  InA: main.InA{
    Id: 100,
    Type: "test",
  },
}
@atombender
Copy link
Member

Litter doesn't use fmt.Stringer because Litter is meant to produce a format that corresponds to the data structure. If you want to modify the output, you can plug in a custom formatter. You should easily be able to add something that dumps fmt.Stringer types as strings instead.

@rsgok
Copy link
Author

rsgok commented Jul 11, 2022

Litter doesn't use fmt.Stringer because Litter is meant to produce a format that corresponds to the data structure. If you want to modify the output, you can plug in a custom formatter. You should easily be able to add something that dumps fmt.Stringer types as strings instead.

Have tried custom formatter, seems good.

However, when i use anonymous struct member, output is unexpected. Like I mention in davecgh/go-spew#142

Here is my demo code

package main

import (
	"io"

	"github.com/sanity-io/litter"
)

func main() {
	TT()
}

type A struct {
	Tag string
	Num int
	InA
}

type InA struct {
	Id   int
	Type string
}

func (t InA) String() string {
	return t.Type
}

func (t InA) LitterDump(w io.Writer) {
	w.Write([]byte(t.Type))
}

func TT() {
	a := A{
		Tag: "tag-A",
		Num: 1,
		InA: InA{
			Id:   100,
			Type: "test",
		},
	}
	litter.Dump(a)
}

output is

main.Atest

struct A is missing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants