func (f *Composite) SetBytes(data []byte) error {
	_, err := f.unpack(data)
	return err
}
SetBytes iterates over the receiver's subfields and unpacks them.
Data passed into this method must consist of the necessary information to
pack all subfields in full. However, unlike Unpack(), it requires the
aggregate length of the subfields not to be encoded in the prefix.
package field
import (
	"encoding/json"
	"fmt"
	"reflect"
	"github.com/moov-io/iso8583/padding"
	"github.com/moov-io/iso8583/sort"
)
var _ Field = (*Composite)(nil)
var _ json.Marshaler = (*Composite)(nil)
var _ json.Unmarshaler = (*Composite)(nil)
type Composite struct {
	spec *Spec
	orderedSpecFieldTags []string
	tagToSubfieldMap     map[string]Field
	data *reflect.Value
}
func NewComposite(spec *Spec) *Composite {
	f := &Composite{}
	f.SetSpec(spec)
	return f
}
func (f *Composite) Spec() *Spec {
	return f.spec
}
func (f *Composite) SetSpec(spec *Spec) {
	if err := validateCompositeSpec(spec); err != nil {
		panic(err)
	}
	f.spec = spec
	f.tagToSubfieldMap = map[string]Field{}
	f.orderedSpecFieldTags = orderedKeys(spec.Subfields, spec.Tag.Sort)
}
func (f *Composite) SetData(data interface{}) error {
	dataStruct := reflect.ValueOf(data)
	if dataStruct.Kind() == reflect.Ptr || dataStruct.Kind() == reflect.Interface {
		dataStruct = dataStruct.Elem()
	}
	if dataStruct.Kind() != reflect.Struct {
		return fmt.Errorf("failed to set data as struct is expected, got: %s", dataStruct.Kind())
	}
	f.data = &dataStruct
	return nil
}
func (f *Composite) Pack() ([]byte, error) {
	packed, err := f.pack()
	if err != nil {
		return nil, err
	}
	packedLength, err := f.spec.Pref.EncodeLength(f.spec.Length, len(packed))
	if err != nil {
		return nil, fmt.Errorf("failed to encode length: %w", err)
	}
	return append(packedLength, packed...), nil
}
func (f *Composite) Unpack(data []byte) (int, error) {
	dataLen, offset, err := f.spec.Pref.DecodeLength(f.spec.Length, data)
	if err != nil {
		return 0, fmt.Errorf("failed to decode length: %w", err)
	}
	read, err := f.unpack(data[offset : offset+dataLen])
	if err != nil {
		return 0, err
	}
	if dataLen != read {
		return 0, fmt.Errorf("data length: %v does not match aggregate data read from decoded subfields: %v", dataLen, read)
	}
	return offset + read, nil
}