github.com/parquet-go/parquet-go@v0.20.0/encoding/notsupported.go (about)

     1  package encoding
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  
     7  	"github.com/parquet-go/parquet-go/deprecated"
     8  	"github.com/parquet-go/parquet-go/format"
     9  )
    10  
    11  var (
    12  	// ErrNotSupported is an error returned when the underlying encoding does
    13  	// not support the type of values being encoded or decoded.
    14  	//
    15  	// This error may be wrapped with type information, applications must use
    16  	// errors.Is rather than equality comparisons to test the error values
    17  	// returned by encoders and decoders.
    18  	ErrNotSupported = errors.New("encoding not supported")
    19  
    20  	// ErrInvalidArgument is an error returned one or more arguments passed to
    21  	// the encoding functions are incorrect.
    22  	//
    23  	// As with ErrNotSupported, this error may be wrapped with specific
    24  	// information about the problem and applications are expected to use
    25  	// errors.Is for comparisons.
    26  	ErrInvalidArgument = errors.New("invalid argument")
    27  )
    28  
    29  // Error constructs an error which wraps err and indicates that it originated
    30  // from the given encoding.
    31  func Error(e Encoding, err error) error {
    32  	return fmt.Errorf("%s: %w", e, err)
    33  }
    34  
    35  // Errorf is like Error but constructs the error message from the given format
    36  // and arguments.
    37  func Errorf(e Encoding, msg string, args ...interface{}) error {
    38  	return Error(e, fmt.Errorf(msg, args...))
    39  }
    40  
    41  // ErrEncodeInvalidInputSize constructs an error indicating that encoding failed
    42  // due to the size of the input.
    43  func ErrEncodeInvalidInputSize(e Encoding, typ string, size int) error {
    44  	return errInvalidInputSize(e, "encode", typ, size)
    45  }
    46  
    47  // ErrDecodeInvalidInputSize constructs an error indicating that decoding failed
    48  // due to the size of the input.
    49  func ErrDecodeInvalidInputSize(e Encoding, typ string, size int) error {
    50  	return errInvalidInputSize(e, "decode", typ, size)
    51  }
    52  
    53  func errInvalidInputSize(e Encoding, op, typ string, size int) error {
    54  	return Errorf(e, "cannot %s %s from input of size %d: %w", op, typ, size, ErrInvalidArgument)
    55  }
    56  
    57  // CanEncodeInt8 reports whether e can encode LEVELS values.
    58  func CanEncodeLevels(e Encoding) bool {
    59  	_, err := e.EncodeLevels(nil, nil)
    60  	return !errors.Is(err, ErrNotSupported)
    61  }
    62  
    63  // CanEncodeBoolean reports whether e can encode BOOLEAN values.
    64  func CanEncodeBoolean(e Encoding) bool {
    65  	_, err := e.EncodeBoolean(nil, nil)
    66  	return !errors.Is(err, ErrNotSupported)
    67  }
    68  
    69  // CanEncodeInt32 reports whether e can encode INT32 values.
    70  func CanEncodeInt32(e Encoding) bool {
    71  	_, err := e.EncodeInt32(nil, nil)
    72  	return !errors.Is(err, ErrNotSupported)
    73  }
    74  
    75  // CanEncodeInt64 reports whether e can encode INT64 values.
    76  func CanEncodeInt64(e Encoding) bool {
    77  	_, err := e.EncodeInt64(nil, nil)
    78  	return !errors.Is(err, ErrNotSupported)
    79  }
    80  
    81  // CanEncodeInt96 reports whether e can encode INT96 values.
    82  func CanEncodeInt96(e Encoding) bool {
    83  	_, err := e.EncodeInt96(nil, nil)
    84  	return !errors.Is(err, ErrNotSupported)
    85  }
    86  
    87  // CanEncodeFloat reports whether e can encode FLOAT values.
    88  func CanEncodeFloat(e Encoding) bool {
    89  	_, err := e.EncodeFloat(nil, nil)
    90  	return !errors.Is(err, ErrNotSupported)
    91  }
    92  
    93  // CanEncodeDouble reports whether e can encode DOUBLE values.
    94  func CanEncodeDouble(e Encoding) bool {
    95  	_, err := e.EncodeDouble(nil, nil)
    96  	return !errors.Is(err, ErrNotSupported)
    97  }
    98  
    99  // CanEncodeByteArray reports whether e can encode BYTE_ARRAY values.
   100  func CanEncodeByteArray(e Encoding) bool {
   101  	_, err := e.EncodeByteArray(nil, nil, zeroOffsets[:])
   102  	return !errors.Is(err, ErrNotSupported)
   103  }
   104  
   105  // CanEncodeFixedLenByteArray reports whether e can encode
   106  // FIXED_LEN_BYTE_ARRAY values.
   107  func CanEncodeFixedLenByteArray(e Encoding) bool {
   108  	_, err := e.EncodeFixedLenByteArray(nil, nil, 1)
   109  	return !errors.Is(err, ErrNotSupported)
   110  }
   111  
   112  var zeroOffsets [1]uint32
   113  
   114  // NotSupported is a type satisfying the Encoding interface which does not
   115  // support encoding nor decoding any value types.
   116  type NotSupported struct {
   117  }
   118  
   119  func (NotSupported) String() string {
   120  	return "NOT_SUPPORTED"
   121  }
   122  
   123  func (NotSupported) Encoding() format.Encoding {
   124  	return -1
   125  }
   126  
   127  func (NotSupported) EncodeLevels(dst []byte, src []uint8) ([]byte, error) {
   128  	return dst[:0], errNotSupported("LEVELS")
   129  }
   130  
   131  func (NotSupported) EncodeBoolean(dst []byte, src []byte) ([]byte, error) {
   132  	return dst[:0], errNotSupported("BOOLEAN")
   133  }
   134  
   135  func (NotSupported) EncodeInt32(dst []byte, src []int32) ([]byte, error) {
   136  	return dst[:0], errNotSupported("INT32")
   137  }
   138  
   139  func (NotSupported) EncodeInt64(dst []byte, src []int64) ([]byte, error) {
   140  	return dst[:0], errNotSupported("INT64")
   141  }
   142  
   143  func (NotSupported) EncodeInt96(dst []byte, src []deprecated.Int96) ([]byte, error) {
   144  	return dst[:0], errNotSupported("INT96")
   145  }
   146  
   147  func (NotSupported) EncodeFloat(dst []byte, src []float32) ([]byte, error) {
   148  	return dst[:0], errNotSupported("FLOAT")
   149  }
   150  
   151  func (NotSupported) EncodeDouble(dst []byte, src []float64) ([]byte, error) {
   152  	return dst[:0], errNotSupported("DOUBLE")
   153  }
   154  
   155  func (NotSupported) EncodeByteArray(dst []byte, src []byte, offsets []uint32) ([]byte, error) {
   156  	return dst[:0], errNotSupported("BYTE_ARRAY")
   157  }
   158  
   159  func (NotSupported) EncodeFixedLenByteArray(dst []byte, src []byte, size int) ([]byte, error) {
   160  	return dst[:0], errNotSupported("FIXED_LEN_BYTE_ARRAY")
   161  }
   162  
   163  func (NotSupported) DecodeLevels(dst []uint8, src []byte) ([]uint8, error) {
   164  	return dst, errNotSupported("LEVELS")
   165  }
   166  
   167  func (NotSupported) DecodeBoolean(dst []byte, src []byte) ([]byte, error) {
   168  	return dst, errNotSupported("BOOLEAN")
   169  }
   170  
   171  func (NotSupported) DecodeInt32(dst []int32, src []byte) ([]int32, error) {
   172  	return dst, errNotSupported("INT32")
   173  }
   174  
   175  func (NotSupported) DecodeInt64(dst []int64, src []byte) ([]int64, error) {
   176  	return dst, errNotSupported("INT64")
   177  }
   178  
   179  func (NotSupported) DecodeInt96(dst []deprecated.Int96, src []byte) ([]deprecated.Int96, error) {
   180  	return dst, errNotSupported("INT96")
   181  }
   182  
   183  func (NotSupported) DecodeFloat(dst []float32, src []byte) ([]float32, error) {
   184  	return dst, errNotSupported("FLOAT")
   185  }
   186  
   187  func (NotSupported) DecodeDouble(dst []float64, src []byte) ([]float64, error) {
   188  	return dst, errNotSupported("DOUBLE")
   189  }
   190  
   191  func (NotSupported) DecodeByteArray(dst []byte, src []byte, offsets []uint32) ([]byte, []uint32, error) {
   192  	return dst, offsets, errNotSupported("BYTE_ARRAY")
   193  }
   194  
   195  func (NotSupported) DecodeFixedLenByteArray(dst []byte, src []byte, size int) ([]byte, error) {
   196  	return dst, errNotSupported("FIXED_LEN_BYTE_ARRAY")
   197  }
   198  
   199  func (NotSupported) EstimateDecodeByteArraySize(src []byte) int {
   200  	return 0
   201  }
   202  
   203  func (NotSupported) CanDecodeInPlace() bool {
   204  	return false
   205  }
   206  
   207  func errNotSupported(typ string) error {
   208  	return fmt.Errorf("%w for type %s", ErrNotSupported, typ)
   209  }
   210  
   211  var (
   212  	_ Encoding = NotSupported{}
   213  )