func()

in datahub/binaryrecord.go [180:231]


func (bRecord *binaryRecord) setField(index int, data interface{}) error {
	if err := bRecord.checkFieldIndex(index); err != nil {
		return err
	}

	if data == nil {
		return nil
	}
	bRecord.setNotNullAt(index)

	offset := bRecord.getFieldOffset(index)

	if bRecord.schema != nil {
		field := bRecord.schema.Fields[index]
		switch field.Type {
		case STRING:
			str, ok := data.(String)
			if !ok {
				return fmt.Errorf("value type [%v] dismatch field type [STRING]", reflect.TypeOf(data))
			}
			if err := bRecord.writeStr(offset, []byte(str)); err != nil {
				return err
			}
		case DECIMAL:
			val, ok := data.(Decimal)
			if !ok {
				return fmt.Errorf("value type [%v] dismatch field type [DECIMAL]", reflect.TypeOf(data))
			}
			if err := bRecord.writeStr(offset, []byte(val.String())); err != nil {
				return err
			}
		case BOOLEAN, TINYINT, SMALLINT, INTEGER, BIGINT, TIMESTAMP, FLOAT, DOUBLE:
			val, err := bRecord.convertToUInt64(data)
			if err != nil {
				return err
			}
			binary.LittleEndian.PutUint64(bRecord.data[offset:], val)
		default:
			return fmt.Errorf("invalid field type [%v]", field.Type)
		}
	} else {
		buf, ok := data.([]byte)
		if !ok {
			return fmt.Errorf("only support write byte[] for no schema")
		}
		if err := bRecord.writeStr(offset, buf); err != nil {
			return err
		}
	}

	return nil
}