github.com/hamba/avro@v1.8.0/codec_skip.go (about)

     1  package avro
     2  
     3  import (
     4  	"fmt"
     5  	"unsafe"
     6  )
     7  
     8  func createSkipDecoder(schema Schema) ValDecoder {
     9  	switch schema.Type() {
    10  	case Boolean:
    11  		return &boolSkipDecoder{}
    12  
    13  	case Int:
    14  		return &intSkipDecoder{}
    15  
    16  	case Long:
    17  		return &longSkipDecoder{}
    18  
    19  	case Float:
    20  		return &floatSkipDecoder{}
    21  
    22  	case Double:
    23  		return &doubleSkipDecoder{}
    24  
    25  	case String:
    26  		return &stringSkipDecoder{}
    27  
    28  	case Bytes:
    29  		return &bytesSkipDecoder{}
    30  
    31  	case Record:
    32  		return skipDecoderOfRecord(schema)
    33  
    34  	case Ref:
    35  		return createSkipDecoder(schema.(*RefSchema).Schema())
    36  
    37  	case Enum:
    38  		return &enumSkipDecoder{symbols: schema.(*EnumSchema).Symbols()}
    39  
    40  	case Array:
    41  		return skipDecoderOfArray(schema)
    42  
    43  	case Map:
    44  		return skipDecoderOfMap(schema)
    45  
    46  	case Union:
    47  		return skipDecoderOfUnion(schema)
    48  
    49  	case Fixed:
    50  		return &fixedSkipDecoder{size: schema.(*FixedSchema).Size()}
    51  
    52  	default:
    53  		return &errorDecoder{err: fmt.Errorf("avro: schema type %s is unsupported", schema.Type())}
    54  	}
    55  }
    56  
    57  type boolSkipDecoder struct{}
    58  
    59  func (*boolSkipDecoder) Decode(_ unsafe.Pointer, r *Reader) {
    60  	r.SkipBool()
    61  }
    62  
    63  type intSkipDecoder struct{}
    64  
    65  func (*intSkipDecoder) Decode(_ unsafe.Pointer, r *Reader) {
    66  	r.SkipInt()
    67  }
    68  
    69  type longSkipDecoder struct{}
    70  
    71  func (*longSkipDecoder) Decode(_ unsafe.Pointer, r *Reader) {
    72  	r.SkipLong()
    73  }
    74  
    75  type floatSkipDecoder struct{}
    76  
    77  func (*floatSkipDecoder) Decode(_ unsafe.Pointer, r *Reader) {
    78  	r.SkipFloat()
    79  }
    80  
    81  type doubleSkipDecoder struct{}
    82  
    83  func (*doubleSkipDecoder) Decode(_ unsafe.Pointer, r *Reader) {
    84  	r.SkipDouble()
    85  }
    86  
    87  type stringSkipDecoder struct{}
    88  
    89  func (*stringSkipDecoder) Decode(_ unsafe.Pointer, r *Reader) {
    90  	r.SkipString()
    91  }
    92  
    93  type bytesSkipDecoder struct{}
    94  
    95  func (c *bytesSkipDecoder) Decode(_ unsafe.Pointer, r *Reader) {
    96  	r.SkipBytes()
    97  }
    98  
    99  func skipDecoderOfRecord(schema Schema) ValDecoder {
   100  	rec := schema.(*RecordSchema)
   101  
   102  	decoders := make([]ValDecoder, len(rec.Fields()))
   103  	for i, field := range rec.Fields() {
   104  		decoders[i] = createSkipDecoder(field.Type())
   105  	}
   106  
   107  	return &recordSkipDecoder{
   108  		decoders: decoders,
   109  	}
   110  }
   111  
   112  type recordSkipDecoder struct {
   113  	decoders []ValDecoder
   114  }
   115  
   116  func (d *recordSkipDecoder) Decode(_ unsafe.Pointer, r *Reader) {
   117  	for _, decoder := range d.decoders {
   118  		decoder.Decode(nil, r)
   119  	}
   120  }
   121  
   122  type enumSkipDecoder struct {
   123  	symbols []string
   124  }
   125  
   126  func (c *enumSkipDecoder) Decode(_ unsafe.Pointer, r *Reader) {
   127  	r.SkipInt()
   128  }
   129  
   130  func skipDecoderOfArray(schema Schema) ValDecoder {
   131  	arr := schema.(*ArraySchema)
   132  	decoder := createSkipDecoder(arr.Items())
   133  
   134  	return &sliceSkipDecoder{
   135  		decoder: decoder,
   136  	}
   137  }
   138  
   139  type sliceSkipDecoder struct {
   140  	decoder ValDecoder
   141  }
   142  
   143  func (d *sliceSkipDecoder) Decode(_ unsafe.Pointer, r *Reader) {
   144  	for {
   145  		l, size := r.ReadBlockHeader()
   146  		if l == 0 {
   147  			break
   148  		}
   149  
   150  		if size > 0 {
   151  			r.SkipNBytes(int(size))
   152  			continue
   153  		}
   154  
   155  		for i := 0; i < int(l); i++ {
   156  			d.decoder.Decode(nil, r)
   157  		}
   158  	}
   159  }
   160  
   161  func skipDecoderOfMap(schema Schema) ValDecoder {
   162  	m := schema.(*MapSchema)
   163  	decoder := createSkipDecoder(m.Values())
   164  
   165  	return &mapSkipDecoder{
   166  		decoder: decoder,
   167  	}
   168  }
   169  
   170  type mapSkipDecoder struct {
   171  	decoder ValDecoder
   172  }
   173  
   174  func (d *mapSkipDecoder) Decode(_ unsafe.Pointer, r *Reader) {
   175  	for {
   176  		l, size := r.ReadBlockHeader()
   177  		if l == 0 {
   178  			break
   179  		}
   180  
   181  		if size > 0 {
   182  			r.SkipNBytes(int(size))
   183  			continue
   184  		}
   185  
   186  		for i := 0; i < int(l); i++ {
   187  			r.SkipString()
   188  			d.decoder.Decode(nil, r)
   189  		}
   190  	}
   191  }
   192  
   193  func skipDecoderOfUnion(schema Schema) ValDecoder {
   194  	union := schema.(*UnionSchema)
   195  
   196  	return &unionSkipDecoder{
   197  		schema: union,
   198  	}
   199  }
   200  
   201  type unionSkipDecoder struct {
   202  	schema *UnionSchema
   203  }
   204  
   205  func (d *unionSkipDecoder) Decode(ptr unsafe.Pointer, r *Reader) {
   206  	_, resSchema := getUnionSchema(d.schema, r)
   207  	if resSchema == nil {
   208  		return
   209  	}
   210  
   211  	// In a null case, just return
   212  	if resSchema.Type() == Null {
   213  		return
   214  	}
   215  
   216  	createSkipDecoder(resSchema).Decode(nil, r)
   217  }
   218  
   219  type fixedSkipDecoder struct {
   220  	size int
   221  }
   222  
   223  func (d *fixedSkipDecoder) Decode(_ unsafe.Pointer, r *Reader) {
   224  	r.SkipNBytes(d.size)
   225  }