Skip to content

Commit

Permalink
feat: integer types
Browse files Browse the repository at this point in the history
  • Loading branch information
sonirico committed Aug 13, 2022
1 parent 94f4bad commit 7c1d39b
Show file tree
Hide file tree
Showing 16 changed files with 971 additions and 298 deletions.
47 changes: 46 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ addition to have an appositive effect on performance.

## Usage

### Parser & compiler

```go
type (
Animal struct {
Expand Down Expand Up @@ -109,6 +111,46 @@ func main() {

```

### Single types

```go
func main () {
intType := parco.Int(binary.LittleEndian)
buf := bytes.NewBuffer(nil)
_ = intType.Compile(math.MaxInt, buf)
n, _ := intType.Parse(buf)
log.Println(n == math.MaxInt)
}
```

### Supported fields

| Field | Status | Size |
|---------------|--------|------------------------------|
| byte || 1 |
| int8 || 1 |
| uint8 || 1 |
| int16 || 2 |
| uint16 || 2 |
| int32 || 4 |
| uint32 || 4 |
| int64 || 8 |
| uint64 || 8 |
| float32 | 👷🚧 | 4 |
| float64 | 👷🚧 | 8 |
| int || 4/8 |
| bool || 1 |
| small varchar || dyn (up to 255) |
| varchar || dyn (up to 65535) |
| text || dyn (up to max uint32 chars) |
| long text || dyn (up to max uint64 chars) |
| string || dyn |
| bytes (blob) || dyn |
| map || - |
| slice || - |
| struct || - |
| time.Time | 👷🚧 | ? |

For fully functional examples showing the whole API, refer to [Examples](https://github.com/sonirico/parco/tree/master/examples).


Expand Down Expand Up @@ -153,7 +195,10 @@ Msgpack_Compile/large_size-12 6274 191314 ns/op

## TODO

- Support for all primitive types: boolean, nil...
- Support for all primitive types:
- Float32/64
- Optional / Pointer

- Extend interface to include version
- Static code generation
- Replace `encoding/binary` usage by faster implementations (`WriteByte`)
Expand Down
42 changes: 42 additions & 0 deletions builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,18 @@ func (b ModelBuilder[T]) UInt8(getter Getter[T, uint8], setter Setter[T, uint8])
return b
}

func (b ModelBuilder[T]) Byte(getter Getter[T, byte], setter Setter[T, byte]) ModelBuilder[T] {
b.parser.Byte(setter)
b.compiler.Byte(getter)
return b
}

func (b ModelBuilder[T]) Int8(getter Getter[T, int8], setter Setter[T, int8]) ModelBuilder[T] {
b.parser.Int8(setter)
b.compiler.Int8(getter)
return b
}

