Skip to content

Commit

Permalink
refactor: remove unused fields
Browse files Browse the repository at this point in the history
  • Loading branch information
trim21 committed Oct 10, 2024
1 parent 0b6cc4a commit 4d445eb
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 54 deletions.
6 changes: 3 additions & 3 deletions internal/decoder/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,9 +301,9 @@ func compileStruct(rt reflect.Type, structName, fieldName string, structTypeToDe
}

fieldSet := &structFieldSet{
dec: dec,
offset: field.Offset,
key: key,
dec: dec,
fieldIdx: i,
key: key,
}

allFields = append(allFields, fieldSet)
Expand Down
1 change: 0 additions & 1 deletion internal/decoder/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

type structFieldSet struct {
dec Decoder
offset uintptr
fieldIdx int
key string
err error
Expand Down
1 change: 0 additions & 1 deletion internal/encoder/comple_reflect.go

This file was deleted.

36 changes: 1 addition & 35 deletions internal/encoder/uint_at_string.go
Original file line number Diff line number Diff line change
@@ -1,46 +1,12 @@
package encoder

import (
"fmt"
"reflect"
"strconv"
)

func compileUintAsString(rt reflect.Type) (encoder, error) {
switch rt.Kind() {
case reflect.Uint8:
return encodeUint8AsString, nil
case reflect.Uint16:
return encodeUint16AsString, nil
case reflect.Uint32:
return encodeUint32AsString, nil
case reflect.Uint64:
return encodeUint64AsString, nil
case reflect.Uint:
return encodeUintAsString, nil
}

panic(fmt.Sprintf("unexpected kind %s", rt.Kind()))
}

func encodeUint8AsString(buf *Ctx, b []byte, rv reflect.Value) ([]byte, error) {
return appendUintAsString(b, rv.Uint())

}

func encodeUint16AsString(buf *Ctx, b []byte, rv reflect.Value) ([]byte, error) {
return appendUintAsString(b, rv.Uint())

}

func encodeUint32AsString(buf *Ctx, b []byte, rv reflect.Value) ([]byte, error) {
return appendUintAsString(b, rv.Uint())

}

func encodeUint64AsString(buf *Ctx, b []byte, rv reflect.Value) ([]byte, error) {
return appendUintAsString(b, rv.Uint())

return encodeUintAsString, nil
}

func encodeUintAsString(buf *Ctx, b []byte, rv reflect.Value) ([]byte, error) {
Expand Down
31 changes: 17 additions & 14 deletions internal/test/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package test

import (
"bytes"
"strconv"
"testing"

"github.com/fatih/color"
Expand All @@ -13,22 +14,17 @@ func init() {
}

func diff(a, b string) []diffmatchpatch.Diff {
dmp := diffmatchpatch.New()
diffs := dmp.DiffMain(a, b, true)
if len(diffs) > 2 {
diffs = dmp.DiffCleanupSemantic(diffs)
diffs = dmp.DiffCleanupEfficiency(diffs)
}
return diffs
return diffmatchpatch.New().DiffMain(a, b, false)
}

// CharacterDiff returns an inline diff between the two strings, using (++added++) and (~~deleted~~) markup.
func CharacterDiff(a, b string) string {
// characterDiff returns an inline diff between the two strings, using (++added++) and (~~deleted~~) markup.
func characterDiff(a, b string) string {
return diffsToString(diff(a, b))
}

func diffsToString(diffs []diffmatchpatch.Diff) string {
var buff bytes.Buffer
buff.WriteString("expected : ")
for _, diff := range diffs {
text := diff.Text
switch diff.Type {
Expand All @@ -39,7 +35,7 @@ func diffsToString(diffs []diffmatchpatch.Diff) string {
}
}

buff.WriteString("\n")
buff.WriteString("\nactual : ")

for _, diff := range diffs {
text := diff.Text
Expand All @@ -54,10 +50,17 @@ func diffsToString(diffs []diffmatchpatch.Diff) string {
return buff.String()
}

func StringEqual(t *testing.T, expected, actual string) {
type stringLike interface {
~string | ~[]byte
}

func StringEqual[A stringLike, B stringLike](t *testing.T, expected A, actual B) {
t.Helper()
if actual != expected {
t.Errorf("Result not as expected:\n%v", CharacterDiff(expected, actual))
t.FailNow()

if string(expected) == string(actual) {
return
}

t.Errorf("Result not as expected:\n%v", characterDiff(strconv.QuoteToASCII(string(expected)), strconv.QuoteToASCII(string(actual))))
t.FailNow()
}
13 changes: 13 additions & 0 deletions unmarshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -645,3 +645,16 @@ func TestUnmarshal_array_with_bool_to_map(t *testing.T) {
}
require.Equal(t, expected, actual)
}

type Tag struct {
Name *string `php:"tag_name"`
Count int `php:"result,string"`
TotalCount int `php:"tag_results,string"`
}

func TestUnmarshal_error_case(t *testing.T) {
raw := `a:30:{i:0;a:2:{s:8:"tag_name";s:18:"叛逆的鲁鲁修";s:6:"result";s:4:"1645";}i:1;a:2:{s:8:"tag_name";s:7:"SUNRISE";s:6:"result";s:4:"1229";}i:2;a:2:{s:8:"tag_name";s:24:"反逆のルルーシュ";s:6:"result";s:3:"936";}i:3;a:2:{s:8:"tag_name";s:15:"还是死妹控";s:6:"result";s:3:"721";}i:4;a:2:{s:8:"tag_name";s:2:"TV";s:6:"result";s:3:"664";}i:5;a:2:{s:8:"tag_name";s:6:"妹控";s:6:"result";s:3:"603";}i:6;a:2:{s:8:"tag_name";s:9:"codegeass";s:6:"result";s:3:"569";}i:7;a:2:{s:8:"tag_name";s:12:"谷口悟朗";s:6:"result";s:3:"523";}i:8;a:2:{s:8:"tag_name";s:9:"鲁路修";s:6:"result";s:3:"453";}i:9;a:2:{s:8:"tag_name";s:2:"R2";s:6:"result";s:3:"427";}i:10;a:2:{s:8:"tag_name";s:4:"2008";s:6:"result";s:3:"409";}i:11;a:2:{s:8:"tag_name";s:6:"原创";s:6:"result";s:3:"385";}i:12;a:2:{s:8:"tag_name";s:11:"2008年4月";s:6:"result";s:3:"357";}i:13;a:2:{s:8:"tag_name";s:15:"大河内一楼";s:6:"result";s:3:"174";}i:14;a:2:{s:8:"tag_name";s:6:"日升";s:6:"result";s:3:"151";}i:15;a:2:{s:8:"tag_name";s:6:"萝卜";s:6:"result";s:3:"120";}i:16;a:2:{s:8:"tag_name";s:6:"机战";s:6:"result";s:3:"111";}i:17;a:2:{s:8:"tag_name";s:15:"狗得鸡鸭死";s:6:"result";s:3:"104";}i:18;a:2:{s:8:"tag_name";s:9:"福山润";s:6:"result";s:2:"94";}i:19;a:2:{s:8:"tag_name";s:9:"露露胸";s:6:"result";s:2:"84";}i:20;a:2:{s:8:"tag_name";s:5:"CLAMP";s:6:"result";s:2:"69";}i:21;a:2:{s:8:"tag_name";s:6:"科幻";s:6:"result";s:2:"67";}i:22;a:2:{s:8:"tag_name";s:9:"鲁鲁修";s:6:"result";s:2:"62";}i:23;a:2:{s:8:"tag_name";s:5:"GEASS";s:6:"result";s:2:"57";}i:24;a:2:{s:8:"tag_name";s:6:"神作";s:6:"result";s:2:"54";}i:25;a:2:{s:8:"tag_name";s:6:"战斗";s:6:"result";s:2:"49";}i:26;a:2:{s:8:"tag_name";s:6:"战争";s:6:"result";s:2:"41";}i:27;a:2:{s:8:"tag_name";s:25:"裸露修的跌二次KUSO";s:6:"result";s:2:"40";}i:28;a:2:{s:8:"tag_name";s:6:"中二";s:6:"result";s:2:"37";}i:29;a:2:{s:8:"tag_name";s:12:"樱井孝宏";s:6:"result";s:2:"34";}}`
var tags []Tag
err := phpserialize.Unmarshal([]byte(raw), &tags)
require.NoError(t, err)
}

0 comments on commit 4d445eb

Please sign in to comment.