package tdjson
import (
"encoding/base64"
"encoding/hex"
"github.com/go-faster/errors"
"github.com/go-faster/jx"
"github.com/gotd/td/bin"
)
type Decoder struct {
*jx .Decoder
}
func (b Decoder ) Obj (cb func (d Decoder , key []byte ) error ) error {
return b .Decoder .ObjBytes (func (d *jx .Decoder , key []byte ) error {
return cb (Decoder {Decoder : d }, key )
})
}
func (b Decoder ) Arr (cb func (d Decoder ) error ) error {
return b .Decoder .Arr (func (d *jx .Decoder ) error {
return cb (Decoder {Decoder : d })
})
}
func (b Decoder ) ID () (string , error ) {
return b .Decoder .Str ()
}
func (b Decoder ) FindTypeID () (string , error ) {
var (
found bool
typ string
)
if err := b .Decoder .Capture (func (d *jx .Decoder ) error {
return d .ObjBytes (func (d *jx .Decoder , key []byte ) error {
if found || string (key ) != TypeField {
return d .Skip ()
}
t , err := d .Str ()
if err != nil {
return err
}
typ = t
found = true
return nil
})
}); err != nil {
return "" , err
}
if !found {
return "" , ErrTypeIDNotFound
}
return typ , nil
}
func (b Decoder ) ConsumeID (id string ) error {
v , err := b .Decoder .Str ()
if err != nil {
return err
}
if v != id {
return NewUnexpectedID (id )
}
return nil
}
func (b Decoder ) Int () (int , error ) {
return b .Decoder .Int ()
}
func (b Decoder ) Bool () (bool , error ) {
return b .Decoder .Bool ()
}
func (b Decoder ) Uint16 () (uint16 , error ) {
v , err := b .Decoder .UInt32 ()
if err != nil {
return 0 , err
}
return uint16 (v ), nil
}
func (b Decoder ) Int32 () (int32 , error ) {
return b .Decoder .Int32 ()
}
func (b Decoder ) Uint32 () (uint32 , error ) {
return b .Decoder .UInt32 ()
}
func (b Decoder ) Int53 () (int64 , error ) {
return b .Decoder .Int64 ()
}
func (b Decoder ) Long () (int64 , error ) {
n , err := b .Decoder .Num ()
if err != nil {
return 0 , err
}
return n .Int64 ()
}
func (b Decoder ) Uint64 () (uint64 , error ) {
return b .Decoder .UInt64 ()
}
func (b Decoder ) Double () (float64 , error ) {
return b .Decoder .Float64 ()
}
func (b Decoder ) Int128 () (bin .Int128 , error ) {
v , err := b .Decoder .Str ()
if err != nil {
return bin .Int128 {}, err
}
var result bin .Int128
if l := hex .DecodedLen (len (v )); l != len (result ) {
return bin .Int128 {}, errors .Wrapf (err , "invalid length %d" , l )
}
if _ , err := hex .Decode (result [:], []byte (v )); err != nil {
return bin .Int128 {}, err
}
return result , nil
}
func (b Decoder ) Int256 () (bin .Int256 , error ) {
v , err := b .Decoder .StrBytes ()
if err != nil {
return bin .Int256 {}, err
}
var result bin .Int256
if l := hex .DecodedLen (len (v )); l != len (result ) {
return bin .Int256 {}, errors .Wrapf (err , "invalid length %d" , l )
}
if _ , err := hex .Decode (result [:], v ); err != nil {
return bin .Int256 {}, err
}
return result , nil
}
func (b Decoder ) String () (string , error ) {
return b .Decoder .Str ()
}
func (b Decoder ) Bytes () ([]byte , error ) {
enc := base64 .RawStdEncoding
v , err := b .Decoder .StrBytes ()
if err != nil {
return nil , err
}
result := make ([]byte , enc .DecodedLen (len (v )))
if _ , err := enc .Decode (result , v ); err != nil {
return nil , err
}
return result , nil
}
The pages are generated with Golds v0.6.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds .