Skip to content

Commit

Permalink
feat: array fixed type
Browse files Browse the repository at this point in the history
  • Loading branch information
sonirico committed Aug 19, 2022
1 parent 98190a7 commit 7b4a9e2
Show file tree
Hide file tree
Showing 11 changed files with 268 additions and 26 deletions.
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type (
EvenOrOdd bool
Pet Animal
Pointer *int
Flags [5]bool
}
)

Expand Down Expand Up @@ -67,7 +68,7 @@ func main() {
parco.UInt8Header(), // up to 255 items
parco.SmallVarchar(), // each item's type
func(e *Example, friends parco.SliceView[string]) { e.Friends = friends },
func(e *Example) parco.Slice[string] { return e.Friends },
func(e *Example) parco.SliceView[string] { return e.Friends },
),
).
Bool(
Expand All @@ -88,7 +89,19 @@ func main() {
func(e *Example) *int { return e.Pointer },
),
).
ParCo()
Array(
parco.ArrayField[Example, bool](
5,
parco.Bool(),
func(e *Example, flags parco.SliceView[bool]) {
copy(e.Flags[:], flags)
},
func(e *Example) parco.SliceView[bool] {
return e.Flags[:]
},
),
).
Parco()

ex := Example{
Greet: "hey",
Expand All @@ -98,6 +111,7 @@ func main() {
EvenOrOdd: true,
Pet: Animal{Age: 3, Specie: "cat"},
Pointer: parco.Ptr(73),
Flags: [5]bool{true, false, false, true, false},
}

output := bytes.NewBuffer(nil)
Expand All @@ -117,6 +131,7 @@ func main() {
panic("not equals")
}
}

