Skip to content

Commit

Permalink
chore: add notice for go1.24.0 on README (#766)
Browse files Browse the repository at this point in the history
  • Loading branch information
AsterDY authored Mar 7, 2025
1 parent ece85a2 commit e3ad5b2
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 114 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ A blazingly fast JSON serializing & deserializing library, accelerated by JI
## Requirement

- Go: 1.17~1.24
- Notice: Go1.24.0 is not supported due to the [issue](https://github.com/golang/go/issues/71672), please use higher go version or add build tag `--ldflags="-checklinkname=0"`
- OS: Linux / MacOS / Windows
- CPU: AMD64 / (ARM64, need go1.20 above)

Expand Down
1 change: 1 addition & 0 deletions README_ZH_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
## 依赖

- Go: 1.17~1.24
- 注意:Go1.24.0 由于 [issue](https://github.com/golang/go/issues/71672) 不可用,请升级到更高 Go 版本,或添加编译选项 `--ldflags="-checklinkname=0"`
- OS: Linux / MacOS / Windows
- CPU: AMD64 / (ARM64, 需要 Go1.20 以上)

Expand Down
229 changes: 115 additions & 114 deletions internal/encoder/omitzero_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//go:build go1.24
//+build go1.24

/**
* Copyright 2025 ByteDance Inc.
Expand All @@ -20,75 +21,75 @@ package encoder

import (
"testing"
"time"
"time"
"fmt"
"strings"
"strings"
)

type NonZeroStruct struct{}

func (nzs NonZeroStruct) IsZero() bool {
return false
return false
}

type NoPanicStruct struct {
Int int `json:"int,omitzero"`
Int int `json:"int,omitzero"`
}

func (nps *NoPanicStruct) IsZero() bool {
return nps.Int != 0
return nps.Int != 0
}

type isZeroer interface {
IsZero() bool
IsZero() bool
}

type OptionalsZero struct {
Sr string `json:"sr"`
So string `json:"so,omitzero"`
Sw string `json:"-"`

Ir int `json:"omitzero"` // actually named omitzero, not an option
Io int `json:"io,omitzero"`

Slr []string `json:"slr,random"`
Slo []string `json:"slo,omitzero"`
SloNonNil []string `json:"slononnil,omitzero"`

Mr map[string]interface{} `json:"mr"`
Mo map[string]interface{} `json:",omitzero"`
Moo map[string]interface{} `json:"moo,omitzero"`

Fr float64 `json:"fr"`
Fo float64 `json:"fo,omitzero"`
Foo float64 `json:"foo,omitzero"`
Foo2 [2]float64 `json:"foo2,omitzero"`

Br bool `json:"br"`
Bo bool `json:"bo,omitzero"`

Ur uint `json:"ur"`
Uo uint `json:"uo,omitzero"`

Str struct{} `json:"str"`
Sto struct{} `json:"sto,omitzero"`

Time time.Time `json:"time,omitzero"`
TimeLocal time.Time `json:"timelocal,omitzero"`
Nzs NonZeroStruct `json:"nzs,omitzero"`

NilIsZeroer isZeroer `json:"niliszeroer,omitzero"` // nil interface
NonNilIsZeroer isZeroer `json:"nonniliszeroer,omitzero"` // non-nil interface
NoPanicStruct0 isZeroer `json:"nps0,omitzero"` // non-nil interface with nil pointer
NoPanicStruct1 isZeroer `json:"nps1,omitzero"` // non-nil interface with non-nil pointer
NoPanicStruct2 *NoPanicStruct `json:"nps2,omitzero"` // nil pointer
NoPanicStruct3 *NoPanicStruct `json:"nps3,omitzero"` // non-nil pointer
NoPanicStruct4 NoPanicStruct `json:"nps4,omitzero"` // concrete type
Sr string `json:"sr"`
So string `json:"so,omitzero"`
Sw string `json:"-"`

Ir int `json:"omitzero"` // actually named omitzero, not an option
Io int `json:"io,omitzero"`

Slr []string `json:"slr,random"`
Slo []string `json:"slo,omitzero"`
SloNonNil []string `json:"slononnil,omitzero"`

Mr map[string]interface{} `json:"mr"`
Mo map[string]interface{} `json:",omitzero"`
Moo map[string]interface{} `json:"moo,omitzero"`

Fr float64 `json:"fr"`
Fo float64 `json:"fo,omitzero"`
Foo float64 `json:"foo,omitzero"`
Foo2 [2]float64 `json:"foo2,omitzero"`

Br bool `json:"br"`
Bo bool `json:"bo,omitzero"`

Ur uint `json:"ur"`
Uo uint `json:"uo,omitzero"`

Str struct{} `json:"str"`
Sto struct{} `json:"sto,omitzero"`

Time time.Time `json:"time,omitzero"`
TimeLocal time.Time `json:"timelocal,omitzero"`
Nzs NonZeroStruct `json:"nzs,omitzero"`

NilIsZeroer isZeroer `json:"niliszeroer,omitzero"` // nil interface
NonNilIsZeroer isZeroer `json:"nonniliszeroer,omitzero"` // non-nil interface
NoPanicStruct0 isZeroer `json:"nps0,omitzero"` // non-nil interface with nil pointer
NoPanicStruct1 isZeroer `json:"nps1,omitzero"` // non-nil interface with non-nil pointer
NoPanicStruct2 *NoPanicStruct `json:"nps2,omitzero"` // nil pointer
NoPanicStruct3 *NoPanicStruct `json:"nps3,omitzero"` // non-nil pointer
NoPanicStruct4 NoPanicStruct `json:"nps4,omitzero"` // concrete type
}

func TestOmitZero(t *testing.T) {
// ForceUseVM()
const want = `{
const want = `{
"sr": "",
"omitzero": 0,
"slr": null,
Expand All @@ -104,33 +105,33 @@ func TestOmitZero(t *testing.T) {
"nps3": {},
"nps4": {}
}`
var o OptionalsZero
o.Sw = "something"
o.SloNonNil = make([]string, 0)
o.Mr = map[string]interface{}{}
o.Mo = map[string]interface{}{}

o.Foo = -0
o.Foo2 = [2]float64{+0, -0}

o.TimeLocal = time.Time{}.Local()

o.NonNilIsZeroer = time.Time{}
o.NoPanicStruct0 = (*NoPanicStruct)(nil)
o.NoPanicStruct1 = &NoPanicStruct{}
o.NoPanicStruct3 = &NoPanicStruct{}

got, err := EncodeIndented(&o, "", " ", 0)
if err != nil {
t.Fatalf("MarshalIndent error: %v", err)
}
if got := string(got); got != want {
t.Errorf("MarshalIndent:\n\tgot: %s\n\twant: %s\n", indentNewlines(got), indentNewlines(want))
}
var o OptionalsZero
o.Sw = "something"
o.SloNonNil = make([]string, 0)
o.Mr = map[string]interface{}{}
o.Mo = map[string]interface{}{}

o.Foo = -0
o.Foo2 = [2]float64{+0, -0}

o.TimeLocal = time.Time{}.Local()

o.NonNilIsZeroer = time.Time{}
o.NoPanicStruct0 = (*NoPanicStruct)(nil)
o.NoPanicStruct1 = &NoPanicStruct{}
o.NoPanicStruct3 = &NoPanicStruct{}

got, err := EncodeIndented(&o, "", " ", 0)
if err != nil {
t.Fatalf("MarshalIndent error: %v", err)
}
if got := string(got); got != want {
t.Errorf("MarshalIndent:\n\tgot: %s\n\twant: %s\n", indentNewlines(got), indentNewlines(want))
}
}

func TestOmitZeroMap(t *testing.T) {
const want = `{
const want = `{
"foo": {
"sr": "",
"omitzero": 0,
Expand All @@ -144,49 +145,49 @@ func TestOmitZeroMap(t *testing.T) {
"nps4": {}
}
}`
m := map[string]OptionalsZero{"foo": {}}
got, err := EncodeIndented(m, "", " ", 0)
if err != nil {
t.Fatalf("MarshalIndent error: %v", err)
}
if got := string(got); got != want {
fmt.Println(got)
t.Errorf("MarshalIndent:\n\tgot: %s\n\twant: %s\n", indentNewlines(got), indentNewlines(want))
}
m := map[string]OptionalsZero{"foo": {}}
got, err := EncodeIndented(m, "", " ", 0)
if err != nil {
t.Fatalf("MarshalIndent error: %v", err)
}
if got := string(got); got != want {
fmt.Println(got)
t.Errorf("MarshalIndent:\n\tgot: %s\n\twant: %s\n", indentNewlines(got), indentNewlines(want))
}
}

type OptionalsEmptyZero struct {
Sr string `json:"sr"`
So string `json:"so,omitempty,omitzero"`
Sw string `json:"-"`
Sr string `json:"sr"`
So string `json:"so,omitempty,omitzero"`
Sw string `json:"-"`

Io int `json:"io,omitempty,omitzero"`
Io int `json:"io,omitempty,omitzero"`

Slr []string `json:"slr,random"`
Slo []string `json:"slo,omitempty,omitzero"`
SloNonNil []string `json:"slononnil,omitempty,omitzero"`
Slr []string `json:"slr,random"`
Slo []string `json:"slo,omitempty,omitzero"`
SloNonNil []string `json:"slononnil,omitempty,omitzero"`

Mr map[string]interface{} `json:"mr"`
Mo map[string]interface{} `json:",omitempty,omitzero"`
Mr map[string]interface{} `json:"mr"`
Mo map[string]interface{} `json:",omitempty,omitzero"`

Fr float64 `json:"fr"`
Fo float64 `json:"fo,omitempty,omitzero"`
Fr float64 `json:"fr"`
Fo float64 `json:"fo,omitempty,omitzero"`

Br bool `json:"br"`
Bo bool `json:"bo,omitempty,omitzero"`
Br bool `json:"br"`
Bo bool `json:"bo,omitempty,omitzero"`

Ur uint `json:"ur"`
Uo uint `json:"uo,omitempty,omitzero"`
Ur uint `json:"ur"`
Uo uint `json:"uo,omitempty,omitzero"`

Str struct{} `json:"str"`
Sto struct{} `json:"sto,omitempty,omitzero"`
Str struct{} `json:"str"`
Sto struct{} `json:"sto,omitempty,omitzero"`

Time time.Time `json:"time,omitempty,omitzero"`
Nzs NonZeroStruct `json:"nzs,omitempty,omitzero"`
Time time.Time `json:"time,omitempty,omitzero"`
Nzs NonZeroStruct `json:"nzs,omitempty,omitzero"`
}

func TestOmitEmptyZero(t *testing.T) {
const want = `{
const want = `{
"sr": "",
"slr": null,
"mr": {},
Expand All @@ -196,21 +197,21 @@ func TestOmitEmptyZero(t *testing.T) {
"str": {},
"nzs": {}
}`
var o OptionalsEmptyZero
o.Sw = "something"
o.SloNonNil = make([]string, 0)
o.Mr = map[string]interface{}{}
o.Mo = map[string]interface{}{}

got, err := EncodeIndented(&o, "", " ", 0)
if err != nil {
t.Fatalf("MarshalIndent error: %v", err)
}
if got := string(got); got != want {
t.Errorf("MarshalIndent:\n\tgot: %s\n\twant: %s\n", indentNewlines(got), indentNewlines(want))
}
var o OptionalsEmptyZero
o.Sw = "something"
o.SloNonNil = make([]string, 0)
o.Mr = map[string]interface{}{}
o.Mo = map[string]interface{}{}

got, err := EncodeIndented(&o, "", " ", 0)
if err != nil {
t.Fatalf("MarshalIndent error: %v", err)
}
if got := string(got); got != want {
t.Errorf("MarshalIndent:\n\tgot: %s\n\twant: %s\n", indentNewlines(got), indentNewlines(want))
}
}

func indentNewlines(s string) string {
return strings.Join(strings.Split(s, "\n"), "\n\t")
return strings.Join(strings.Split(s, "\n"), "\n\t")
}

0 comments on commit e3ad5b2

Please sign in to comment.