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