```

### Single types
Expand Down Expand Up @@ -215,6 +230,7 @@ func main() {
| bytes (blob) || dyn |
| map || - |
| slice || - |
| array (fixed) || - |
| struct || - |
| time.Time | 👷🚧 | ? |
| optional[T] (pointer) || 1 + inner size |
Expand Down
8 changes: 7 additions & 1 deletion builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (b ModelBuilder[T]) Parse(r io.Reader) (T, error) {
return b.parser.Parse(r)
}

func (b ModelBuilder[T]) ParCo() (*Parser[T], *Compiler[T]) {
func (b ModelBuilder[T]) Parco() (*Parser[T], *Compiler[T]) {
return b.parser, b.compiler
}

Expand All @@ -57,6 +57,12 @@ func (b ModelBuilder[T]) Slice(field fieldBuilder[T]) ModelBuilder[T] {
return b
}

func (b ModelBuilder[T]) Array(field fieldBuilder[T]) ModelBuilder[T] {
b.parser.Array(field)
b.compiler.Array(field)
return b
}

func (b ModelBuilder[T]) Varchar(getter Getter[T, string], setter Setter[T, string]) ModelBuilder[T] {
b.parser.Varchar(setter)
b.compiler.Varchar(getter)
Expand Down
4 changes: 4 additions & 0 deletions compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ func (c *Compiler[T]) Slice(field fieldCompiler[T]) *Compiler[T] {
return c.register(field)
}

func (c *Compiler[T]) Array(field fieldCompiler[T]) *Compiler[T] {
return c.register(field)
}

func (c *Compiler[T]) Map(field fieldCompiler[T]) *Compiler[T] {
return c.register(field)
}
Expand Down
16 changes: 15 additions & 1 deletion examples/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type (
EvenOrOdd bool
Pet Animal
Pointer *int
Flags [5]bool
}
)

Expand Down Expand Up @@ -83,7 +84,19 @@ func main() {
func(e *Example) *int { return e.Pointer },
),
).
ParCo()
Array(
parco.ArrayField[Example, bool](
5,
parco.Bool(),
func(e *Example, flags parco.SliceView[bool]) {
copy(e.Flags[:], flags)
},
func(e *Example) parco.SliceView[bool] {
return e.Flags[:]
},
),
).
Parco()

ex := Example{
Greet: "hey",
Expand All @@ -93,6 +106,7 @@ func main() {
EvenOrOdd: true,
Pet: Animal{Age: 3, Specie: "cat"},
Pointer: parco.Ptr(73),
Flags: [5]bool{true, false, false, true, false},
}

output := bytes.NewBuffer(nil)
Expand Down
11 changes: 11 additions & 0 deletions examples/compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type (
EvenOrOdd bool
Pet Animal
Pointer *int
Flags [5]bool
}
)

Expand Down Expand Up @@ -76,6 +77,15 @@ func main() {
parco.Int(binary.LittleEndian),
func(e *Example) *int { return e.Pointer },
),
).
Array(
parco.ArrayFieldGetter[Example, bool](
5,
parco.Bool(),
func(e *Example) parco.SliceView[bool] {
return e.Flags[:]
},
),
)

ex := Example{
Expand All @@ -92,6 +102,7 @@ func main() {
Specie: "cat",
},
Pointer: parco.Ptr(1),
Flags: [5]bool{true, false, false, true, false},
}

output := bytes.NewBuffer(nil)
Expand Down
90 changes: 87 additions & 3 deletions examples/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,80 @@ import (

var (
data = []byte{
3, 104, 101, 121, 42, 4, 7, 64, 98, 111, 108, 105, 114, 105, 8, 64, 100, 97, 110, 105, 114, 111, 100,
9, 64, 101, 110, 114, 105, 103, 108, 101, 115, 4, 64, 102, 51, 114, 2, 4, 109, 97, 116, 104, 5, 7, 101,
110, 103, 108, 105, 115, 104, 6, 1, 3, 99, 97, 116, 3, 1, 1, 0, 0, 0, 0, 0, 0, 0,
3,
104,
101,
121,
42,
4,
7,
64,
98,
111,
108,
105,
114,
105,
8,
64,
100,
97,
110,
105,
114,
111,
100,
9,
64,
101,
110,
114,
105,
103,
108,
101,
115,
4,
64,
102,
51,
114,
2,
4,
109,
97,
116,
104,
5,
7,
101,
110,
103,
108,
105,
115,
104,
6,
1,
3,
99,
97,
116,
3,
1,
1,
0,
0,
0,
0,
0,
0,
0,
1,
0,
0,
1,
0,
}
)

Expand All @@ -30,6 +101,7 @@ type (
EvenOrOdd bool
Pet Animal
Pointer *int
Flags [5]bool
}
)

Expand Down Expand Up @@ -84,6 +156,15 @@ func newExampleParser(factory parco.Factory[Example]) *parco.Parser[Example] {
parco.Int(binary.LittleEndian),
func(e *Example, value *int) { e.Pointer = value },
),
).
Array(
parco.ArrayFieldSetter[Example, bool](
5,
parco.Bool(),
func(e *Example, flags parco.SliceView[bool]) {
copy(e.Flags[:], flags)
},
),
)
}

Expand All @@ -105,6 +186,7 @@ func parseBytes(data []byte) {
log.Println(parsed.EvenOrOdd)
log.Println(parsed.Pet)
log.Println(parsed.Pointer, *parsed.Pointer)
log.Println(parsed.Flags)
}

func parseStream(data []byte) {
Expand All @@ -124,6 +206,7 @@ func parseStream(data []byte) {
log.Println(parsed.EvenOrOdd)
log.Println(parsed.Pet)
log.Println(parsed.Pointer, *parsed.Pointer)
log.Println(parsed.Flags)
}

func parseWithPool(data []byte) {
Expand Down Expand Up @@ -156,6 +239,7 @@ func parseWithPool(data []byte) {
log.Println(parsed.EvenOrOdd)
log.Println(parsed.Pet)
log.Println(parsed.Pointer, *parsed.Pointer)
log.Println(parsed.Flags)
// ....

// Release model
Expand Down
70 changes: 70 additions & 0 deletions field_array.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package parco

import "io"

type (
BasicArrayField[T, U any] struct {
id string
size int
inner ArrayType[U]
setter Setter[T, SliceView[U]]
getter Getter[T, SliceView[U]]
}
)

func (s BasicArrayField[T, U]) ID() string {
return s.id
}

func (s BasicArrayField[T, U]) Parse(item *T, r io.Reader) error {
values, err := s.inner.Parse(r)
if err != nil {
return err
}
s.setter(item, values.Unwrap())
return nil
}

func (s BasicArrayField[T, U]) Compile(item *T, w io.Writer) error {
value := s.getter(item)
return s.inner.Compile(value, w)
}

func ArrayField[T, U any](
length int,
inner Type[U],
setter Setter[T, SliceView[U]],
getter Getter[T, SliceView[U]],
) Field[T, U] {
return BasicArrayField[T, U]{
inner: Array[U](length, inner),
setter: setter,
getter: getter,
}
}

func ArrayFieldGetter[T any, U any](
length int,
inner Type[U],
getter Getter[T, SliceView[U]],
) Field[T, U] {
return ArrayField[T, U](
length,
inner,
nil,
getter,
)
}

func ArrayFieldSetter[T, U any](
length int,
inner Type[U],
setter Setter[T, SliceView[U]],
) Field[T, U] {
return ArrayField[T, U](
length,
inner,
setter,
nil,
)
}
4 changes: 4 additions & 0 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ func (p *Parser[T]) Slice(field fieldParser[T]) *Parser[T] {
return p.register(field)
}

func (p *Parser[T]) Array(field fieldParser[T]) *Parser[T] {
return p.register(field)
}

func (p *Parser[T]) Map(field fieldParser[T]) *Parser[T] {
return p.register(field)
}
Expand Down
Loading

0 comments on commit 7b4a9e2

Please sign in to comment.