lundi 12 juin 2017

Golang: How to make a Multidimensional Array or Slice using Reflection

I am currently working on a project to create a binding in Go for another language that utilizes multidimensional arrays. I'm trying to find a dynamic way to arbitarily create a slice of slices or array of arrays with potentially multiple dimensions. I am currently looking through the reflect documentation but nothing is jumping out at me that seems to be an intuitive method for doing what I need to do.

For context, I can get the type information and parse it into this struct:

const (
    IntTy byte = iota
    UintTy
    BoolTy
    StringTy
    AddressTy
    HashTy
    FixedPointTy
    FunctionTy
    FixedBytesTy
    StaticArrayTy
    DynamicArrayTy
    MultiDimensionalArrayTy
    StructTy
    BytesTy
)

// Type is the reflection of the supported argument type
type Type struct {
    // Slice descriptions
    IsStatic           bool // Determines if its a static array
    IsDynamic          bool // Determines if its a dynamic array
    IsMultiDimensional bool // Determine if there is more than dimension to the array
    SliceSize          int  // Size of the slice if it's static
    Dimensions         int  // Number of dimensions if type is a multidimensional array

    // If applicable (struct, slice), the underlying type
    Elem *Type

    Kind           reflect.Kind // corresponding go Kind.
    Type           reflect.Type // corresponding go Type.
    Size           int          // type size (denotes uint256, uint248, etc.)
    T              byte         // Our own type checking
    RequiresOffset bool         // denotes whether the type needs an offset

    stringKind string // holds the unparsed string for deriving signatures
}

Note that there are fields made to help describe the slice and the underlying type (we want to be as accurate as possible). I have a type byte as well as elements denoting the number of elements and dimensions for the slice. But what would be an intuitive way to create a golang multidimensional slice type from these description? One algorithm showcasing this for say a [][]uint or a [][][]uint would be greatly appreciated.





Aucun commentaire:

Enregistrer un commentaire