You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: encode.go
    path of file: ./repos/cockroach/pkg/sql/rowenc/valueside
    
    the code of the file until where you have to start completion: // Copyright 2021 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

package valueside

import (
	"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
	"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
	"github.com/cockroachdb/cockroach/pkg/util/buildutil"
	"github.com/cockroachdb/cockroach/pkg/util/encoding"
	"github.com/cockroachdb/cockroach/pkg/util/json"
	"github.com/cockroachdb/cockroach/pkg/util/tsearch"
	"github.com/cockroachdb/errors"
)

// Encode encodes `val` using value encoding and appends it to `app
    
func Encode(appendTo []byte, colID ColumnIDDelta, val tree.Datum, scratch []byte) ([]byte, error) {
	if val == tree.DNull {
		return encoding.EncodeNullValue(appendTo, uint32(colID)), nil
	}
	switch t := tree.UnwrapDOidWrapper(val).(type) {
	case *tree.DBitArray:
		return encoding.EncodeBitArrayValue(appendTo, uint32(colID), t.BitArray), nil
	case *tree.DBool:
		return encoding.EncodeBoolValue(appendTo, uint32(colID), bool(*t)), nil
	case *tree.DInt:
		return encoding.EncodeIntValue(appendTo, uint32(colID), int64(*t)), nil
	case *tree.DFloat:
		return encoding.EncodeFloatValue(appendTo, uint32(colID), float64(*t)), nil
	case *tree.DDecimal:
		return encoding.EncodeDecimalValue(appendTo, uint32(colID), &t.Decimal), nil
	case *tree.DString:
		return encoding.EncodeBytesValue(appendTo, uint32(colID), t.UnsafeBytes()), nil
	case *tree.DBytes:
		return encoding.EncodeBytesValue(appendTo, uint32(colID), t.UnsafeBytes()), nil
	case *tree.DEncodedKey:
		return encoding.EncodeBytesValue(appendTo, uint32(colID), t.UnsafeBytes()), nil
	case *tree.DDate:
		return encoding.EncodeIntValue(appendTo, uint32(colID), t.UnixEpochDaysWithOrig()), nil
	case *tree.DPGLSN:
		return encoding.EncodeIntValue(appendTo, uint32(colID), int64(t.LSN)), nil
	case *tree.DBox2D:
		return encoding.EncodeBox2DValue(appendTo, uint32(colID), t.CartesianBoundingBox.BoundingBox)
	case *tree.DGeography:
		return encoding.EncodeGeoValue(appendTo, uint32(colID), t.SpatialObjectRef())
	case *tree.DGeometry:
		return encoding.EncodeGeoValue(appendTo, uint32(colID), t.SpatialObjectRef())
	case *tree.DTime:
		return encoding.EncodeIntValue(appendTo, uint32(colID), int64(*t)), nil
	case *tree.DTimeTZ:
		return encoding.EncodeTimeTZValue(appendTo, uint32(colID), t.TimeTZ), nil
	case *tree.DTimestamp:
		return encoding.EncodeTimeValue(appendTo, uint32(colID), t.Time), nil
	case *tree.DTimestampTZ:
		return encoding.EncodeTimeValue(appendTo, uint32(colID), t.Time), nil
	case *tree.DInterval:
		return encoding.EncodeDurationValue(appendTo, uint32(colID), t.Duration), nil
	case *tree.DUuid:
		return encoding.EncodeUUIDValue(appendTo, uint32(colID), t.UUID), nil
	case *tree.DIPAddr:
		return encoding.EncodeIPAddrValue(appendTo, uint32(colID), t.IPAddr), nil
	case *tree.DJSON:
		encoded, err := json.EncodeJSON(scratch, t.JSON)
		if err != nil {
			return nil, err
		}
		return encoding.EncodeJSONValue(appendTo, uint32(colID), encoded), nil
	case *tree.DTSQuery:
		encoded, err := tsearch.EncodeTSQuery(scratch, t.TSQuery)
		if err != nil {
			return nil, err
		}
		return encoding.EncodeTSQueryValue(appendTo, uint32(colID), encoded), nil
	case *tree.DTSVector:
		encoded, err := tsearch.EncodeTSVector(scratch, t.TSVector)
		if err != nil {
			return nil, err
		}
		return encoding.EncodeTSVectorValue(appendTo, uint32(colID), encoded), nil
	case *tree.DArray:
		a, err := encodeArray(t, scratch)
		if err != nil {
			return nil, err
		}
		return encoding.EncodeArrayValue(appendTo, uint32(colID), a), nil
	case *tree.DTuple:
		return encodeTuple(t, appendTo, uint32(colID), scratch)
	case *tree.DCollatedString:
		return encoding.EncodeBytesValue(appendTo, uint32(colID), t.UnsafeContentBytes()), nil
	case *tree.DOid:
		return encoding.EncodeIntValue(appendTo, uint32(colID), int64(t.Oid)), nil
	case *tree.DEnum:
		return encoding.EncodeBytesValue(appendTo, uint32(colID), t.PhysicalRep), nil
	case *tree.DVoid:
		return encoding.EncodeVoidValue(appendTo, uint32(colID)), nil
	default:
		if buildutil.CrdbTestBuild {
			return nil, errors.AssertionFailedf("unable to encode table value: %T", t)
		}
		return nil, errors.Errorf("unable to encode table value: %T", t)
	}
}