From 4d445ebeb0dbc99534421a2a5c6c681261f75fd9 Mon Sep 17 00:00:00 2001 From: Trim21 Date: Thu, 10 Oct 2024 18:39:19 +0800 Subject: [PATCH] refactor: remove unused fields --- internal/decoder/compile.go | 6 ++--- internal/decoder/struct.go | 1 - internal/encoder/comple_reflect.go | 1 - internal/encoder/uint_at_string.go | 36 +----------------------------- internal/test/diff.go | 31 +++++++++++++------------ unmarshal_test.go | 13 +++++++++++ 6 files changed, 34 insertions(+), 54 deletions(-) delete mode 100644 internal/encoder/comple_reflect.go diff --git a/internal/decoder/compile.go b/internal/decoder/compile.go index 0143b13..8460de8 100644 --- a/internal/decoder/compile.go +++ b/internal/decoder/compile.go @@ -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) diff --git a/internal/decoder/struct.go b/internal/decoder/struct.go index 9c0d65f..d4d0f24 100644 --- a/internal/decoder/struct.go +++ b/internal/decoder/struct.go @@ -9,7 +9,6 @@ import ( type structFieldSet struct { dec Decoder - offset uintptr fieldIdx int key string err error diff --git a/internal/encoder/comple_reflect.go b/internal/encoder/comple_reflect.go deleted file mode 100644 index 8a2c87f..0000000 --- a/internal/encoder/comple_reflect.go +++ /dev/null @@ -1 +0,0 @@ -package encoder diff --git a/internal/encoder/uint_at_string.go b/internal/encoder/uint_at_string.go index b184d3e..5a93b90 100644 --- a/internal/encoder/uint_at_string.go +++ b/internal/encoder/uint_at_string.go @@ -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) { diff --git a/internal/test/diff.go b/internal/test/diff.go index 24921f5..5475ac8 100644 --- a/internal/test/diff.go +++ b/internal/test/diff.go @@ -2,6 +2,7 @@ package test import ( "bytes" + "strconv" "testing" "github.com/fatih/color" @@ -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 { @@ -39,7 +35,7 @@ func diffsToString(diffs []diffmatchpatch.Diff) string { } } - buff.WriteString("\n") + buff.WriteString("\nactual : ") for _, diff := range diffs { text := diff.Text @@ -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() } diff --git a/unmarshal_test.go b/unmarshal_test.go index 047caff..ef4be59 100644 --- a/unmarshal_test.go +++ b/unmarshal_test.go @@ -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) +}