github.com/trim21/go-phpserialize@v0.0.22-0.20240301204449-2fca0319b3f0/internal/encoder/array.go (about) 1 package encoder 2 3 import ( 4 "reflect" 5 6 "github.com/trim21/go-phpserialize/internal/runtime" 7 ) 8 9 func compileArray(rt *runtime.Type) (encoder, error) { 10 offset := rt.Elem().Size() 11 length := rt.Len() 12 i64 := int64(length) 13 var enc encoder 14 var err error 15 16 if rt.Elem().Kind() == reflect.Map { 17 enc, err = compileWithCache(runtime.PtrTo(rt.Elem())) 18 if err != nil { 19 return nil, err 20 } 21 } else { 22 enc, err = compileWithCache(rt.Elem()) 23 if err != nil { 24 return nil, err 25 } 26 } 27 28 return func(ctx *Ctx, b []byte, dataPtr uintptr) ([]byte, error) { 29 b = appendArrayBegin(b, i64) 30 var err error // create a new error value, so shadow compiler's error 31 for i := 0; i < length; i++ { 32 b = appendIntBytes(b, int64(i)) 33 b, err = enc(ctx, b, dataPtr+offset*uintptr(i)) 34 if err != nil { 35 return b, err 36 } 37 } 38 return append(b, '}'), nil 39 }, nil 40 }