You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently, go-substrate-gen encodes variants pretty inefficiently by having an empty entry in every variant struct. This could be solved by having methods that return a struct containing each variant's information, and only storing a uint8 and the actual information of the variant in go. It would look something like the below (omitting the err != nil checks for brevity)
typeVariantXYZstruct {
indexuint8datainterface{}
}
func (tyVariantXYZ) Encode(encoder scale.Encoder) (errerror) {
err=encoder.PushByte(ty.index)
switchty.index {
case0:
err=encode.Encode(ty.data.(X))
returnerrcase1:
err=encode.Encode(ty.data.(Y))
returnerrcase2:
err=encode.Encode(ty.data.(Z))
returnerr
}
returnfmt.Errorf("Unrecognized variant")
}
func (ty*VariantXYZ) Decode(decoder scale.Decoder) (errerror) {
variant, err:=decoder.ReadOneByte()
ty.variant=variantswitchvariant {
case0:
vartmpXerr=decoder.Decode(&tmp)
ty.data=&tmpreturncase1:
vartmpYerr=decoder.Decode(&tmp)
ty.data=&tmpreturncase2:
vartmpZerr=decoder.Decode(&tmp)
ty.data=&tmpreturn
}
}
func (ty*VariantXYZ) IsX() bool {
returnty.index==0
}
func (ty*VariantXYZ) IsY() bool {
returnty.index==1
}
func (ty*VariantXYZ) IsZ() bool {
returnty.index==2
}
// Optionally these could just use go semantics and return the zero struct if it fails for easier chainingfunc (ty*VariantXYZ) X() (dX, errerror) {
if!ty.IsX() {
err=fmt.Errorf("Variant is not X")
}
returnty.data.(X), nil
}
func (ty*VariantXYZ) Y() (dY, errerror) {
if!ty.IsY() {
err=fmt.Errorf("Variant is not Y")
}
returnty.data.(Y), nil
}
func (ty*VariantXYZ) X() (dZ, errerror) {
if!ty.IsZ() {
err=fmt.Errorf("Variant is not Z")
}
returnty.data.(X), nil
}
The text was updated successfully, but these errors were encountered:
Currently,
go-substrate-gen
encodes variants pretty inefficiently by having an empty entry in every variant struct. This could be solved by having methods that return a struct containing each variant's information, and only storing auint8
and the actual information of the variant ingo
. It would look something like the below (omitting theerr != nil
checks for brevity)The text was updated successfully, but these errors were encountered: