xssnick / tonutils-go

TON SDK Library in pure Golang for interacting with The Open Network ecosystem using native protocols, such as ADNL, RLDP and etc.
Apache License 2.0
485 stars 97 forks source link

How to use `tlb` struct tag to load string snake? #220

Closed mritd closed 1 month ago

mritd commented 1 month ago

I've looked at the docs in the loader.go, but still haven't figured out how to load the string snake; for now I'm stuck with the *cell.Cell:

var comment struct {
    _     tlb.Magic  `tlb:"#00000000"`
    Value *cell.Cell `tlb:"."`
}
_ = tlb.LoadFromCell(&comment, msgs[1].AsInternal().Body.BeginParse(), false)

// load string snake
cStr, err := comment.Value.BeginParse().LoadStringSnake()

I'm wondering if there are other ways to directly deserialize structs, such as tlb: "snake"?

xssnick commented 1 month ago

Hi, at this moment no, snake has no special tag, because it is pretty rare.

For your case it will be better to implement custom LoadFromCell method for comment type, and it will be called automatically instead of parsing tags.

Example https://github.com/xssnick/tonutils-go/blob/master/tlb/account.go#L128-L163

Then you can have just something like

type Comment struct {
    Text string
}

with custom LoadFromCell method (and ToCell if you want serialization also)