Skip to content

Commit

Permalink
org code
Browse files Browse the repository at this point in the history
  • Loading branch information
trim21 committed Jul 17, 2024
1 parent e352d20 commit e479bb0
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 94 deletions.
File renamed without changes.
16 changes: 0 additions & 16 deletions api.go → marshal.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package phpserialize

import (
"fmt"

_ "go4.org/unsafe/assume-no-moving-gc"

"github.com/trim21/go-phpserialize/internal/encoder"
)

Expand All @@ -14,18 +10,6 @@ type Marshaler interface {
MarshalPHP() ([]byte, error)
}

type Unmarshaler interface {
UnmarshalPHP([]byte) error
}

func Marshal(v any) ([]byte, error) {
return encoder.Marshal(v)
}

func Unmarshal(data []byte, v any) error {
if len(data) == 0 {
return fmt.Errorf("empty bytes")
}

return unmarshal(data, v)
}
78 changes: 0 additions & 78 deletions marshal_go118_test.go

This file was deleted.

67 changes: 67 additions & 0 deletions marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1166,3 +1166,70 @@ func TestUserMarshaler(t *testing.T) {

test.StringEqual(t, `a:1:{s:1:"T";s:25:"2024-07-16T01:02:03+08:00";}`, string(actual))
}

type Generic[T any] struct {
Value T
}

type Generic2[T any] struct {
B bool // prevent direct
Value T
}

var go118TestCase = []test.Case{
{
Name: "generic[int]",
Data: Generic[int]{1},
Expected: `a:1:{s:5:"Value";i:1;}`,
},
{
Name: "generic[struct]",
Data: Generic[test.User]{test.User{}},
Expected: `a:1:{s:5:"Value";a:2:{s:2:"id";i:0;s:4:"name";s:0:"";}}`,
},

{
Name: "generic[map]",
Data: Generic[map[string]int]{map[string]int{"one": 1}},
Expected: `a:1:{s:5:"Value";a:1:{s:3:"one";i:1;}}`,
},

{
Name: "generic[slice]",
Data: Generic[[]string]{[]string{"hello", "world"}},
Expected: `a:1:{s:5:"Value";a:2:{i:0;s:5:"hello";i:1;s:5:"world";}}`,
},

{
Name: "generic2[slice]",
Data: Generic2[[]string]{Value: []string{"hello", "world"}},
Expected: `a:2:{s:1:"B";b:0;s:5:"Value";a:2:{i:0;s:5:"hello";i:1;s:5:"world";}}`,
},
}

func TestMarshal_go118_concrete_types(t *testing.T) {
t.Parallel()
for _, data := range go118TestCase {
data := data
t.Run(data.Name, func(t *testing.T) {
actual, err := phpserialize.Marshal(data.Data)
require.NoError(t, err)

test.StringEqual(t, data.Expected, string(actual))
})
}
}

func TestMarshal_go118_interface(t *testing.T) {
t.Parallel()
for _, data := range go118TestCase {
data := data
t.Run(data.Name, func(t *testing.T) {
t.Parallel()
actual, err := phpserialize.Marshal(data)
require.NoError(t, err)

test.StringEqual(t, data.WrappedExpected(), string(actual))
})
}
}
14 changes: 14 additions & 0 deletions unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,24 @@ import (
"fmt"
"reflect"

_ "go4.org/unsafe/assume-no-moving-gc"

"github.com/trim21/go-phpserialize/internal/decoder"
"github.com/trim21/go-phpserialize/internal/errors"
)

type Unmarshaler interface {
UnmarshalPHP([]byte) error
}

func Unmarshal(data []byte, v any) error {
if len(data) == 0 {
return fmt.Errorf("empty bytes")
}

return unmarshal(data, v)
}

func unmarshal(data []byte, v any) error {
rv := reflect.ValueOf(v)

Expand Down

0 comments on commit e479bb0

Please sign in to comment.