unhtml icon indicating copy to clipboard operation
unhtml copied to clipboard

converter inheritance of parent struct funcs

Open iu0v1 opened this issue 5 years ago • 1 comments

Hey! :)

Is there any way to inherit parent node/struct funcs?

For example, a situation that can be expressed with such code.

type ParentNode struct {
	ListOfSomething []struct{
		Data string `html:"div > span" converter:"Trim"`
	} `html:".awesome-div"`
}

func (ParrentNode) Trim(data string) (string, error) {
	. . .
}

When trying to use Trim from ParentNode for Data I get the error "converter not exist: Trim". And the only way that I found is to take out the structures, and attach to them their personal convertors.

type ParentNode struct {
	ListOfSomething []Something `html:".awesome-div"`
}

type Something struct {
	Data string `html:"div > span" converter:"Trim"`
}

func (Something) Trim(data string) (string, error) {
	. . .
}

This isn't a big problem for small structs. But with a deep nesting this will become a big problem with a bloating and duplicated amount of code.

PS: Thank you so much for the work and time that you spent on creating this package. I don’t understand why such a convenient tool has such little fame.

iu0v1 avatar Sep 16 '19 23:09 iu0v1

Thx for your advice.

I think inheriting methods is unnecessary. On the one hand, using underlying types straightly may be unreadable for me. On the other hand, a converter may should be normal function instead of method.

Do you think using normal functions is a better choise?

Hexilee avatar Sep 17 '19 14:09 Hexilee