func (b ModelBuilder[T]) UInt16(order binary.ByteOrder, getter Getter[T, uint16], setter Setter[T, uint16]) ModelBuilder[T] {
b.parser.UInt16(order, setter)
b.compiler.UInt16(order, getter)
Expand All @@ -99,6 +111,36 @@ func (b ModelBuilder[T]) UInt16BE(getter Getter[T, uint16], setter Setter[T, uin
return b
}

func (b ModelBuilder[T]) Int32(order binary.ByteOrder, getter Getter[T, int32], setter Setter[T, int32]) ModelBuilder[T] {
b.parser.Int32(order, setter)
b.compiler.Int32(order, getter)
return b
}

func (b ModelBuilder[T]) UInt32(order binary.ByteOrder, getter Getter[T, uint32], setter Setter[T, uint32]) ModelBuilder[T] {
b.parser.UInt32(order, setter)
b.compiler.UInt32(order, getter)
return b
}

func (b ModelBuilder[T]) Int64(order binary.ByteOrder, getter Getter[T, int64], setter Setter[T, int64]) ModelBuilder[T] {
b.parser.Int64(order, setter)
b.compiler.Int64(order, getter)
return b
}

func (b ModelBuilder[T]) UInt64(order binary.ByteOrder, getter Getter[T, uint64], setter Setter[T, uint64]) ModelBuilder[T] {
b.parser.UInt64(order, setter)
b.compiler.UInt64(order, getter)
return b
}

func (b ModelBuilder[T]) Int(order binary.ByteOrder, getter Getter[T, int], setter Setter[T, int]) ModelBuilder[T] {
b.parser.Int(order, setter)
b.compiler.Int(order, getter)
return b
}

func (b ModelBuilder[T]) Field(f fieldBuilder[T]) ModelBuilder[T] {
b.parser.Field(f)
b.compiler.Field(f)
Expand Down
32 changes: 32 additions & 0 deletions compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,26 @@ func (c *ModelCompiler[T]) Bool(getter Getter[T, bool]) *ModelCompiler[T] {
return c.register(BoolFieldGetter[T](Bool(), getter))
}

func (c *ModelCompiler[T]) Byte(getter Getter[T, byte]) *ModelCompiler[T] {
return c.register(UInt8FieldGetter[T](Byte(), getter))
}

func (c *ModelCompiler[T]) UInt8(getter Getter[T, uint8]) *ModelCompiler[T] {
return c.register(UInt8FieldGetter[T](UInt8(), getter))
}

func (c *ModelCompiler[T]) Int8(getter Getter[T, int8]) *ModelCompiler[T] {
return c.register(Int8FieldGetter[T](Int8(), getter))
}

func (c *ModelCompiler[T]) UInt16(order binary.ByteOrder, getter Getter[T, uint16]) *ModelCompiler[T] {
return c.register(UInt16FieldGetter[T](UInt16(order), getter))
}

func (c *ModelCompiler[T]) Int16(order binary.ByteOrder, getter Getter[T, int16]) *ModelCompiler[T] {
return c.register(Int16FieldGetter[T](Int16(order), getter))
}

func (c *ModelCompiler[T]) UInt16LE(getter Getter[T, uint16]) *ModelCompiler[T] {
return c.register(UInt16FieldGetter[T](UInt16LE(), getter))
}
Expand All @@ -73,6 +85,26 @@ func (c *ModelCompiler[T]) UInt16BE(getter Getter[T, uint16]) *ModelCompiler[T]
return c.register(UInt16FieldGetter[T](UInt16BE(), getter))
}

func (c *ModelCompiler[T]) UInt32(order binary.ByteOrder, getter Getter[T, uint32]) *ModelCompiler[T] {
return c.register(UInt32FieldGetter[T](UInt32(order), getter))
}

func (c *ModelCompiler[T]) Int32(order binary.ByteOrder, getter Getter[T, int32]) *ModelCompiler[T] {
return c.register(Int32FieldGetter[T](Int32(order), getter))
}

func (c *ModelCompiler[T]) UInt64(order binary.ByteOrder, getter Getter[T, uint64]) *ModelCompiler[T] {
return c.register(UInt64FieldGetter[T](UInt64(order), getter))
}

func (c *ModelCompiler[T]) Int64(order binary.ByteOrder, getter Getter[T, int64]) *ModelCompiler[T] {
return c.register(Int64FieldGetter[T](Int64(order), getter))
}

func (c *ModelCompiler[T]) Int(order binary.ByteOrder, getter Getter[T, int]) *ModelCompiler[T] {
return c.register(IntFieldGetter[T](Int(order), getter))
}

func (c *ModelCompiler[T]) Field(f fieldCompiler[T]) *ModelCompiler[T] {
return c.register(f)
}
Expand Down
189 changes: 189 additions & 0 deletions field_fixed.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,33 @@ func UInt8FieldSetter[T any](
return UInt8Field[T](tp, nil, setter)
}

func Int8Field[T any](
tp Type[int8],
getter Getter[T, int8],
setter Setter[T, int8],
) Field[T, int8] {
return FixedField[T, int8]{
Type: tp,
Setter: setter,
Getter: getter,
Pooler: SinglePool,
}
}

func Int8FieldGetter[T any](
tp Type[int8],
getter Getter[T, int8],
) Field[T, int8] {
return Int8Field[T](tp, getter, nil)
}

func Int8FieldSetter[T any](
tp Type[int8],
setter Setter[T, int8],
) Field[T, int8] {
return Int8Field[T](tp, nil, setter)
}

func UInt16Field[T any](
tp Type[uint16],
getter Getter[T, uint16],
Expand Down Expand Up @@ -110,6 +137,168 @@ func UInt16FieldSetter[T any](
return UInt16Field[T](tp, nil, setter)
}

func Int16Field[T any](
tp Type[int16],
getter Getter[T, int16],
setter Setter[T, int16],
) Field[T, int16] {
return FixedField[T, int16]{
Type: tp,
Setter: setter,
Getter: getter,
Pooler: SinglePool,
}
}

func Int16FieldGetter[T any](
tp Type[int16],
getter Getter[T, int16],
) Field[T, int16] {
return Int16Field[T](tp, getter, nil)
}

func Int16FieldSetter[T any](
tp Type[int16],
setter Setter[T, int16],
) Field[T, int16] {
return Int16Field[T](tp, nil, setter)
}

func UInt32Field[T any](
tp Type[uint32],
getter Getter[T, uint32],
setter Setter[T, uint32],
) Field[T, uint32] {
return FixedField[T, uint32]{
Type: tp,
Setter: setter,
Getter: getter,
Pooler: SinglePool,
}
}

func UInt32FieldGetter[T any](
tp Type[uint32],
getter Getter[T, uint32],
) Field[T, uint32] {
return UInt32Field[T](tp, getter, nil)
}

func UInt32FieldSetter[T any](
tp Type[uint32],
setter Setter[T, uint32],
) Field[T, uint32] {
return UInt32Field[T](tp, nil, setter)
}

func Int32Field[T any](
tp Type[int32],
getter Getter[T, int32],
setter Setter[T, int32],
) Field[T, int32] {
return FixedField[T, int32]{
Type: tp,
Setter: setter,
Getter: getter,
Pooler: SinglePool,
}
}

func Int32FieldGetter[T any](
tp Type[int32],
getter Getter[T, int32],
) Field[T, int32] {
return Int32Field[T](tp, getter, nil)
}

func Int32FieldSetter[T any](
tp Type[int32],
setter Setter[T, int32],
) Field[T, int32] {
return Int32Field[T](tp, nil, setter)
}

func UInt64Field[T any](
tp Type[uint64],
getter Getter[T, uint64],
setter Setter[T, uint64],
) Field[T, uint64] {
return FixedField[T, uint64]{
Type: tp,
Setter: setter,
Getter: getter,
Pooler: SinglePool,
}
}

func UInt64FieldGetter[T any](
tp Type[uint64],
getter Getter[T, uint64],
) Field[T, uint64] {
return UInt64Field[T](tp, getter, nil)
}

func UInt64FieldSetter[T any](
tp Type[uint64],
setter Setter[T, uint64],
) Field[T, uint64] {
return UInt64Field[T](tp, nil, setter)
}

func Int64Field[T any](
tp Type[int64],
getter Getter[T, int64],
setter Setter[T, int64],
) Field[T, int64] {
return FixedField[T, int64]{
Type: tp,
Setter: setter,
Getter: getter,
Pooler: SinglePool,
}
}

func Int64FieldGetter[T any](
tp Type[int64],
getter Getter[T, int64],
) Field[T, int64] {
return Int64Field[T](tp, getter, nil)
}

func Int64FieldSetter[T any](
tp Type[int64],
setter Setter[T, int64],
) Field[T, int64] {
return Int64Field[T](tp, nil, setter)
}

func IntField[T any](
tp Type[int],
getter Getter[T, int],
setter Setter[T, int],
) Field[T, int] {
return FixedField[T, int]{
Type: tp,
Setter: setter,
Getter: getter,
Pooler: SinglePool,
}
}

func IntFieldGetter[T any](
tp Type[int],
getter Getter[T, int],
) Field[T, int] {
return IntField[T](tp, getter, nil)
}

func IntFieldSetter[T any](
tp Type[int],
setter Setter[T, int],
) Field[T, int] {
return IntField[T](tp, nil, setter)
}

func SkipField[T any](
tp Type[any],
) Field[T, any] {
Expand Down
Loading

0 comments on commit 7c1d39b

Please sign in to comment.