diff --git a/cmd/agentbasedinstaller/import.go b/cmd/agentbasedinstaller/import.go index 70027328abd..a3582fc826b 100644 --- a/cmd/agentbasedinstaller/import.go +++ b/cmd/agentbasedinstaller/import.go @@ -2,11 +2,11 @@ package agentbasedinstaller import ( "context" - "encoding/json" "fmt" "io/fs" "os" + json "github.com/bytedance/sonic" "github.com/go-openapi/strfmt" "github.com/openshift/assisted-service/client" "github.com/openshift/assisted-service/client/installer" diff --git a/cmd/main.go b/cmd/main.go index e2a1c6094ca..054554b991d 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -2,7 +2,6 @@ package main import ( "context" - "encoding/json" "flag" "fmt" "net/http" @@ -15,6 +14,7 @@ import ( "time" "github.com/NYTimes/gziphandler" + json "github.com/bytedance/sonic" "github.com/go-openapi/runtime" "github.com/go-openapi/strfmt" "github.com/go-openapi/swag" diff --git a/cmd/operator/main.go b/cmd/operator/main.go index ab06a64b40b..680f3023f6b 100644 --- a/cmd/operator/main.go +++ b/cmd/operator/main.go @@ -18,11 +18,11 @@ package main import ( "context" - "encoding/json" "flag" "fmt" "os" + json "github.com/bytedance/sonic" certtypes "github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1" configv1 "github.com/openshift/api/config/v1" routev1 "github.com/openshift/api/route/v1" diff --git a/go.mod b/go.mod index 50a60a2e0bb..ddcd1be6bb7 100644 --- a/go.mod +++ b/go.mod @@ -10,6 +10,7 @@ require ( github.com/aws/aws-sdk-go v1.49.13 github.com/bombsimon/logrusr/v3 v3.0.0 github.com/buger/jsonparser v1.1.1 + github.com/bytedance/sonic v1.12.9 github.com/cavaliercoder/go-cpio v0.0.0-20180626203310-925f9528c45e github.com/cert-manager/cert-manager v1.14.7 github.com/coreos/ignition/v2 v2.14.0 @@ -99,7 +100,9 @@ require ( github.com/MakeNowJust/heredoc v1.0.0 // indirect github.com/Microsoft/hcsshim v0.11.4 // indirect github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20230305170008-8188dc5388df // indirect + github.com/bytedance/sonic/loader v0.2.2 // indirect github.com/chai2010/gettext-go v1.0.2 // indirect + github.com/cloudwego/base64x v0.1.5 // indirect github.com/containerd/containerd v1.7.12 // indirect github.com/containerd/log v0.1.0 // indirect github.com/cpuguy83/dockercfg v0.3.1 // indirect @@ -120,6 +123,7 @@ require ( github.com/jackc/chunkreader/v2 v2.0.1 // indirect github.com/jackc/pgio v1.0.0 // indirect github.com/jackc/pgproto3/v2 v2.3.3 // indirect + github.com/klauspost/cpuid/v2 v2.2.4 // indirect github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de // indirect github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect github.com/magiconair/properties v1.8.7 // indirect @@ -141,6 +145,7 @@ require ( github.com/stoewer/go-strcase v1.3.0 // indirect github.com/tklauser/go-sysconf v0.3.12 // indirect github.com/tklauser/numcpus v0.6.1 // indirect + github.com/twitchyliquid64/golang-asm v0.15.1 // indirect github.com/xdg/scram v1.0.5 // indirect github.com/xdg/stringprep v1.0.3 // indirect github.com/xlab/treeprint v1.2.0 // indirect @@ -148,6 +153,7 @@ require ( go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.21.0 // indirect go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.21.0 // indirect go.starlark.net v0.0.0-20230525235612-a134d8f9ddca // indirect + golang.org/x/arch v0.3.0 // indirect golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 // indirect golang.org/x/mod v0.19.0 // indirect golang.org/x/tools v0.23.0 // indirect diff --git a/go.sum b/go.sum index b19f5e617ad..c62cb63f883 100644 --- a/go.sum +++ b/go.sum @@ -148,6 +148,11 @@ github.com/bombsimon/logrusr/v3 v3.0.0 h1:tcAoLfuAhKP9npBxWzSdpsvKPQt1XV02nSf2lZ github.com/bombsimon/logrusr/v3 v3.0.0/go.mod h1:PksPPgSFEL2I52pla2glgCyyd2OqOHAnFF5E+g8Ixco= github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMUs= github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0= +github.com/bytedance/sonic v1.12.9 h1:Od1BvK55NnewtGaJsTDeAOSnLVO2BTSLOe0+ooKokmQ= +github.com/bytedance/sonic v1.12.9/go.mod h1:uVvFidNmlt9+wa31S1urfwwthTWteBgG0hWuoKAXTx8= +github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU= +github.com/bytedance/sonic/loader v0.2.2 h1:jxAJuN9fOot/cyz5Q6dUuMJF5OqQ6+5GfA8FjjQ0R4o= +github.com/bytedance/sonic/loader v0.2.2/go.mod h1:N8A3vUdtUebEY2/VQC0MyhYeKUFosQU6FxH2JmUe6VI= github.com/casbin/casbin/v2 v2.1.2/go.mod h1:YcPU1XXisHhLzuxH9coDNf2FbKpjGlbCg3n9yuLkIJQ= github.com/cavaliercoder/go-cpio v0.0.0-20180626203310-925f9528c45e h1:hHg27A0RSSp2Om9lubZpiMgVbvn39bsUmW9U5h0twqc= github.com/cavaliercoder/go-cpio v0.0.0-20180626203310-925f9528c45e/go.mod h1:oDpT4efm8tSYHXV5tHSdRvBet/b/QzxZ+XyyPehvm3A= @@ -174,6 +179,9 @@ github.com/cilium/ebpf v0.7.0/go.mod h1:/oI2+1shJiTGAMgl6/RgJr36Eo1jzrRcAWbcXO2u github.com/clarketm/json v1.14.1/go.mod h1:ynr2LRfb0fQU34l07csRNBTcivjySLLiY1YzQqKVfdo= github.com/clbanning/x2j v0.0.0-20191024224557-825249438eec/go.mod h1:jMjuTZXRI4dUb/I5gc9Hdhagfvm9+RyrPryS/auMzxE= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= +github.com/cloudwego/base64x v0.1.5 h1:XPciSp1xaq2VCSt6lF0phncD4koWyULpl5bUxbfCyP4= +github.com/cloudwego/base64x v0.1.5/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w= +github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY= github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= github.com/cncf/xds/go v0.0.0-20230607035331-e9ce68804cb4 h1:/inchEIKaYC1Akx+H+gqO04wryn5h75LSazbRlnya1k= github.com/cncf/xds/go v0.0.0-20230607035331-e9ce68804cb4/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs= @@ -810,7 +818,11 @@ github.com/klauspost/compress v1.17.1 h1:NE3C767s2ak2bweCZo3+rdP4U/HoyVXLv/X9f2g github.com/klauspost/compress v1.17.1/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= github.com/klauspost/cpuid v0.0.0-20180405133222-e7e905edc00e/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek= github.com/klauspost/cpuid v1.2.0/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek= +github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= +github.com/klauspost/cpuid/v2 v2.2.4 h1:acbojRNwl3o09bUq+yDCtZFc1aiwaAAxtcn8YkZXnvk= +github.com/klauspost/cpuid/v2 v2.2.4/go.mod h1:RVVoqg1df56z8g3pUjL/3lE5UfnlrJX8tyFgg4nqhuY= github.com/klauspost/pgzip v1.2.4/go.mod h1:Ch1tH69qFZu15pkjo5kYi6mth2Zzwzt50oCQKQE9RUs= +github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= @@ -1249,6 +1261,8 @@ github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1 github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= github.com/tmc/grpc-websocket-proxy v0.0.0-20220101234140-673ab2c3ae75 h1:6fotK7otjonDflCTK0BCfls4SPy3NcCVb5dqqmbRknE= github.com/tmc/grpc-websocket-proxy v0.0.0-20220101234140-673ab2c3ae75/go.mod h1:KO6IkyS8Y3j8OdNO85qEYBsRPuteD+YciPomcXdrMnk= +github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= +github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc= github.com/ulikunitz/xz v0.5.7/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14= github.com/ulikunitz/xz v0.5.11 h1:kpFauv27b6ynzBNT/Xy+1k+fK4WswhN/6PN5WhFAGw8= @@ -1377,6 +1391,8 @@ go.uber.org/zap v1.13.0/go.mod h1:zwrFLgMcdUuIBviXEYEH1YKNaOBnKXsx2IPda5bBwHM= go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo= go.uber.org/zap v1.26.0/go.mod h1:dtElttAiwGvoJ/vj4IwHBS/gXsEu/pZ50mUIRWuG0so= go4.org v0.0.0-20200104003542-c7e774b10ea0/go.mod h1:MkTOUMDaeVYJUOUsaDXIhWPZYa1yOyC1qaOBpL57BhE= +golang.org/x/arch v0.3.0 h1:02VY4/ZcO/gBOH6PUaoiptASxtXU10jazRCP865E97k= +golang.org/x/arch v0.3.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20181029021203-45a5f77698d3/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= @@ -1611,6 +1627,7 @@ golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220209214540-3681064d5158/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220408201424-a24fb2fb8a0f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= @@ -1971,6 +1988,7 @@ k8s.io/utils v0.0.0-20240102154912-e7106e64919e/go.mod h1:OLgZIPagt7ERELqWJFomSt mvdan.cc/interfacer v0.0.0-20180901003855-c20040233aed/go.mod h1:Xkxe497xwlCKkIaQYRfC7CSLworTXY9RMqwhhCm+8Nc= mvdan.cc/lint v0.0.0-20170908181259-adc824a0674b/go.mod h1:2odslEg/xrtNQqCYg2/jCoyKnw3vv5biOc3JnIcYfL4= mvdan.cc/unparam v0.0.0-20190209190245-fbb59629db34/go.mod h1:H6SUd1XjIs+qQCyskXg5OFSrilMRUkD8ePJpHKDPaeY= +nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50= open-cluster-management.io/api v0.12.0 h1:sNkj4k2XyWA/GLsTiFg82bLIZ7JDZKkLLLyZjJUlJMs= open-cluster-management.io/api v0.12.0/go.mod h1:/CZhelEH+30/pX7vXGSZOzLMX0zvjthYOkT/5ZTzVTQ= rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8= diff --git a/internal/bminventory/inventory.go b/internal/bminventory/inventory.go index 24184f90c5d..541fe3c93ce 100644 --- a/internal/bminventory/inventory.go +++ b/internal/bminventory/inventory.go @@ -5,7 +5,6 @@ import ( "context" "crypto/md5" // #nosec "crypto/x509" - "encoding/json" "fmt" "io" "net" @@ -14,6 +13,7 @@ import ( "strings" "time" + json "github.com/bytedance/sonic" "github.com/go-openapi/runtime/middleware" "github.com/go-openapi/strfmt" "github.com/go-openapi/swag" @@ -508,7 +508,7 @@ func MarshalNewClusterParamsNoPullSecret(params installer.V2RegisterClusterParam clusterParamsNoPullSecret.PullSecret = &newSecret } - jsonNewClusterParams, err := json.Marshal(clusterParamsNoPullSecret) + jsonNewClusterParams, err := json.ConfigStd.Marshal(clusterParamsNoPullSecret) if err != nil { log.Error("An error occurred while marshaling the new cluster's parameters: ", err) return nil @@ -2940,7 +2940,7 @@ func (b *bareMetalInventory) setDefaultUsage(cluster *models.Cluster) error { if err != nil { return fmt.Errorf("failed setting platform usages, error is: %w", err) } - featusage, _ := json.Marshal(usages) + featusage, _ := json.ConfigStd.Marshal(usages) cluster.FeatureUsage = string(featusage) return nil } @@ -3812,7 +3812,7 @@ func filterReply(expected interface{}, input string) (string, error) { if err := json.Unmarshal([]byte(input), expected); err != nil { return "", err } - reply, err := json.Marshal(expected) + reply, err := json.ConfigStd.Marshal(expected) if err != nil { return "", err } @@ -4617,7 +4617,7 @@ func (b *bareMetalInventory) DeregisterInfraEnvInternal(ctx context.Context, par if len(hosts) > 0 { msg := fmt.Sprintf("failed to deregister infraEnv %s, %d hosts are still associated", params.InfraEnvID, len(hosts)) log.Error(msg) - return common.NewApiError(http.StatusBadRequest, fmt.Errorf(msg)) + return common.NewApiError(http.StatusBadRequest, errors.New(msg)) } if err = b.infraEnvApi.DeregisterInfraEnv(ctx, params.InfraEnvID); err != nil { @@ -4824,7 +4824,7 @@ func (b *bareMetalInventory) RegisterInfraEnvInternal(ctx context.Context, kubeK var kernelArguments *string if len(params.InfraenvCreateParams.KernelArguments) > 0 { var b []byte - b, err = json.Marshal(¶ms.InfraenvCreateParams.KernelArguments) + b, err = json.ConfigStd.Marshal(¶ms.InfraenvCreateParams.KernelArguments) if err != nil { return common.NewApiError(http.StatusBadRequest, errors.Wrap(err, "failed to format kernel arguments as json")) } @@ -5456,7 +5456,7 @@ func (b *bareMetalInventory) updateInfraEnvKernelArguments(params installer.Upda } if len(params.InfraEnvUpdateParams.KernelArguments) > 0 { - b, err := json.Marshal(¶ms.InfraEnvUpdateParams.KernelArguments) + b, err := json.ConfigStd.Marshal(¶ms.InfraEnvUpdateParams.KernelArguments) if err != nil { return common.NewApiError(http.StatusBadRequest, errors.Wrap(err, "failed to format kernel arguments as json")) } @@ -6006,7 +6006,7 @@ func (b *bareMetalInventory) V2UpdateHostInstallerArgsInternal(ctx context.Conte return nil, err } - argsBytes, err := json.Marshal(params.InstallerArgsParams.Args) + argsBytes, err := json.ConfigStd.Marshal(params.InstallerArgsParams.Args) if err != nil { return nil, err } @@ -6455,7 +6455,7 @@ func (b *bareMetalInventory) updateIgnitionEndpointHTTPHeaders(ctx context.Conte ignitionEndpointHTTPHeaders[*hdr.Key] = *hdr.Value } - ignitionEndpointHTTPHeadersStr, err := json.Marshal(ignitionEndpointHTTPHeaders) + ignitionEndpointHTTPHeadersStr, err := json.ConfigStd.Marshal(ignitionEndpointHTTPHeaders) if err != nil { return common.NewApiError(http.StatusBadRequest, errors.Wrapf(err, "failed to marshal ignition endpoint HTTP Headers for host %s", host.ID)) } diff --git a/internal/bminventory/inventory_test.go b/internal/bminventory/inventory_test.go index 73f38b05b04..8ed77f55280 100644 --- a/internal/bminventory/inventory_test.go +++ b/internal/bminventory/inventory_test.go @@ -6,7 +6,6 @@ import ( "context" "crypto/rand" "encoding/base64" - "encoding/json" "fmt" "io" "mime/multipart" @@ -22,6 +21,7 @@ import ( "testing" "time" + json "github.com/bytedance/sonic" "github.com/cavaliercoder/go-cpio" ign_3_1 "github.com/coreos/ignition/v2/config/v3_1" "github.com/go-openapi/runtime" @@ -396,7 +396,7 @@ func addVMToCluster(cluster *common.Cluster, db *gorm.DB) { Virtual: true, }, } - inventoryByte, err := json.Marshal(inventory) + inventoryByte, err := json.ConfigStd.Marshal(inventory) Expect(err).ToNot(HaveOccurred()) host := addHost(hostID, models.HostRoleAutoAssign, models.HostStatusKnown, models.HostKindHost, *infraEnv.ID, *cluster.ID, string(inventoryByte), db) @@ -871,7 +871,7 @@ func makeFreeNetworksAddresses(elems ...*models.FreeNetworkAddresses) models.Fre func makeFreeNetworksAddressesStr(elems ...*models.FreeNetworkAddresses) string { toMarshal := models.FreeNetworksAddresses(elems) - b, err := json.Marshal(&toMarshal) + b, err := json.ConfigStd.Marshal(&toMarshal) Expect(err).ToNot(HaveOccurred()) return string(b) } @@ -898,7 +898,7 @@ var _ = Describe("v2PostStepReply", func() { Context("Free addresses", func() { var makeStepReply = func(clusterID, hostID strfmt.UUID, freeAddresses models.FreeNetworksAddresses) installer.V2PostStepReplyParams { - b, _ := json.Marshal(&freeAddresses) + b, _ := json.ConfigStd.Marshal(&freeAddresses) return installer.V2PostStepReplyParams{ InfraEnvID: clusterID, HostID: hostID, @@ -985,7 +985,7 @@ var _ = Describe("v2PostStepReply", func() { Expect(db.Create(&host).Error).ShouldNot(HaveOccurred()) }) var makeStepReply = func(clusterID, hostID strfmt.UUID, response models.VerifyVipsResponse) installer.V2PostStepReplyParams { - b, _ := json.Marshal(&response) + b, _ := json.ConfigStd.Marshal(&response) return installer.V2PostStepReplyParams{ InfraEnvID: clusterID, HostID: hostID, @@ -997,7 +997,7 @@ var _ = Describe("v2PostStepReply", func() { } marshal := func(response models.VerifyVipsResponse) string { - b, err := json.Marshal(&response) + b, err := json.ConfigStd.Marshal(&response) Expect(err).ToNot(HaveOccurred()) return string(b) } @@ -1018,7 +1018,7 @@ var _ = Describe("v2PostStepReply", func() { var ( clusterId, hostId *strfmt.UUID makeStepReply = func(clusterID, hostID strfmt.UUID, dhcpAllocationResponse *models.DhcpAllocationResponse) installer.V2PostStepReplyParams { - b, err := json.Marshal(dhcpAllocationResponse) + b, err := json.ConfigStd.Marshal(dhcpAllocationResponse) Expect(err).ToNot(HaveOccurred()) return installer.V2PostStepReplyParams{ InfraEnvID: clusterID, @@ -1198,7 +1198,7 @@ var _ = Describe("v2PostStepReply", func() { NtpSources: ntpSources, } - b, _ := json.Marshal(&response) + b, _ := json.ConfigStd.Marshal(&response) return installer.V2PostStepReplyParams{ InfraEnvID: clusterID, @@ -1257,7 +1257,7 @@ var _ = Describe("v2PostStepReply", func() { Images: statuses, } - b, err := json.Marshal(&response) + b, err := json.ConfigStd.Marshal(&response) Expect(err).ShouldNot(HaveOccurred()) return installer.V2PostStepReplyParams{ @@ -1320,7 +1320,7 @@ var _ = Describe("v2PostStepReply", func() { Path: path, } - b, err := json.Marshal(&response) + b, err := json.ConfigStd.Marshal(&response) Expect(err).ShouldNot(HaveOccurred()) return installer.V2PostStepReplyParams{ @@ -6166,7 +6166,7 @@ var _ = Describe("V2UpdateCluster", func() { SerialNumber: "VMware-12 34 56 78 90 12 ab cd-ef gh 12 34 56 67 89 90", }, } - inventoryByte, err := json.Marshal(inventory) + inventoryByte, err := json.ConfigStd.Marshal(inventory) Expect(err).ToNot(HaveOccurred()) addHost(strfmt.UUID(uuid.New().String()), role, models.HostStatusKnown, models.HostKindHost, clusterID, string(inventoryByte), db) } @@ -8847,7 +8847,7 @@ var _ = Describe("infraEnvs", func() { var dbInfraEnv common.InfraEnv Expect(db.First(&dbInfraEnv, "id = ?", actual.Payload.ID.String()).Error).To(Succeed()) Expect(dbInfraEnv.ImageTokenKey).NotTo(Equal("")) - jsonEncodedKernelParameters, err := json.Marshal(&kernelArguments) + jsonEncodedKernelParameters, err := json.ConfigStd.Marshal(&kernelArguments) Expect(err).ToNot(HaveOccurred()) Expect(swag.StringValue(dbInfraEnv.KernelArguments)).To(Equal(string(jsonEncodedKernelParameters))) }) @@ -8891,7 +8891,7 @@ var _ = Describe("infraEnvs", func() { var dbInfraEnv common.InfraEnv Expect(db.First(&dbInfraEnv, "id = ?", actual.Payload.ID.String()).Error).To(Succeed()) Expect(dbInfraEnv.ImageTokenKey).NotTo(Equal("")) - jsonEncodedKernelParameters, err := json.Marshal(&kernelArguments) + jsonEncodedKernelParameters, err := json.ConfigStd.Marshal(&kernelArguments) Expect(err).ToNot(HaveOccurred()) Expect(swag.StringValue(dbInfraEnv.KernelArguments)).To(Equal(string(jsonEncodedKernelParameters))) }) @@ -9410,7 +9410,7 @@ var _ = Describe("infraEnvs", func() { }) Context("Update discovery kernel arguments", func() { jsonEncodeKernelArguments := func(array models.KernelArguments) string { - b, e := json.Marshal(&array) + b, e := json.ConfigStd.Marshal(&array) Expect(e).ToNot(HaveOccurred()) return string(b) } @@ -13336,7 +13336,7 @@ func getInventoryStr(hostname, bootMode string, ipv4Addresses ...string) string {Path: "/dev/sdb", Bootable: false}, }, } - ret, _ := json.Marshal(&inventory) + ret, _ := json.ConfigStd.Marshal(&inventory) return string(ret) } @@ -13356,7 +13356,7 @@ func getInventoryStrWithIPv6(hostname, bootMode string, ipv4Addresses []string, {Path: "/dev/sdb", Bootable: false}, }, } - ret, _ := json.Marshal(&inventory) + ret, _ := json.ConfigStd.Marshal(&inventory) return string(ret) } diff --git a/internal/bminventory/inventory_v2_handlers.go b/internal/bminventory/inventory_v2_handlers.go index 3f6c9aa3968..aa1c9d643a3 100644 --- a/internal/bminventory/inventory_v2_handlers.go +++ b/internal/bminventory/inventory_v2_handlers.go @@ -2,7 +2,6 @@ package bminventory import ( "context" - "encoding/json" "fmt" "io" "net/http" @@ -11,6 +10,7 @@ import ( "strings" "time" + json "github.com/bytedance/sonic" "github.com/go-openapi/runtime/middleware" "github.com/go-openapi/strfmt" "github.com/go-openapi/swag" @@ -334,7 +334,7 @@ func (b *bareMetalInventory) V2CompleteInstallation(ctx context.Context, params } parse := func(data interface{}) ([]models.OperatorMonitorReport, error) { - raw, err := json.Marshal(data) + raw, err := json.ConfigStd.Marshal(data) if err != nil { return nil, err } diff --git a/internal/cluster/capabilities.go b/internal/cluster/capabilities.go index 0cd7665ddf5..a9e75efef87 100644 --- a/internal/cluster/capabilities.go +++ b/internal/cluster/capabilities.go @@ -1,8 +1,7 @@ package cluster import ( - "encoding/json" - + json "github.com/bytedance/sonic" configv1 "github.com/openshift/api/config/v1" "github.com/openshift/assisted-service/internal/common" "github.com/openshift/assisted-service/internal/installcfg" diff --git a/internal/cluster/cluster.go b/internal/cluster/cluster.go index 7cee13fccef..836fbf5a473 100644 --- a/internal/cluster/cluster.go +++ b/internal/cluster/cluster.go @@ -2,7 +2,6 @@ package cluster import ( "context" - "encoding/json" "fmt" "io" "net/http" @@ -13,6 +12,7 @@ import ( "strings" "time" + json "github.com/bytedance/sonic" "github.com/filanov/stateswitch" "github.com/go-openapi/strfmt" "github.com/go-openapi/swag" @@ -326,7 +326,7 @@ func (m *Manager) didValidationChanged(newValidationRes, currentValidationRes Va } func (m *Manager) updateValidationsInDB(ctx context.Context, db *gorm.DB, c *common.Cluster, newValidationRes ValidationsStatus) (*common.Cluster, error) { - b, err := json.Marshal(newValidationRes) + b, err := json.ConfigStd.Marshal(newValidationRes) if err != nil { return nil, err } @@ -1082,7 +1082,7 @@ func (m *Manager) SetVipsData(ctx context.Context, c *common.Cluster, apiVip, in } func (m *Manager) uploadDataAsFile(ctx context.Context, log logrus.FieldLogger, data interface{}, fileName string, objectHandler s3wrapper.API) error { - marshalled, err := json.MarshalIndent(data, "", " ") + marshalled, err := json.ConfigStd.MarshalIndent(data, "", " ") if err != nil { log.WithError(err).Warnf("Failed to marshall data for %s", fileName) return err @@ -1314,7 +1314,7 @@ func (m *Manager) detectAndStoreCollidingIPsForCluster(cluster *common.Cluster, } } - b, err := json.Marshal(&collidingIPSWithMacs) + b, err := json.ConfigStd.Marshal(&collidingIPSWithMacs) if err != nil { return common.NewApiError(http.StatusInternalServerError, err) } @@ -1409,7 +1409,7 @@ func (m *Manager) setConnectivityMajorityGroupsForClusterInternal(cluster *commo return common.NewApiError(http.StatusInternalServerError, err) } connectivity.L3ConnectedAddresses = connectedAddresses - b, err := json.Marshal(&connectivity) + b, err := json.ConfigStd.Marshal(&connectivity) if err != nil { return common.NewApiError(http.StatusInternalServerError, err) } diff --git a/internal/cluster/cluster_test.go b/internal/cluster/cluster_test.go index d1b40c67456..4b558a7291a 100644 --- a/internal/cluster/cluster_test.go +++ b/internal/cluster/cluster_test.go @@ -3,7 +3,6 @@ package cluster import ( "bytes" "context" - "encoding/json" "fmt" "io" "net/http" @@ -11,6 +10,7 @@ import ( "sort" "time" + json "github.com/bytedance/sonic" "github.com/go-openapi/strfmt" "github.com/go-openapi/swag" "github.com/golang/mock/gomock" @@ -1805,7 +1805,7 @@ func addInstallationRequirementsWithConnectivity(clusterId strfmt.UUID, db *gorm }, }, } - b, err := json.Marshal(&connectivityReport) + b, err := json.ConfigStd.Marshal(&connectivityReport) Expect(err).ToNot(HaveOccurred()) host = models.Host{ ID: &hostId, @@ -1848,7 +1848,7 @@ func defaultInventory() string { }, Routes: common.TestDefaultRouteConfiguration, } - b, err := json.Marshal(&inventory) + b, err := json.ConfigStd.Marshal(&inventory) Expect(err).To(Not(HaveOccurred())) return string(b) } @@ -1865,7 +1865,7 @@ func twoNetworksInventory() string { }, }, } - b, err := json.Marshal(&inventory) + b, err := json.ConfigStd.Marshal(&inventory) Expect(err).To(Not(HaveOccurred())) return string(b) } @@ -1881,7 +1881,7 @@ func nonDefaultInventory() string { }, }, } - b, err := json.Marshal(&inventory) + b, err := json.ConfigStd.Marshal(&inventory) Expect(err).To(Not(HaveOccurred())) return string(b) } @@ -2398,7 +2398,7 @@ var _ = Describe("validate vips response", func() { }) createPayload := func(response models.VerifyVipsResponse) string { - b, err := json.Marshal(&response) + b, err := json.ConfigStd.Marshal(&response) Expect(err) return string(b) } @@ -2862,7 +2862,7 @@ var _ = Describe("Cluster tarred files", func() { }, }} mockEvents.EXPECT().V2GetEvents(gomock.Any(), common.GetDefaultV2GetEventsParams(cl.ID, nil, nil)).Return(&common.V2GetEventsResponse{Events: events}, nil).Times(1) - eventsData, _ := json.MarshalIndent(events, "", " ") + eventsData, _ := json.ConfigStd.MarshalIndent(events, "", " ") mockS3Client.EXPECT().Upload(ctx, eventsData, eventsFilename).Return(nil).Times(1) } @@ -3632,7 +3632,7 @@ var _ = Describe("Validation metrics and events", func() { }, } validationRes := generateTestValidationResult(ValidationFailure) - bytes, err := json.Marshal(validationRes) + bytes, err := json.ConfigStd.Marshal(validationRes) Expect(err).ShouldNot(HaveOccurred()) c.ValidationsInfo = string(bytes) err = m.RegisterCluster(ctx, &c) diff --git a/internal/cluster/transition_test.go b/internal/cluster/transition_test.go index 332abd44d32..8e9e6bc7cb1 100644 --- a/internal/cluster/transition_test.go +++ b/internal/cluster/transition_test.go @@ -2,12 +2,12 @@ package cluster import ( "context" - "encoding/json" "fmt" "net/http" "regexp" "time" + json "github.com/bytedance/sonic" "github.com/go-openapi/strfmt" "github.com/go-openapi/swag" "github.com/golang/mock/gomock" @@ -5303,7 +5303,7 @@ func makeFreeAddresses(network string, ips ...strfmt.IPv4) *models.FreeNetworkAd func makeFreeNetworksAddressesStr(elems ...*models.FreeNetworkAddresses) string { toMarshal := models.FreeNetworksAddresses(elems) - b, err := json.Marshal(&toMarshal) + b, err := json.ConfigStd.Marshal(&toMarshal) Expect(err).ToNot(HaveOccurred()) return string(b) } diff --git a/internal/cluster/validations/pull_secret_validation.go b/internal/cluster/validations/pull_secret_validation.go index 6d1311c14e4..8c6b0abbcf7 100644 --- a/internal/cluster/validations/pull_secret_validation.go +++ b/internal/cluster/validations/pull_secret_validation.go @@ -2,10 +2,10 @@ package validations import ( "encoding/base64" - "encoding/json" "fmt" "strings" + json "github.com/bytedance/sonic" "github.com/openshift/assisted-service/pkg/auth" "github.com/openshift/assisted-service/pkg/mirrorregistries" "github.com/openshift/assisted-service/pkg/ocm" @@ -106,13 +106,13 @@ func AddRHRegPullSecret(secret, rhCred string) (string, error) { return "", errors.Errorf("invalid pull secret") } var s imagePullSecret - err := json.Unmarshal([]byte(strings.TrimSpace(secret)), &s) + err := json.ConfigStd.Unmarshal([]byte(strings.TrimSpace(secret)), &s) if err != nil { return secret, errors.Errorf("invalid pull secret: %v", err) } s.Auths[stageRegistry] = make(map[string]interface{}) s.Auths[stageRegistry]["auth"] = base64.StdEncoding.EncodeToString([]byte(rhCred)) - ps, err := json.Marshal(s) + ps, err := json.ConfigStd.Marshal(s) if err != nil { return secret, err } diff --git a/internal/cluster/validator.go b/internal/cluster/validator.go index 382c7c9853d..63abe22cd46 100644 --- a/internal/cluster/validator.go +++ b/internal/cluster/validator.go @@ -1,12 +1,12 @@ package cluster import ( - "encoding/json" "fmt" "reflect" "strings" "time" + json "github.com/bytedance/sonic" "github.com/go-openapi/strfmt" "github.com/go-openapi/swag" "github.com/hashicorp/go-multierror" @@ -616,7 +616,7 @@ func (v *clusterValidator) skipNetworkHostPrefixCheck(c *clusterPreprocessContex networkType := swag.StringValue(c.cluster.NetworkType) if c.cluster.InstallConfigOverrides != "" { // use networkType from install-config overrides if set - overrideDecoder := json.NewDecoder(strings.NewReader(c.cluster.InstallConfigOverrides)) + overrideDecoder := json.ConfigStd.NewDecoder(strings.NewReader(c.cluster.InstallConfigOverrides)) overrideDecoder.DisallowUnknownFields() cfg := &installcfg.InstallerConfigBaremetal{} diff --git a/internal/common/common.go b/internal/common/common.go index fb1bfe88b3e..eab97de8f07 100644 --- a/internal/common/common.go +++ b/internal/common/common.go @@ -2,13 +2,13 @@ package common import ( "crypto/x509" - "encoding/json" "encoding/pem" "errors" "fmt" "reflect" "strings" + json "github.com/bytedance/sonic" "github.com/docker/distribution/reference" "github.com/go-openapi/strfmt" "github.com/go-openapi/swag" diff --git a/internal/common/common_test.go b/internal/common/common_test.go index 7a10d7ee79f..8089850dc5a 100644 --- a/internal/common/common_test.go +++ b/internal/common/common_test.go @@ -2,12 +2,12 @@ package common import ( "crypto/rand" - "encoding/json" "fmt" "math" "math/big" "testing" + json "github.com/bytedance/sonic" "github.com/go-openapi/strfmt" "github.com/go-openapi/swag" "github.com/google/uuid" @@ -345,7 +345,7 @@ var _ = Describe("db features", func() { /** * This test is to ensure that our assumptions about how golang handles the serialisation of a map is consistent with how we understand this to work. -* to cover any changes in the implementation of json.Marshal that could affect the order in which items are serialized. +* to cover any changes in the implementation of json.ConfigStd.Marshal that could affect the order in which items are serialized. */ var _ = Describe("JSON serialization checks", func() { It("json serialization of a map should return a consistent string for the same entries irrespective of the order in which they were added", func() { @@ -363,7 +363,7 @@ var _ = Describe("JSON serialization checks", func() { slice = append(slice[:index], slice[index+1:]...) m[value] = value } - j, e := json.Marshal(m) + j, e := json.ConfigStd.Marshal(m) if e != nil { fmt.Println("Error") } diff --git a/internal/common/db.go b/internal/common/db.go index cca0082595a..233040ea466 100644 --- a/internal/common/db.go +++ b/internal/common/db.go @@ -1,10 +1,10 @@ package common import ( - "encoding/json" "fmt" "time" + json "github.com/bytedance/sonic" "github.com/go-openapi/strfmt" "github.com/go-openapi/swag" v1 "github.com/openshift/api/config/v1" @@ -642,7 +642,7 @@ func ConvertMirrorRegistryConfigToString(configuration *MirrorRegistryConfigurat return "", nil } - data, err := json.Marshal(configuration) + data, err := json.ConfigStd.Marshal(configuration) if err != nil { return "", fmt.Errorf("failed to marshal MirrorRegistryConfiguration: %w", err) } diff --git a/internal/common/disks_info.go b/internal/common/disks_info.go index 856d03ec5d1..cb6201c07cc 100644 --- a/internal/common/disks_info.go +++ b/internal/common/disks_info.go @@ -1,9 +1,9 @@ package common import ( - "encoding/json" "fmt" + json "github.com/bytedance/sonic" "github.com/openshift/assisted-service/models" ) @@ -21,7 +21,7 @@ func UnMarshalDisks(diskInfoStr string) (DisksInfo, error) { } func MarshalDisks(disksInfo DisksInfo) (string, error) { - b, err := json.Marshal(&disksInfo) + b, err := json.ConfigStd.Marshal(&disksInfo) if err != nil { return "", err } diff --git a/internal/common/image_status.go b/internal/common/image_status.go index 8b19b0244df..736e31be8ba 100644 --- a/internal/common/image_status.go +++ b/internal/common/image_status.go @@ -1,8 +1,7 @@ package common import ( - "encoding/json" - + json "github.com/bytedance/sonic" "github.com/openshift/assisted-service/models" ) @@ -24,7 +23,7 @@ func MarshalImageStatuses(statuses ImageStatuses) (string, error) { b []byte err error ) - if b, err = json.Marshal(&statuses); err != nil { + if b, err = json.ConfigStd.Marshal(&statuses); err != nil { return "", err } return string(b), nil diff --git a/internal/common/inventory.go b/internal/common/inventory.go index e7638765e10..8d2a1f3275a 100644 --- a/internal/common/inventory.go +++ b/internal/common/inventory.go @@ -1,13 +1,12 @@ package common import ( - "encoding/json" - + json "github.com/bytedance/sonic" "github.com/openshift/assisted-service/models" ) func MarshalInventory(inventory *models.Inventory) (string, error) { - if data, err := json.Marshal(inventory); err != nil { + if data, err := json.ConfigStd.Marshal(inventory); err != nil { return "", err } else { return string(data), nil @@ -17,7 +16,7 @@ func MarshalInventory(inventory *models.Inventory) (string, error) { func UnmarshalInventory(inventoryStr string) (*models.Inventory, error) { var inventory models.Inventory - if err := json.Unmarshal([]byte(inventoryStr), &inventory); err != nil { + if err := json.ConfigDefault.Unmarshal([]byte(inventoryStr), &inventory); err != nil { return nil, err } return &inventory, nil diff --git a/internal/common/node_labels.go b/internal/common/node_labels.go index d93b5779df7..eb7fd3bf252 100644 --- a/internal/common/node_labels.go +++ b/internal/common/node_labels.go @@ -1,8 +1,7 @@ package common import ( - "encoding/json" - + json "github.com/bytedance/sonic" "github.com/openshift/assisted-service/models" ) @@ -12,7 +11,7 @@ func MarshalNodeLabels(nodeLabelsList []*models.NodeLabelParams) (string, error) nodeLabelsMap[*nl.Key] = *nl.Value } - nodeLabelsJson, err := json.Marshal(&nodeLabelsMap) + nodeLabelsJson, err := json.ConfigStd.Marshal(&nodeLabelsMap) if err != nil { return "", err } diff --git a/internal/common/test_configuration.go b/internal/common/test_configuration.go index 8cfa058cce9..04d187e975a 100644 --- a/internal/common/test_configuration.go +++ b/internal/common/test_configuration.go @@ -1,11 +1,11 @@ package common import ( - "encoding/json" "fmt" "io" "net" + json "github.com/bytedance/sonic" "github.com/go-openapi/strfmt" . "github.com/onsi/gomega" "github.com/openshift/assisted-service/internal/constants" @@ -368,7 +368,7 @@ func GenerateTestDefaultInventory() string { Routes: TestDefaultRouteConfiguration, } - b, err := json.Marshal(inventory) + b, err := json.ConfigStd.Marshal(inventory) Expect(err).To(Not(HaveOccurred())) return string(b) } @@ -405,7 +405,7 @@ func GenerateTestInventoryWithVirtualInterface(physicalInterfaces, virtualInterf Routes: TestDefaultRouteConfiguration, } - b, err := json.Marshal(inventory) + b, err := json.ConfigStd.Marshal(inventory) Expect(err).To(Not(HaveOccurred())) return string(b) } @@ -430,7 +430,7 @@ func GenerateTest2IPv4AddressesInventory() string { Routes: TestDefaultRouteConfiguration, } - b, err := json.Marshal(inventory) + b, err := json.ConfigStd.Marshal(inventory) Expect(err).To(Not(HaveOccurred())) return string(b) } @@ -451,7 +451,7 @@ func GenerateTestIPv6Inventory() string { Routes: TestDefaultRouteConfiguration, } - b, err := json.Marshal(inventory) + b, err := json.ConfigStd.Marshal(inventory) Expect(err).To(Not(HaveOccurred())) return string(b) } @@ -502,7 +502,7 @@ func GenerateTestInventoryWithNetwork(netAddress NetAddress) string { Hostname: netAddress.Hostname, Routes: TestDefaultRouteConfiguration, } - b, err := json.Marshal(inventory) + b, err := json.ConfigStd.Marshal(inventory) Expect(err).To(Not(HaveOccurred())) return string(b) } @@ -527,7 +527,7 @@ func GenerateTestInventoryWithMutate(mutateFn func(*models.Inventory)) string { Routes: TestDefaultRouteConfiguration, } mutateFn(inventory) - b, err := json.Marshal(inventory) + b, err := json.ConfigStd.Marshal(inventory) Expect(err).To(Not(HaveOccurred())) return string(b) } @@ -544,7 +544,7 @@ func GenerateTestInventoryWithTpmVersion(tpmVersion string) string { SystemVendor: &models.SystemVendor{Manufacturer: "Red Hat", ProductName: "RHEL", SerialNumber: "3534"}, TpmVersion: tpmVersion, } - b, err := json.Marshal(inventory) + b, err := json.ConfigStd.Marshal(inventory) Expect(err).To(Not(HaveOccurred())) return string(b) } diff --git a/internal/connectivity/validator.go b/internal/connectivity/validator.go index 78307c5d57a..58e45fe9c66 100644 --- a/internal/connectivity/validator.go +++ b/internal/connectivity/validator.go @@ -1,9 +1,9 @@ package connectivity import ( - "encoding/json" "time" + json "github.com/bytedance/sonic" "github.com/openshift/assisted-service/internal/common" "github.com/openshift/assisted-service/models" "github.com/pkg/errors" diff --git a/internal/connectivity/validator_test.go b/internal/connectivity/validator_test.go index bc8319902ad..0b13b0a3996 100644 --- a/internal/connectivity/validator_test.go +++ b/internal/connectivity/validator_test.go @@ -1,9 +1,9 @@ package connectivity import ( - "encoding/json" "testing" + json "github.com/bytedance/sonic" "github.com/go-openapi/strfmt" "github.com/google/uuid" . "github.com/onsi/ginkgo" @@ -40,7 +40,7 @@ var _ = Describe("get valid interfaces", func() { }) It("valid interfaces", func() { - hw, err := json.Marshal(&inventory) + hw, err := json.ConfigStd.Marshal(&inventory) Expect(err).NotTo(HaveOccurred()) host.Inventory = string(hw) interfaces, err := connectivityValidator.GetHostValidInterfaces(host) diff --git a/internal/controller/controllers/agent_controller.go b/internal/controller/controllers/agent_controller.go index 4615928f9ac..b27422e35ac 100644 --- a/internal/controller/controllers/agent_controller.go +++ b/internal/controller/controllers/agent_controller.go @@ -18,7 +18,6 @@ package controllers import ( "context" - "encoding/json" "fmt" "net/http" "reflect" @@ -27,6 +26,7 @@ import ( "strings" "time" + json "github.com/bytedance/sonic" "github.com/go-openapi/strfmt" "github.com/go-openapi/swag" bmh_v1alpha1 "github.com/metal3-io/baremetal-operator/apis/metal3.io/v1alpha1" @@ -806,7 +806,7 @@ func (r *AgentReconciler) shouldApproveCSRsForAgent(ctx context.Context, agent * } func marshalNodeLabels(nodeLabels map[string]string) (string, error) { - b, err := json.Marshal(&nodeLabels) + b, err := json.ConfigStd.Marshal(&nodeLabels) if err != nil { return "", err } @@ -1111,7 +1111,7 @@ func (r *AgentReconciler) updateInstallerArgs(ctx context.Context, log logrus.Fi } // as we marshalling same var or []string, there is no point to verify error on marshalling it - argsBytes, _ := json.Marshal(agentSpecInstallerArgs.Args) + argsBytes, _ := json.ConfigStd.Marshal(agentSpecInstallerArgs.Args) // we need to validate if the equal one more after marshalling if string(argsBytes) == host.InstallerArgs { log.Debugf("Nothing to update, installer args were already set") diff --git a/internal/controller/controllers/agent_controller_test.go b/internal/controller/controllers/agent_controller_test.go index cc626c08aaf..c27b122bb86 100644 --- a/internal/controller/controllers/agent_controller_test.go +++ b/internal/controller/controllers/agent_controller_test.go @@ -2,12 +2,12 @@ package controllers import ( "context" - "encoding/json" "fmt" "net/http" "os" "time" + json "github.com/bytedance/sonic" "github.com/go-openapi/strfmt" "github.com/go-openapi/swag" "github.com/golang/mock/gomock" @@ -272,7 +272,7 @@ var _ = Describe("agent reconcile", func() { } var bytesValidationInfo []byte var err error - bytesValidationInfo, err = json.Marshal(validationInfo) + bytesValidationInfo, err = json.ConfigStd.Marshal(validationInfo) Expect(err).To(BeNil()) stringifiedValidationInfo := string(bytesValidationInfo) @@ -514,7 +514,7 @@ var _ = Describe("agent reconcile", func() { }) marshalLabels := func(m map[string]string) string { - b, err := json.Marshal(&m) + b, err := json.ConfigStd.Marshal(&m) Expect(err).ToNot(HaveOccurred()) return string(b) } @@ -1790,7 +1790,7 @@ var _ = Describe("agent reconcile", func() { var j []string err = json.Unmarshal([]byte(installerArgs), &j) Expect(err).To(BeNil()) - arrBytes, _ := json.Marshal(j) + arrBytes, _ := json.ConfigStd.Marshal(j) commonHost.InstallerArgs = string(arrBytes) mockInstallerInternal.EXPECT().GetHostByKubeKey(gomock.Any()).Return(commonHost, nil) mockInstallerInternal.EXPECT().GetClusterByKubeKey(gomock.Any()).Return(backEndCluster, nil) @@ -1892,7 +1892,7 @@ var _ = Describe("agent reconcile", func() { {Path: "/dev/sdb", Bootable: false, DriveType: models.DriveTypeHDD}, }, } - inv, _ := json.Marshal(&inventory) + inv, _ := json.ConfigStd.Marshal(&inventory) commonHost := &common.Host{ Host: models.Host{ ID: &hostId, @@ -1961,7 +1961,7 @@ var _ = Describe("agent reconcile", func() { SourceState: srcState, }, } - ntp, _ := json.Marshal(&ntpSources) + ntp, _ := json.ConfigStd.Marshal(&ntpSources) commonHost := &common.Host{ Host: models.Host{ ID: &hostId, @@ -2388,7 +2388,7 @@ VU1eS0RiS/Lz6HwRs2mATNY5FrpZOgdM3cI= Routes: common.TestDefaultRouteConfiguration, } - b, err := json.Marshal(inventory) + b, err := json.ConfigStd.Marshal(inventory) Expect(err).To(Not(HaveOccurred())) return string(b) } @@ -3188,7 +3188,7 @@ VU1eS0RiS/Lz6HwRs2mATNY5FrpZOgdM3cI= }, }, } - installConfigData, err := json.Marshal(installConfig) + installConfigData, err := json.ConfigStd.Marshal(installConfig) Expect(err).ToNot(HaveOccurred()) Expect(db.Model(backEndCluster).Update("install_config_overrides", installConfigData).Error).ShouldNot(HaveOccurred()) } @@ -4669,7 +4669,7 @@ var _ = Describe("Restore Host - Reconcile an Agent with missing Host", func() { } var bytesValidationInfo []byte var err error - bytesValidationInfo, err = json.Marshal(validationInfo) + bytesValidationInfo, err = json.ConfigStd.Marshal(validationInfo) Expect(err).To(BeNil()) stringifiedValidationInfo := string(bytesValidationInfo) @@ -4952,7 +4952,7 @@ var _ = Describe("createNewHost", func() { hostInventory := models.Inventory{} hostInventory.SystemVendor = &models.SystemVendor{} hostInventory.SystemVendor.Manufacturer = "RedHat" - inventory, err := json.Marshal(hostInventory) + inventory, err := json.ConfigStd.Marshal(hostInventory) Expect(err).To(BeNil()) Expect(host.Inventory).To(Equal(string(inventory))) }) diff --git a/internal/controller/controllers/agentlabel_controller.go b/internal/controller/controllers/agentlabel_controller.go index 54dc1242a01..209170c5809 100644 --- a/internal/controller/controllers/agentlabel_controller.go +++ b/internal/controller/controllers/agentlabel_controller.go @@ -18,9 +18,9 @@ package controllers import ( "context" - "encoding/json" "fmt" + json "github.com/bytedance/sonic" "github.com/itchyny/gojq" aiv1beta1 "github.com/openshift/assisted-service/api/v1beta1" logutil "github.com/openshift/assisted-service/pkg/log" @@ -65,7 +65,7 @@ func (r *AgentLabelReconciler) Reconcile(origCtx context.Context, req ctrl.Reque // Get the inventory into interfaces by way of json marshal/unmarshal var inventoryInterface interface{} - jsonInventory, _ := json.Marshal(agent.Status.Inventory) + jsonInventory, _ := json.ConfigStd.Marshal(agent.Status.Inventory) _ = json.Unmarshal(jsonInventory, &inventoryInterface) classifications := aiv1beta1.AgentClassificationList{} diff --git a/internal/controller/controllers/agentserviceconfig_controller.go b/internal/controller/controllers/agentserviceconfig_controller.go index 37a1bd31d4f..43ed8f991c7 100644 --- a/internal/controller/controllers/agentserviceconfig_controller.go +++ b/internal/controller/controllers/agentserviceconfig_controller.go @@ -19,13 +19,13 @@ package controllers import ( "context" _ "embed" - "encoding/json" "fmt" "net/url" "os" "path/filepath" "strings" + json "github.com/bytedance/sonic" certtypes "github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1" "github.com/go-openapi/swag" "github.com/hashicorp/go-version" @@ -2114,7 +2114,7 @@ func getMustGatherImages(log logrus.FieldLogger, spec *aiv1beta1.AgentServiceCon if len(mustGatherVersions) == 0 { return MustGatherImages() } - encodedVersions, err := json.Marshal(mustGatherVersions) + encodedVersions, err := json.ConfigStd.Marshal(mustGatherVersions) if err != nil { log.WithError(err).Error(fmt.Sprintf("Problem marshaling must gather images (%v) to string, returning default %v", mustGatherVersions, MustGatherImages())) return MustGatherImages() @@ -2157,7 +2157,7 @@ func getOSImages(log logrus.FieldLogger, spec *aiv1beta1.AgentServiceConfigSpec) return OSImages() } - encodedOSImages, err := json.Marshal(osImages) + encodedOSImages, err := json.ConfigStd.Marshal(osImages) if err != nil { log.WithError(err).Error(fmt.Sprintf("Problem marshaling OSImages (%v) to string, returning default %v", osImages, OSImages())) return OSImages() diff --git a/internal/controller/controllers/agentserviceconfig_controller_test.go b/internal/controller/controllers/agentserviceconfig_controller_test.go index 2e0abc8d06d..389b6e035f6 100644 --- a/internal/controller/controllers/agentserviceconfig_controller_test.go +++ b/internal/controller/controllers/agentserviceconfig_controller_test.go @@ -2,10 +2,10 @@ package controllers import ( "context" - "encoding/json" "fmt" "os" + json "github.com/bytedance/sonic" certtypes "github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1" certmeta "github.com/cert-manager/cert-manager/pkg/apis/meta/v1" "github.com/go-openapi/swag" @@ -2023,12 +2023,12 @@ var _ = Describe("getMustGatherImages", func() { }, } var spec2string = func() string { - bytes, err := json.Marshal(outSpecMustGatherImages) + bytes, err := json.ConfigStd.Marshal(outSpecMustGatherImages) Expect(err).NotTo(HaveOccurred()) return string(bytes) } var env2string = func() string { - bytes, err := json.Marshal(defaultEnvMustGatherImages) + bytes, err := json.ConfigStd.Marshal(defaultEnvMustGatherImages) Expect(err).NotTo(HaveOccurred()) return string(bytes) } @@ -2135,12 +2135,12 @@ var _ = Describe("getOSImages", func() { }, } var spec2string = func() string { - bytes, err := json.Marshal(outSpecOsImages) + bytes, err := json.ConfigStd.Marshal(outSpecOsImages) Expect(err).NotTo(HaveOccurred()) return string(bytes) } var env2string = func() string { - bytes, err := json.Marshal(defaultSpecOsImages) + bytes, err := json.ConfigStd.Marshal(defaultSpecOsImages) Expect(err).NotTo(HaveOccurred()) return string(bytes) } @@ -2596,7 +2596,7 @@ func newASCWithOSImages() (*aiv1beta1.AgentServiceConfig, string) { }, } - encoded, _ := json.Marshal(models.OsImages{ + encoded, _ := json.ConfigStd.Marshal(models.OsImages{ &models.OsImage{ CPUArchitecture: swag.String(""), OpenshiftVersion: swag.String("4.8"), @@ -2629,7 +2629,7 @@ func newASCWithMultipleOpenshiftVersions() (*aiv1beta1.AgentServiceConfig, strin }, } - encoded, _ := json.Marshal(models.OsImages{ + encoded, _ := json.ConfigStd.Marshal(models.OsImages{ &models.OsImage{ CPUArchitecture: swag.String(""), OpenshiftVersion: swag.String("4.7"), @@ -2667,7 +2667,7 @@ func newASCWithDuplicateOpenshiftVersions() (*aiv1beta1.AgentServiceConfig, stri }, } - encoded, _ := json.Marshal(models.OsImages{ + encoded, _ := json.ConfigStd.Marshal(models.OsImages{ &models.OsImage{ CPUArchitecture: swag.String(""), OpenshiftVersion: swag.String("4.7"), @@ -2702,7 +2702,7 @@ func newASCWithLongOpenshiftVersion() (*aiv1beta1.AgentServiceConfig, string) { }, } - encoded, _ := json.Marshal(models.OsImages{ + encoded, _ := json.ConfigStd.Marshal(models.OsImages{ &models.OsImage{ CPUArchitecture: swag.String(""), OpenshiftVersion: swag.String("4.8.0"), diff --git a/internal/controller/controllers/bmh_agent_controller.go b/internal/controller/controllers/bmh_agent_controller.go index ccb2328be48..25827ea7926 100644 --- a/internal/controller/controllers/bmh_agent_controller.go +++ b/internal/controller/controllers/bmh_agent_controller.go @@ -20,7 +20,6 @@ import ( "bytes" "context" "encoding/base64" - "encoding/json" "fmt" "reflect" "sort" @@ -28,6 +27,7 @@ import ( "text/template" "time" + json "github.com/bytedance/sonic" bmh_v1alpha1 "github.com/metal3-io/baremetal-operator/apis/metal3.io/v1alpha1" machinev1beta1 "github.com/openshift/api/machine/v1beta1" aiv1beta1 "github.com/openshift/assisted-service/api/v1beta1" @@ -611,7 +611,7 @@ func (r *BMACReconciler) detachedValue(bmh *bmh_v1alpha1.BareMetalHost) (string, if _, has_annotation := bmh.GetAnnotations()[BMH_DELETE_ANNOTATION]; has_annotation && r.ConvergedFlowEnabled { arg := bmh_v1alpha1.DetachedAnnotationArguments{DeleteAction: bmh_v1alpha1.DetachedDeleteActionDelay} var err error - detachValue, err = json.Marshal(arg) + detachValue, err = json.ConfigStd.Marshal(arg) if err != nil { return "", err } @@ -763,7 +763,7 @@ func (r *BMACReconciler) reconcileAgentInventory(log logrus.FieldLogger, bmh *bm hardwareDetails.Hostname = inventory.Hostname } - bytes, err := json.Marshal(hardwareDetails) + bytes, err := json.ConfigStd.Marshal(hardwareDetails) if err != nil { return reconcileError{err: err} } @@ -1934,7 +1934,7 @@ func (r *BMACReconciler) addBMHStatusAndPausedAnnotations(log logrus.FieldLogger } // Convert BMH status to a JSON string - statusJson, err := json.Marshal(bmh.Status) + statusJson, err := json.ConfigStd.Marshal(bmh.Status) if err != nil { return reconcileError{err: err} } diff --git a/internal/controller/controllers/bmh_agent_controller_test.go b/internal/controller/controllers/bmh_agent_controller_test.go index ff662e69101..91c463dd7e1 100644 --- a/internal/controller/controllers/bmh_agent_controller_test.go +++ b/internal/controller/controllers/bmh_agent_controller_test.go @@ -2,10 +2,10 @@ package controllers import ( "context" - "encoding/json" "fmt" "time" + json "github.com/bytedance/sonic" "github.com/go-openapi/strfmt" "github.com/go-openapi/swag" "github.com/golang/mock/gomock" @@ -1731,7 +1731,7 @@ var _ = Describe("bmac reconcile", func() { }, }, } - installConfigData, err := json.Marshal(installConfig) + installConfigData, err := json.ConfigStd.Marshal(installConfig) Expect(err).ToNot(HaveOccurred()) Expect(db.Model(&dbCluster).Update("install_config_overrides", installConfigData).Error).ShouldNot(HaveOccurred()) @@ -2127,7 +2127,7 @@ var _ = Describe("bmac reconcile", func() { Expect(updatedBMH.ObjectMeta.Annotations[BMH_PAUSED_ANNOTATION]).To(Equal("assisted-service-controller")) // Ensure 'status' annotation is added - statusJson, err := json.Marshal(updatedBMH.Status) + statusJson, err := json.ConfigStd.Marshal(updatedBMH.Status) Expect(err).To(BeNil()) Expect(updatedBMH.ObjectMeta.Annotations).To(HaveKey(BMH_STATUS_ANNOTATION)) Expect(updatedBMH.ObjectMeta.Annotations[BMH_STATUS_ANNOTATION]).To(Equal(string(statusJson))) @@ -2142,7 +2142,7 @@ var _ = Describe("bmac reconcile", func() { It("should update status annotation if stale", func() { // Create BMH bmh.ObjectMeta.Annotations[BMH_PAUSED_ANNOTATION] = "assisted-service-controller" - originalStatusJson, err := json.Marshal(bmh.Status) + originalStatusJson, err := json.ConfigStd.Marshal(bmh.Status) Expect(err).To(BeNil()) bmh.ObjectMeta.Annotations[BMH_STATUS_ANNOTATION] = string(originalStatusJson) Expect(c.Create(ctx, bmh)).To(Succeed()) @@ -2169,7 +2169,7 @@ var _ = Describe("bmac reconcile", func() { Expect(err).To(BeNil()) // Ensure 'status' annotation is updated - updatedStatusJson, err := json.Marshal(updatedBMH.Status) + updatedStatusJson, err := json.ConfigStd.Marshal(updatedBMH.Status) Expect(err).To(BeNil()) Expect(updatedStatusJson).To(Not(Equal(originalStatusJson))) Expect(updatedBMH.ObjectMeta.Annotations[BMH_STATUS_ANNOTATION]).To(Equal(string(updatedStatusJson))) @@ -2184,7 +2184,7 @@ var _ = Describe("bmac reconcile", func() { It("should keep paused and status annotations if already exist", func() { // Create BMH bmh.ObjectMeta.Annotations[BMH_PAUSED_ANNOTATION] = "assisted-service-controller" - statusJson, err := json.Marshal(bmh.Status) + statusJson, err := json.ConfigStd.Marshal(bmh.Status) Expect(err).To(BeNil()) bmh.ObjectMeta.Annotations[BMH_STATUS_ANNOTATION] = string(statusJson) Expect(c.Create(ctx, bmh)).To(Succeed()) @@ -2204,7 +2204,7 @@ var _ = Describe("bmac reconcile", func() { Expect(updatedBMH.ObjectMeta.Annotations[BMH_PAUSED_ANNOTATION]).To(Equal("assisted-service-controller")) // Ensure 'status' annotation exists - statusJson, err = json.Marshal(updatedBMH.Status) + statusJson, err = json.ConfigStd.Marshal(updatedBMH.Status) Expect(err).To(BeNil()) Expect(updatedBMH.ObjectMeta.Annotations).To(HaveKey(BMH_STATUS_ANNOTATION)) Expect(updatedBMH.ObjectMeta.Annotations[BMH_STATUS_ANNOTATION]).To(Equal(string(statusJson))) @@ -2225,7 +2225,7 @@ var _ = Describe("bmac reconcile", func() { bmh.ObjectMeta.Annotations[BMH_PAUSED_ANNOTATION] = "assisted-service-controller" // Add 'status' annotation - originalStatusJson, err := json.Marshal(bmh.Status) + originalStatusJson, err := json.ConfigStd.Marshal(bmh.Status) Expect(err).To(BeNil()) bmh.ObjectMeta.Annotations[BMH_STATUS_ANNOTATION] = string(originalStatusJson) @@ -2280,7 +2280,7 @@ var _ = Describe("bmac reconcile", func() { It("should remove paused annotation if also status annotation exists", func() { // Create BMH bmh.ObjectMeta.Annotations[BMH_PAUSED_ANNOTATION] = "assisted-service-controller" - statusJson, err := json.Marshal(bmh.Status) + statusJson, err := json.ConfigStd.Marshal(bmh.Status) Expect(err).To(BeNil()) bmh.ObjectMeta.Annotations[BMH_STATUS_ANNOTATION] = string(statusJson) Expect(c.Create(ctx, bmh)).To(Succeed()) @@ -2475,7 +2475,7 @@ var _ = Describe("bmac reconcile - converged flow enabled", func() { args := &bmh_v1alpha1.DetachedAnnotationArguments{ DeleteAction: bmh_v1alpha1.DetachedDeleteActionDelay, } - data, err := json.Marshal(args) + data, err := json.ConfigStd.Marshal(args) Expect(err).To(BeNil()) host.Annotations = map[string]string{BMH_DETACHED_ANNOTATION: string(data)} Expect(c.Update(ctx, host)).To(Succeed()) @@ -2724,7 +2724,7 @@ var _ = Describe("handleBMHFinalizer", func() { args := &bmh_v1alpha1.DetachedAnnotationArguments{ DeleteAction: bmh_v1alpha1.DetachedDeleteActionDelay, } - data, err := json.Marshal(args) + data, err := json.ConfigStd.Marshal(args) Expect(err).To(BeNil()) setAnnotation(&bmh.ObjectMeta, BMH_DETACHED_ANNOTATION, string(data)) setAnnotation(&bmh.ObjectMeta, BMH_PAUSED_ANNOTATION, "assisted-service-controller") @@ -2742,7 +2742,7 @@ var _ = Describe("handleBMHFinalizer", func() { args := &bmh_v1alpha1.DetachedAnnotationArguments{ DeleteAction: bmh_v1alpha1.DetachedDeleteActionDelay, } - data, err := json.Marshal(args) + data, err := json.ConfigStd.Marshal(args) Expect(err).To(BeNil()) setAnnotation(&bmh.ObjectMeta, BMH_DETACHED_ANNOTATION, string(data)) setAnnotation(&bmh.ObjectMeta, BMH_PAUSED_ANNOTATION, "assisted-service-controller") diff --git a/internal/controller/controllers/clusterdeployments_controller.go b/internal/controller/controllers/clusterdeployments_controller.go index d233c03177f..dc9bff757d2 100644 --- a/internal/controller/controllers/clusterdeployments_controller.go +++ b/internal/controller/controllers/clusterdeployments_controller.go @@ -19,7 +19,6 @@ package controllers import ( "context" "encoding/base64" - "encoding/json" errpkg "errors" "fmt" "io" @@ -29,6 +28,7 @@ import ( "strings" "time" + json "github.com/bytedance/sonic" "github.com/go-openapi/strfmt" "github.com/go-openapi/swag" "github.com/google/uuid" diff --git a/internal/controller/controllers/clusterdeployments_controller_test.go b/internal/controller/controllers/clusterdeployments_controller_test.go index 2cbba760e68..a60a63e0f66 100644 --- a/internal/controller/controllers/clusterdeployments_controller_test.go +++ b/internal/controller/controllers/clusterdeployments_controller_test.go @@ -3,7 +3,6 @@ package controllers import ( "context" "encoding/base64" - "encoding/json" "fmt" "io" "net/http" @@ -11,6 +10,7 @@ import ( "strings" "time" + json "github.com/bytedance/sonic" "github.com/go-openapi/strfmt" "github.com/go-openapi/swag" "github.com/golang/mock/gomock" @@ -1322,7 +1322,7 @@ var _ = Describe("cluster reconcile", func() { } var bytesValidationInfo []byte var err error - bytesValidationInfo, err = json.Marshal(validationInfo) + bytesValidationInfo, err = json.ConfigStd.Marshal(validationInfo) Expect(err).To(BeNil()) stringifiedValidationInfo := string(bytesValidationInfo) sId := strfmt.UUID(uuid.New().String()) diff --git a/internal/controller/controllers/infraenv_controller.go b/internal/controller/controllers/infraenv_controller.go index d7749f1a402..7754c276078 100644 --- a/internal/controller/controllers/infraenv_controller.go +++ b/internal/controller/controllers/infraenv_controller.go @@ -18,7 +18,6 @@ package controllers import ( "context" - "encoding/json" "fmt" "net/http" "net/url" @@ -27,6 +26,7 @@ import ( "strings" "time" + json "github.com/bytedance/sonic" "github.com/go-openapi/strfmt" "github.com/go-openapi/swag" aiv1beta1 "github.com/openshift/assisted-service/api/v1beta1" diff --git a/internal/controller/controllers/infraenv_controller_test.go b/internal/controller/controllers/infraenv_controller_test.go index 6ee5209c875..15aa7b3700d 100644 --- a/internal/controller/controllers/infraenv_controller_test.go +++ b/internal/controller/controllers/infraenv_controller_test.go @@ -2,13 +2,13 @@ package controllers import ( "context" - "encoding/json" "fmt" "net/http" "net/url" "os" "time" + json "github.com/bytedance/sonic" "github.com/go-openapi/strfmt" "github.com/go-openapi/swag" "github.com/golang/mock/gomock" @@ -423,7 +423,7 @@ var _ = Describe("infraEnv reconcile", func() { if len(internalKargs) == 0 { return nil } - b, err := json.Marshal(&internalKargs) + b, err := json.ConfigStd.Marshal(&internalKargs) Expect(err).ToNot(HaveOccurred()) return swag.String(string(b)) } diff --git a/internal/controller/controllers/spoke_client_cache.go b/internal/controller/controllers/spoke_client_cache.go index b58b80a8ca0..e0c89140f39 100644 --- a/internal/controller/controllers/spoke_client_cache.go +++ b/internal/controller/controllers/spoke_client_cache.go @@ -2,10 +2,10 @@ package controllers import ( "crypto/sha256" - "encoding/json" "fmt" "sync" + json "github.com/bytedance/sonic" "github.com/openshift/assisted-service/internal/spoke_k8s_client" hivev1 "github.com/openshift/hive/apis/hive/v1" "github.com/pkg/errors" @@ -68,7 +68,7 @@ func (c *spokeClientCache) Get(clusterDeployment *hivev1.ClusterDeployment, } func (c *spokeClientCache) calculateSecretHash(secret *corev1.Secret) (string, error) { - data, err := json.Marshal(secret.Data) + data, err := json.ConfigStd.Marshal(secret.Data) if err != nil { return "", err } diff --git a/internal/events/event.go b/internal/events/event.go index 5d062f482c0..9c6546ed762 100644 --- a/internal/events/event.go +++ b/internal/events/event.go @@ -3,12 +3,12 @@ package events import ( "context" "database/sql" - "encoding/json" "errors" "fmt" "strings" "time" + json "github.com/bytedance/sonic" "github.com/go-openapi/strfmt" "github.com/go-openapi/swag" "github.com/openshift/assisted-service/internal/common" @@ -512,7 +512,7 @@ func toProps(attrs ...interface{}) (result string, err error) { if len(props) > 0 { var b []byte - if b, err = json.Marshal(props); err == nil { + if b, err = json.ConfigStd.Marshal(props); err == nil { return string(b), nil } } diff --git a/internal/events/event_test.go b/internal/events/event_test.go index 592cf49e1f5..bf87ebd569d 100644 --- a/internal/events/event_test.go +++ b/internal/events/event_test.go @@ -2,12 +2,12 @@ package events import ( "context" - "encoding/json" "fmt" "net/http" "testing" "time" + json "github.com/bytedance/sonic" "github.com/go-openapi/strfmt" "github.com/go-openapi/swag" "github.com/golang/mock/gomock" @@ -77,7 +77,7 @@ var _ = Describe("Add event", func() { Expect(time.Time(*dbEvent.EventTime)).To(BeTemporally("~", time.Now(), 100*time.Millisecond)) } marshal := func(v interface{}) string { - b, err := json.Marshal(v) + b, err := json.ConfigStd.Marshal(v) Expect(err).ToNot(HaveOccurred()) return string(b) } diff --git a/internal/events/events_api.go b/internal/events/events_api.go index f4841aee867..e8a1365370d 100644 --- a/internal/events/events_api.go +++ b/internal/events/events_api.go @@ -2,10 +2,10 @@ package events import ( "context" - "encoding/json" "net/http" "time" + json "github.com/bytedance/sonic" "github.com/go-openapi/runtime/middleware" "github.com/go-openapi/swag" "github.com/openshift/assisted-service/internal/common" diff --git a/internal/hardware/validator.go b/internal/hardware/validator.go index b9cb55dab09..21c635e66df 100644 --- a/internal/hardware/validator.go +++ b/internal/hardware/validator.go @@ -2,7 +2,6 @@ package hardware import ( "context" - "encoding/json" "fmt" "math" "net/netip" @@ -11,6 +10,7 @@ import ( "strings" "time" + json "github.com/bytedance/sonic" "github.com/dustin/go-humanize" "github.com/go-openapi/swag" "github.com/openshift/assisted-service/internal/common" diff --git a/internal/hardware/validator_test.go b/internal/hardware/validator_test.go index 44c74e80b8b..d428147bda8 100644 --- a/internal/hardware/validator_test.go +++ b/internal/hardware/validator_test.go @@ -2,7 +2,6 @@ package hardware import ( "context" - "encoding/json" "errors" "fmt" "math" @@ -11,6 +10,7 @@ import ( "testing" "github.com/alecthomas/units" + json "github.com/bytedance/sonic" "github.com/go-openapi/strfmt" "github.com/go-openapi/swag" "github.com/golang/mock/gomock" @@ -695,7 +695,7 @@ var _ = Describe("hardware_validator", func() { {DriveType: models.DriveTypeHDD, Name: "sdp", SizeBytes: validDiskSize + 1, InstallationEligibility: eligible, Hctl: "0:3:1:2"}, {DriveType: models.DriveTypeHDD, Name: "sdz", SizeBytes: validDiskSize + 10, InstallationEligibility: eligible, Hctl: "0:3:1:3"}, } - hw, err := json.Marshal(&inventory) + hw, err := json.ConfigStd.Marshal(&inventory) Expect(err).NotTo(HaveOccurred()) host1.Inventory = string(hw) disks, err := hwvalidator.GetHostValidDisks(host1) @@ -728,7 +728,7 @@ var _ = Describe("hardware_validator", func() { InstallationEligibility: models.DiskInstallationEligibility{Eligible: true}, }, } - hw, err := json.Marshal(&inventory) + hw, err := json.ConfigStd.Marshal(&inventory) Expect(err).NotTo(HaveOccurred()) host1.Inventory = string(hw) disks, err := hwvalidator.GetHostValidDisks(host1) @@ -854,7 +854,7 @@ var _ = Describe("Cluster host requirements", func() { {Name: operatorName2, ClusterID: clusterID}, }, }} - versionRequirements, err := json.Marshal(versionRequirementsSource) + versionRequirements, err := json.ConfigStd.Marshal(versionRequirementsSource) Expect(err).ToNot(HaveOccurred()) _ = os.Setenv(prefixedRequirementsEnv, string(versionRequirements)) _ = os.Setenv("EDGE_WORKERS_PRODUCT_NAMES", "test, BlueField SoC,marvell") @@ -980,7 +980,7 @@ var _ = Describe("Cluster host requirements", func() { CPU: &models.CPU{Count: 2, Architecture: common.AARCH64CPUArchitecture}, SystemVendor: &models.SystemVendor{ProductName: "blueField SoC"}, } - hw, err := json.Marshal(&inventory) + hw, err := json.ConfigStd.Marshal(&inventory) Expect(err).NotTo(HaveOccurred()) host = &models.Host{ID: &id1, ClusterID: cluster.ID, Role: role, Inventory: string(hw)} @@ -1004,7 +1004,7 @@ var _ = Describe("Cluster host requirements", func() { CPU: &models.CPU{Count: 2, Architecture: common.AARCH64CPUArchitecture}, SystemVendor: &models.SystemVendor{ProductName: "blueField SoC"}, } - hw, err := json.Marshal(&inventory) + hw, err := json.ConfigStd.Marshal(&inventory) Expect(err).NotTo(HaveOccurred()) host = &models.Host{ID: &id1, ClusterID: cluster.ID, Role: role, Inventory: string(hw)} @@ -1028,7 +1028,7 @@ var _ = Describe("Cluster host requirements", func() { CPU: &models.CPU{Count: 2, Architecture: common.DefaultCPUArchitecture}, SystemVendor: &models.SystemVendor{ProductName: "blueField SoC"}, } - hw, err := json.Marshal(&inventory) + hw, err := json.ConfigStd.Marshal(&inventory) Expect(err).NotTo(HaveOccurred()) host = &models.Host{ID: &id1, ClusterID: cluster.ID, Role: role, Inventory: string(hw)} @@ -1052,7 +1052,7 @@ var _ = Describe("Cluster host requirements", func() { CPU: &models.CPU{Count: 2, Architecture: common.DefaultCPUArchitecture}, SystemVendor: &models.SystemVendor{ProductName: "ding dong SoC"}, } - hw, err := json.Marshal(&inventory) + hw, err := json.ConfigStd.Marshal(&inventory) Expect(err).NotTo(HaveOccurred()) host = &models.Host{ID: &id1, ClusterID: cluster.ID, Role: role, Inventory: string(hw)} @@ -1076,7 +1076,7 @@ var _ = Describe("Cluster host requirements", func() { CPU: &models.CPU{Count: 2, Architecture: common.AARCH64CPUArchitecture}, SystemVendor: &models.SystemVendor{ProductName: "bluefield soc "}, } - hw, err := json.Marshal(&inventory) + hw, err := json.ConfigStd.Marshal(&inventory) Expect(err).NotTo(HaveOccurred()) host = &models.Host{ID: &id1, ClusterID: cluster.ID, Role: role, Inventory: string(hw)} diff --git a/internal/hardware/versioned-requirements.go b/internal/hardware/versioned-requirements.go index c6ddf0fc521..028ce90ec36 100644 --- a/internal/hardware/versioned-requirements.go +++ b/internal/hardware/versioned-requirements.go @@ -1,9 +1,9 @@ package hardware import ( - "encoding/json" "fmt" + json "github.com/bytedance/sonic" "github.com/openshift/assisted-service/models" ) diff --git a/internal/hardware/versioned-requirements_test.go b/internal/hardware/versioned-requirements_test.go index 43291bb88b8..b8dcb89f4de 100644 --- a/internal/hardware/versioned-requirements_test.go +++ b/internal/hardware/versioned-requirements_test.go @@ -1,9 +1,9 @@ package hardware import ( - "encoding/json" "os" + json "github.com/bytedance/sonic" "github.com/kelseyhightower/envconfig" . "github.com/onsi/ginkgo" "github.com/onsi/ginkgo/extensions/table" @@ -448,7 +448,7 @@ var _ = Describe("Versioned Requirements", func() { }) func configureRequirements(jsonSpec []map[string]interface{}) (*ValidatorCfg, error) { - jsonData, err := json.Marshal(jsonSpec) + jsonData, err := json.ConfigStd.Marshal(jsonSpec) if err != nil { return nil, err } diff --git a/internal/host/host.go b/internal/host/host.go index 885a29609ab..f92738b5eba 100644 --- a/internal/host/host.go +++ b/internal/host/host.go @@ -2,7 +2,6 @@ package host import ( "context" - "encoding/json" "fmt" "math" "net/http" @@ -11,6 +10,7 @@ import ( "strings" "time" + json "github.com/bytedance/sonic" "github.com/filanov/stateswitch" "github.com/go-openapi/strfmt" "github.com/go-openapi/swag" @@ -863,7 +863,7 @@ func (m *Manager) UpdateNodeSkipDiskFormatting(ctx context.Context, h *models.Ho } func (m *Manager) UpdateNTP(ctx context.Context, h *models.Host, ntpSources []*models.NtpSource, db *gorm.DB) error { - bytes, err := json.Marshal(ntpSources) + bytes, err := json.ConfigStd.Marshal(ntpSources) if err != nil { return errors.Wrapf(err, "Failed to marshal NTP sources for host %s", h.ID.String()) } @@ -877,7 +877,7 @@ func (m *Manager) UpdateNTP(ctx context.Context, h *models.Host, ntpSources []*m } func (m *Manager) UpdateDomainNameResolution(ctx context.Context, h *models.Host, domainResolutionResponse models.DomainResolutionResponse, db *gorm.DB) error { - response, err := json.Marshal(domainResolutionResponse) + response, err := json.ConfigStd.Marshal(domainResolutionResponse) if err != nil { return errors.Wrapf(err, "Failed to marshal domain name resolution for host %s", h.ID.String()) } @@ -1198,7 +1198,7 @@ func (m *Manager) didValidationChanged(ctx context.Context, newValidationRes, cu } func (m *Manager) updateValidationsInDB(ctx context.Context, db *gorm.DB, h *models.Host, newValidationRes ValidationsStatus) (*common.Host, error) { - b, err := json.Marshal(newValidationRes) + b, err := json.ConfigStd.Marshal(newValidationRes) if err != nil { return nil, err } diff --git a/internal/host/host_test.go b/internal/host/host_test.go index 73bd7ba335f..de33738dbc3 100644 --- a/internal/host/host_test.go +++ b/internal/host/host_test.go @@ -2,7 +2,6 @@ package host import ( "context" - "encoding/json" "errors" "fmt" "math" @@ -11,6 +10,7 @@ import ( "strconv" "time" + json "github.com/bytedance/sonic" "github.com/go-openapi/strfmt" "github.com/go-openapi/swag" "github.com/golang/mock/gomock" @@ -1043,7 +1043,7 @@ func insufficientHWInventory() string { SystemVendor: &models.SystemVendor{Manufacturer: "Red Hat", ProductName: "RHEL", SerialNumber: "3534"}, Routes: common.TestDefaultRouteConfiguration, } - b, err := json.Marshal(&inventory) + b, err := json.ConfigStd.Marshal(&inventory) Expect(err).To(Not(HaveOccurred())) return string(b) } @@ -1070,7 +1070,7 @@ func inventoryWithUnauthorizedVendor() string { SystemVendor: &models.SystemVendor{Manufacturer: "RDO", ProductName: "OpenStack Compute", SerialNumber: "3534"}, Routes: common.TestDefaultRouteConfiguration, } - b, err := json.Marshal(&inventory) + b, err := json.ConfigStd.Marshal(&inventory) Expect(err).To(Not(HaveOccurred())) return string(b) } @@ -1097,7 +1097,7 @@ func workerInventory() string { SystemVendor: &models.SystemVendor{Manufacturer: "Red Hat", ProductName: "RHEL", SerialNumber: "3534"}, Routes: common.TestDefaultRouteConfiguration, } - b, err := json.Marshal(&inventory) + b, err := json.ConfigStd.Marshal(&inventory) Expect(err).To(Not(HaveOccurred())) return string(b) } @@ -1251,7 +1251,7 @@ var _ = Describe("UpdateInventory", func() { }, }} - _, err := json.Marshal(&testInventory) + _, err := json.ConfigStd.Marshal(&testInventory) Expect(err).ToNot(HaveOccurred()) expectedDisks := []*models.Disk{ @@ -2284,7 +2284,7 @@ var _ = Describe("UpdateNTP", func() { h := hostutil.GetHostFromDB(*host.ID, host.InfraEnvID, db) - marshalled, err := json.Marshal(t.ntpSources) + marshalled, err := json.ConfigStd.Marshal(t.ntpSources) Expect(err).ShouldNot(HaveOccurred()) Expect(h.NtpSources).Should(Equal(string(marshalled))) @@ -2735,7 +2735,7 @@ var _ = Describe("UpdateImageStatus", func() { expectedImage.SizeBytes = t.originalImageStatuses[common.TestDefaultConfig.ImageName].SizeBytes expectedImage.Time = t.originalImageStatuses[common.TestDefaultConfig.ImageName].Time - bytes, err := json.Marshal(t.originalImageStatuses) + bytes, err := json.ConfigStd.Marshal(t.originalImageStatuses) Expect(err).ShouldNot(HaveOccurred()) Expect(bytes).ShouldNot(BeNil()) host.ImagesStatus = string(bytes) @@ -3181,7 +3181,7 @@ var _ = Describe("Validation metrics and events", func() { h := hostutil.GenerateTestHost(hostID, infraEnvID, clusterID, models.HostStatusInsufficient) validationRes := generateTestValidationResult(ValidationFailure) - bytes, err := json.Marshal(validationRes) + bytes, err := json.ConfigStd.Marshal(validationRes) Expect(err).ToNot(HaveOccurred()) h.ValidationsInfo = string(bytes) @@ -4148,7 +4148,7 @@ var _ = Describe("sortHost by hardware", func() { return d }).([]*models.Disk), } - b, _ := json.Marshal(inventory) + b, _ := json.ConfigStd.Marshal(inventory) id := strfmt.UUID(uuid.New().String()) hosts = append(hosts, &models.Host{ ID: &id, diff --git a/internal/host/hostcommands/api_vip_connectivity_check_cmd.go b/internal/host/hostcommands/api_vip_connectivity_check_cmd.go index 6d7589475ee..0eca9244beb 100644 --- a/internal/host/hostcommands/api_vip_connectivity_check_cmd.go +++ b/internal/host/hostcommands/api_vip_connectivity_check_cmd.go @@ -2,9 +2,9 @@ package hostcommands import ( "context" - "encoding/json" "fmt" + json "github.com/bytedance/sonic" "github.com/openshift/assisted-service/internal/common" "github.com/openshift/assisted-service/internal/host/hostutil" "github.com/openshift/assisted-service/models" @@ -66,7 +66,7 @@ func (c *apivipConnectivityCheckCmd) GetSteps(ctx context.Context, host *models. } request.RequestHeaders = requestHeaders - requestBytes, err := json.Marshal(request) + requestBytes, err := json.ConfigStd.Marshal(request) if err != nil { c.log.WithError(err).Errorf("failed to marshal APIVipConnectivityRequest") return nil, err diff --git a/internal/host/hostcommands/connectivity_check_convertor.go b/internal/host/hostcommands/connectivity_check_convertor.go index a8067f7fc12..43ffe18d5c8 100644 --- a/internal/host/hostcommands/connectivity_check_convertor.go +++ b/internal/host/hostcommands/connectivity_check_convertor.go @@ -1,9 +1,9 @@ package hostcommands import ( - "encoding/json" "strings" + json "github.com/bytedance/sonic" "github.com/go-openapi/strfmt" "github.com/go-openapi/swag" "github.com/openshift/assisted-service/internal/connectivity" @@ -33,7 +33,7 @@ func convertHostsToConnectivityCheckParams(currentHostId *strfmt.UUID, hosts []* if len(connectivityCheckHosts) == 0 { return "", nil } - jsonData, err := json.Marshal(connectivityCheckHosts) + jsonData, err := json.ConfigStd.Marshal(connectivityCheckHosts) return string(jsonData), err } diff --git a/internal/host/hostcommands/container_image_availability_cmd.go b/internal/host/hostcommands/container_image_availability_cmd.go index 2a643d783e4..d7368086a0e 100644 --- a/internal/host/hostcommands/container_image_availability_cmd.go +++ b/internal/host/hostcommands/container_image_availability_cmd.go @@ -2,8 +2,8 @@ package hostcommands import ( "context" - "encoding/json" + json "github.com/bytedance/sonic" "github.com/openshift/assisted-service/internal/common" "github.com/openshift/assisted-service/internal/oc" "github.com/openshift/assisted-service/internal/versions" @@ -88,7 +88,7 @@ func (cmd *imageAvailabilityCmd) prepareParam(ctx context.Context, host *models. Timeout: int64(cmd.timeoutSeconds), } - b, err := json.Marshal(&request) + b, err := json.ConfigStd.Marshal(&request) if err != nil { cmd.log.WithError(err).Errorf("Failed to JSON marshal %+v", request) return "", err diff --git a/internal/host/hostcommands/container_image_availability_cmd_test.go b/internal/host/hostcommands/container_image_availability_cmd_test.go index 2b0b21b76b3..75827d1181e 100644 --- a/internal/host/hostcommands/container_image_availability_cmd_test.go +++ b/internal/host/hostcommands/container_image_availability_cmd_test.go @@ -2,10 +2,10 @@ package hostcommands import ( "context" - "encoding/json" "errors" "strings" + json "github.com/bytedance/sonic" "github.com/go-openapi/strfmt" "github.com/golang/mock/gomock" "github.com/google/uuid" @@ -69,7 +69,7 @@ var _ = Describe("container_image_availability_cmd", func() { Timeout: defaultImageAvailabilityTimeoutSeconds, } - b, err := json.Marshal(&request) + b, err := json.ConfigStd.Marshal(&request) Expect(err).ShouldNot(HaveOccurred()) Expect(strings.Join(step[0].Args, " ")).To(ContainSubstring(string(b))) }) diff --git a/internal/host/hostcommands/dhcp_allocate_cmd.go b/internal/host/hostcommands/dhcp_allocate_cmd.go index 67d0299c960..fd98be270f0 100644 --- a/internal/host/hostcommands/dhcp_allocate_cmd.go +++ b/internal/host/hostcommands/dhcp_allocate_cmd.go @@ -2,8 +2,8 @@ package hostcommands import ( "context" - "encoding/json" + json "github.com/bytedance/sonic" "github.com/go-openapi/strfmt" "github.com/go-openapi/swag" "github.com/openshift/assisted-service/internal/common" @@ -46,7 +46,7 @@ func (f *dhcpAllocateCmd) prepareParam(host *models.Host, cluster *common.Cluste IngressVipLease: cluster.IngressVipLease, Interface: swag.String(nic), } - b, err := json.Marshal(&request) + b, err := json.ConfigStd.Marshal(&request) if err != nil { f.log.WithError(err).Warn("Json marshal") return "", err diff --git a/internal/host/hostcommands/dhcp_allocate_cmd_test.go b/internal/host/hostcommands/dhcp_allocate_cmd_test.go index aecdab0db67..c5270a189fa 100644 --- a/internal/host/hostcommands/dhcp_allocate_cmd_test.go +++ b/internal/host/hostcommands/dhcp_allocate_cmd_test.go @@ -2,8 +2,8 @@ package hostcommands import ( "context" - "encoding/json" + json "github.com/bytedance/sonic" "github.com/go-openapi/strfmt" "github.com/go-openapi/swag" . "github.com/onsi/ginkgo" diff --git a/internal/host/hostcommands/disk_performance_cmd.go b/internal/host/hostcommands/disk_performance_cmd.go index 16cc3719249..4671b9ba583 100644 --- a/internal/host/hostcommands/disk_performance_cmd.go +++ b/internal/host/hostcommands/disk_performance_cmd.go @@ -2,9 +2,9 @@ package hostcommands import ( "context" - "encoding/json" "fmt" + json "github.com/bytedance/sonic" "github.com/go-openapi/swag" "github.com/openshift/assisted-service/internal/common" "github.com/openshift/assisted-service/internal/hardware" @@ -70,7 +70,7 @@ func (c *diskPerfCheckCmd) GetArgs(bootDevice string) ([]string, error) { request := models.DiskSpeedCheckRequest{ Path: swag.String(bootDevice), } - requestBytes, err := json.Marshal(request) + requestBytes, err := json.ConfigStd.Marshal(request) if err != nil { c.log.WithError(err).Errorf("failed to marshal FioPerfCheckRequest") return nil, err diff --git a/internal/host/hostcommands/disk_performance_cmd_test.go b/internal/host/hostcommands/disk_performance_cmd_test.go index 6fe74bea695..2ba17b602e7 100644 --- a/internal/host/hostcommands/disk_performance_cmd_test.go +++ b/internal/host/hostcommands/disk_performance_cmd_test.go @@ -2,8 +2,8 @@ package hostcommands import ( "context" - "encoding/json" + json "github.com/bytedance/sonic" "github.com/go-openapi/strfmt" "github.com/golang/mock/gomock" "github.com/google/uuid" @@ -59,7 +59,7 @@ var _ = Describe("disk_performance", func() { It("returns no steps when boot device is persistent", func() { inventory := &models.Inventory{Boot: &models.Boot{DeviceType: models.BootDeviceTypePersistent}} - invBytes, err := json.Marshal(inventory) + invBytes, err := json.ConfigStd.Marshal(inventory) Expect(err).NotTo(HaveOccurred()) host.Inventory = string(invBytes) steps, err := dCmd.GetSteps(ctx, &host) diff --git a/internal/host/hostcommands/domain_name_resolution_cmd.go b/internal/host/hostcommands/domain_name_resolution_cmd.go index 44e80bc3e9a..0c614719297 100644 --- a/internal/host/hostcommands/domain_name_resolution_cmd.go +++ b/internal/host/hostcommands/domain_name_resolution_cmd.go @@ -2,10 +2,10 @@ package hostcommands import ( "context" - "encoding/json" "fmt" "net" + json "github.com/bytedance/sonic" "github.com/go-openapi/swag" "github.com/openshift/assisted-service/internal/common" "github.com/openshift/assisted-service/internal/constants" @@ -91,7 +91,7 @@ func (f *domainNameResolutionCmd) prepareParam(host *models.Host, cluster *commo Domains: domains, } - b, err := json.Marshal(&request) + b, err := json.ConfigStd.Marshal(&request) if err != nil { f.log.WithError(err).Warn("Json marshal") return "", err diff --git a/internal/host/hostcommands/domain_name_resolution_cmd_test.go b/internal/host/hostcommands/domain_name_resolution_cmd_test.go index 9fc1fc38aab..7a6710e96b8 100644 --- a/internal/host/hostcommands/domain_name_resolution_cmd_test.go +++ b/internal/host/hostcommands/domain_name_resolution_cmd_test.go @@ -2,9 +2,9 @@ package hostcommands import ( "context" - "encoding/json" "fmt" + json "github.com/bytedance/sonic" "github.com/go-openapi/strfmt" "github.com/go-openapi/swag" "github.com/golang/mock/gomock" diff --git a/internal/host/hostcommands/download_boot_artifacts_cmd.go b/internal/host/hostcommands/download_boot_artifacts_cmd.go index 67376441ff6..bfc56e3bcbc 100644 --- a/internal/host/hostcommands/download_boot_artifacts_cmd.go +++ b/internal/host/hostcommands/download_boot_artifacts_cmd.go @@ -2,10 +2,10 @@ package hostcommands import ( "context" - "encoding/json" "fmt" "time" + json "github.com/bytedance/sonic" "github.com/openshift/assisted-service/internal/common" "github.com/openshift/assisted-service/internal/gencrypto" "github.com/openshift/assisted-service/internal/imageservice" @@ -72,7 +72,7 @@ func (c *downloadBootArtifactsCmd) GetSteps(ctx context.Context, host *models.Ho HostFsMountDir: &c.hostFSMountDir, } - requestBytes, err := json.Marshal(request) + requestBytes, err := json.ConfigStd.Marshal(request) if err != nil { return nil, fmt.Errorf("failed to marshal DownloadBootArtifactsRequest: %w", err) } diff --git a/internal/host/hostcommands/download_boot_artifacts_cmd_test.go b/internal/host/hostcommands/download_boot_artifacts_cmd_test.go index f9197ab3dba..9672054e720 100644 --- a/internal/host/hostcommands/download_boot_artifacts_cmd_test.go +++ b/internal/host/hostcommands/download_boot_artifacts_cmd_test.go @@ -2,10 +2,10 @@ package hostcommands import ( "context" - "encoding/json" "fmt" "time" + json "github.com/bytedance/sonic" "github.com/go-openapi/strfmt" "github.com/golang/mock/gomock" "github.com/google/uuid" diff --git a/internal/host/hostcommands/free_addresses_cmd.go b/internal/host/hostcommands/free_addresses_cmd.go index 1803ccef26a..8092237165a 100644 --- a/internal/host/hostcommands/free_addresses_cmd.go +++ b/internal/host/hostcommands/free_addresses_cmd.go @@ -2,9 +2,9 @@ package hostcommands import ( "context" - "encoding/json" "net" + json "github.com/bytedance/sonic" "github.com/openshift/assisted-service/internal/network" "github.com/openshift/assisted-service/models" "github.com/pkg/errors" @@ -76,7 +76,7 @@ func (f *freeAddressesCmd) prepareParam(host *models.Host) (string, error) { for _, cidr := range networks { request = append(request, cidr) } - b, err := json.Marshal(&request) + b, err := json.ConfigStd.Marshal(&request) if err != nil { f.log.WithError(err).Warn("Json marshal") return "", err diff --git a/internal/host/hostcommands/free_addresses_cmd_test.go b/internal/host/hostcommands/free_addresses_cmd_test.go index 08d4cbe4035..a1d4f0e277b 100644 --- a/internal/host/hostcommands/free_addresses_cmd_test.go +++ b/internal/host/hostcommands/free_addresses_cmd_test.go @@ -2,9 +2,9 @@ package hostcommands import ( "context" - "encoding/json" "fmt" + json "github.com/bytedance/sonic" "github.com/go-openapi/strfmt" "github.com/google/uuid" . "github.com/onsi/ginkgo" @@ -78,7 +78,7 @@ var _ = Describe("free_addresses", func() { } originalInventory.Interfaces = append(originalInventory.Interfaces, &newInterface2) - newInventoryBytes, err := json.Marshal(&originalInventory) + newInventoryBytes, err := json.ConfigStd.Marshal(&originalInventory) Expect(err).ToNot(HaveOccurred()) host.Inventory = string(newInventoryBytes) stepReply, stepErr = fCmd.GetSteps(ctx, &host) @@ -98,7 +98,7 @@ var _ = Describe("free_addresses", func() { IPV6Addresses: []string{}, Name: "chocobomb", }} - newInventoryBytes, err := json.Marshal(&originalInventory) + newInventoryBytes, err := json.ConfigStd.Marshal(&originalInventory) Expect(err).ToNot(HaveOccurred()) host.Inventory = string(newInventoryBytes) stepReply, stepErr = fCmd.GetSteps(ctx, &host) diff --git a/internal/host/hostcommands/install_cmd.go b/internal/host/hostcommands/install_cmd.go index 0e264061037..0a2833f794d 100644 --- a/internal/host/hostcommands/install_cmd.go +++ b/internal/host/hostcommands/install_cmd.go @@ -2,12 +2,12 @@ package hostcommands import ( "context" - "encoding/json" "fmt" "net" "net/netip" "strings" + json "github.com/bytedance/sonic" "github.com/go-openapi/strfmt" "github.com/go-openapi/swag" "github.com/openshift/assisted-service/internal/common" @@ -195,7 +195,7 @@ func (i *installCmd) getFullInstallerCommand(ctx context.Context, cluster *commo request.Proxy = i.getProxyArguments(cluster.Name, cluster.BaseDNSDomain, cluster.HTTPProxy, cluster.HTTPSProxy, cluster.NoProxy) - b, err := json.Marshal(&request) + b, err := json.ConfigStd.Marshal(&request) if err != nil { i.log.WithError(err).Warn("Json marshal") return "", err @@ -248,7 +248,7 @@ func (i *installCmd) getMustGatherArgument(mustGatherMap versions.MustGatherVers return mustGatherMap["ocp"], nil } - arg, err := json.Marshal(mustGatherMap) + arg, err := json.ConfigStd.Marshal(mustGatherMap) if err != nil { i.log.WithError(err).Errorf("can not encode must-gather image map") return "", err @@ -557,7 +557,7 @@ func toJSONString(args []string) (string, error) { if len(args) == 0 { return "", nil } - argsBytes, err := json.Marshal(args) + argsBytes, err := json.ConfigStd.Marshal(args) if err != nil { return "", err } diff --git a/internal/host/hostcommands/install_cmd_test.go b/internal/host/hostcommands/install_cmd_test.go index f7e389e4c2e..09266e15a2a 100644 --- a/internal/host/hostcommands/install_cmd_test.go +++ b/internal/host/hostcommands/install_cmd_test.go @@ -2,10 +2,10 @@ package hostcommands import ( "context" - "encoding/json" "fmt" "strings" + json "github.com/bytedance/sonic" "github.com/go-openapi/strfmt" "github.com/go-openapi/swag" "github.com/golang/mock/gomock" @@ -198,7 +198,7 @@ var _ = Describe("installcmd", func() { inventory := models.Inventory{ Disks: disks, } - b, err := json.Marshal(&inventory) + b, err := json.ConfigStd.Marshal(&inventory) Expect(err).To(Not(HaveOccurred())) return string(b) } diff --git a/internal/host/hostcommands/logs_cmd.go b/internal/host/hostcommands/logs_cmd.go index 5042c635ecd..1f4cd24f650 100644 --- a/internal/host/hostcommands/logs_cmd.go +++ b/internal/host/hostcommands/logs_cmd.go @@ -2,10 +2,10 @@ package hostcommands import ( "context" - "encoding/json" "net" "time" + json "github.com/bytedance/sonic" "github.com/go-openapi/swag" "github.com/openshift/assisted-service/internal/common" "github.com/openshift/assisted-service/internal/network" @@ -48,7 +48,7 @@ func (i *logsCmd) prepareParam(ctx context.Context, host *models.Host) (string, MasterIps: mastersIPs, } - b, err := json.Marshal(&request) + b, err := json.ConfigStd.Marshal(&request) if err != nil { i.log.WithError(err).Warn("Json marshal") return "", err diff --git a/internal/host/hostcommands/logs_cmd_test.go b/internal/host/hostcommands/logs_cmd_test.go index 12ac4c88461..a33983bcd4f 100644 --- a/internal/host/hostcommands/logs_cmd_test.go +++ b/internal/host/hostcommands/logs_cmd_test.go @@ -2,9 +2,9 @@ package hostcommands import ( "context" - "encoding/json" "time" + json "github.com/bytedance/sonic" "github.com/go-openapi/strfmt" "github.com/go-openapi/swag" "github.com/google/uuid" @@ -114,7 +114,7 @@ var _ = Describe("upload_logs", func() { common.TestDefaultConfig.Disks, }, } - b, err := json.Marshal(&inventory) + b, err := json.ConfigStd.Marshal(&inventory) Expect(err).To(Not(HaveOccurred())) host2.Inventory = string(b) host2.Role = models.HostRoleMaster diff --git a/internal/host/hostcommands/next_step_runner_cmd.go b/internal/host/hostcommands/next_step_runner_cmd.go index 5f90c139b98..b14c5b397a1 100644 --- a/internal/host/hostcommands/next_step_runner_cmd.go +++ b/internal/host/hostcommands/next_step_runner_cmd.go @@ -1,8 +1,7 @@ package hostcommands import ( - "encoding/json" - + json "github.com/bytedance/sonic" "github.com/go-openapi/strfmt" "github.com/go-openapi/swag" "github.com/openshift/assisted-service/models" @@ -26,7 +25,7 @@ func GetNextStepRunnerCommand(config *NextStepRunnerConfig) (string, *[]string, AgentVersion: swag.String(config.NextStepRunnerImage), } - b, err := json.Marshal(&request) + b, err := json.ConfigStd.Marshal(&request) if err != nil { log.WithError(err).Warn("Json marshal") return "", nil, err diff --git a/internal/host/hostcommands/next_step_runner_cmd_test.go b/internal/host/hostcommands/next_step_runner_cmd_test.go index 0981a351f25..181ef45bd68 100644 --- a/internal/host/hostcommands/next_step_runner_cmd_test.go +++ b/internal/host/hostcommands/next_step_runner_cmd_test.go @@ -1,8 +1,7 @@ package hostcommands import ( - "encoding/json" - + json "github.com/bytedance/sonic" "github.com/go-openapi/strfmt" "github.com/go-openapi/swag" "github.com/google/uuid" diff --git a/internal/host/hostcommands/ntp_synchronizer_cmd.go b/internal/host/hostcommands/ntp_synchronizer_cmd.go index 091565c52ce..dab3efdfe35 100644 --- a/internal/host/hostcommands/ntp_synchronizer_cmd.go +++ b/internal/host/hostcommands/ntp_synchronizer_cmd.go @@ -2,8 +2,8 @@ package hostcommands import ( "context" - "encoding/json" + json "github.com/bytedance/sonic" "github.com/go-openapi/swag" "github.com/openshift/assisted-service/internal/common" "github.com/openshift/assisted-service/models" @@ -33,7 +33,7 @@ func (f *ntpSynchronizerCmd) prepareParam(host *models.Host) (string, error) { request := models.NtpSynchronizationRequest{ NtpSource: swag.String(ntpSources), } - b, err := json.Marshal(&request) + b, err := json.ConfigStd.Marshal(&request) if err != nil { f.log.WithError(err).Warn("Json marshal") return "", err diff --git a/internal/host/hostcommands/reboot_for_reclaim_cmd.go b/internal/host/hostcommands/reboot_for_reclaim_cmd.go index 47126db7707..4eafcd06bdb 100644 --- a/internal/host/hostcommands/reboot_for_reclaim_cmd.go +++ b/internal/host/hostcommands/reboot_for_reclaim_cmd.go @@ -2,9 +2,9 @@ package hostcommands import ( "context" - "encoding/json" "fmt" + json "github.com/bytedance/sonic" "github.com/openshift/assisted-service/models" "github.com/sirupsen/logrus" ) @@ -25,7 +25,7 @@ func (c *rebootForReclaimCmd) GetSteps(ctx context.Context, host *models.Host) ( request := models.RebootForReclaimRequest{ HostFsMountDir: &c.HostFSMountDir, } - requestBytes, err := json.Marshal(request) + requestBytes, err := json.ConfigStd.Marshal(request) if err != nil { return nil, fmt.Errorf("failed to marshal RebootForReclaimRequest: %w", err) } diff --git a/internal/host/hostcommands/tang_connectivity_check_cmd.go b/internal/host/hostcommands/tang_connectivity_check_cmd.go index 9b61adc8e98..1b71dcaf7d1 100644 --- a/internal/host/hostcommands/tang_connectivity_check_cmd.go +++ b/internal/host/hostcommands/tang_connectivity_check_cmd.go @@ -2,8 +2,8 @@ package hostcommands import ( "context" - "encoding/json" + json "github.com/bytedance/sonic" ignition_types "github.com/coreos/ignition/v2/config/v3_2/types" "github.com/go-openapi/swag" "github.com/openshift/assisted-service/internal/common" @@ -47,11 +47,11 @@ func (c *tangConnectivityCheckCmd) getTangServersFromHostIgnition(host *models.H return nil, nil } if len(luks.Clevis.Tang) != 0 { - if tangServers, err = json.Marshal(luks.Clevis.Tang); err != nil { + if tangServers, err = json.ConfigStd.Marshal(luks.Clevis.Tang); err != nil { return nil, err } serverList := string(tangServers[:]) - request, err := json.Marshal( + request, err := json.ConfigStd.Marshal( models.TangConnectivityRequest{ TangServers: &serverList, }, @@ -87,7 +87,7 @@ func (c *tangConnectivityCheckCmd) getTangServersFromCluster(cluster common.Clus err error ) request := models.TangConnectivityRequest{TangServers: &cluster.DiskEncryption.TangServers} - tangServers, err = json.Marshal(request) + tangServers, err = json.ConfigStd.Marshal(request) if err != nil { c.log.WithError(err).Errorf("failed to Marshal TangConnectivityRequest with for cluster %s", host.ClusterID) return nil, err diff --git a/internal/host/hostcommands/tang_connectivity_check_cmd_test.go b/internal/host/hostcommands/tang_connectivity_check_cmd_test.go index 27ca89e159f..6abee9fe8e1 100644 --- a/internal/host/hostcommands/tang_connectivity_check_cmd_test.go +++ b/internal/host/hostcommands/tang_connectivity_check_cmd_test.go @@ -2,8 +2,8 @@ package hostcommands import ( "context" - "encoding/json" + json "github.com/bytedance/sonic" "github.com/go-openapi/strfmt" "github.com/go-openapi/swag" "github.com/google/uuid" @@ -94,7 +94,7 @@ var _ = Describe("tangConnectivitycheckcmd", func() { imported := true cluster.Imported = &imported Expect(db.Create(&cluster).Error).ShouldNot(HaveOccurred()) - apiVipConnectivity, err := json.Marshal(models.APIVipConnectivityResponse{ + apiVipConnectivity, err := json.ConfigStd.Marshal(models.APIVipConnectivityResponse{ IsSuccess: true, Ignition: hostIgnitionWithoutLuks, }) @@ -109,7 +109,7 @@ var _ = Describe("tangConnectivitycheckcmd", func() { imported := true cluster.Imported = &imported Expect(db.Create(&cluster).Error).ShouldNot(HaveOccurred()) - apiVipConnectivity, err := json.Marshal(models.APIVipConnectivityResponse{ + apiVipConnectivity, err := json.ConfigStd.Marshal(models.APIVipConnectivityResponse{ IsSuccess: true, Ignition: hostIgnitionWithoutClevis, }) @@ -126,7 +126,7 @@ var _ = Describe("tangConnectivitycheckcmd", func() { imported := true cluster.Imported = &imported Expect(db.Create(&cluster).Error).ShouldNot(HaveOccurred()) - apiVipConnectivity, err := json.Marshal(models.APIVipConnectivityResponse{ + apiVipConnectivity, err := json.ConfigStd.Marshal(models.APIVipConnectivityResponse{ IsSuccess: true, Ignition: hostIgnition, }) diff --git a/internal/host/hostcommands/upgrade_agent_cmd.go b/internal/host/hostcommands/upgrade_agent_cmd.go index 54d2212289d..ec33995db7d 100644 --- a/internal/host/hostcommands/upgrade_agent_cmd.go +++ b/internal/host/hostcommands/upgrade_agent_cmd.go @@ -2,8 +2,8 @@ package hostcommands import ( "context" - "encoding/json" + json "github.com/bytedance/sonic" "github.com/openshift/assisted-service/models" ) @@ -24,7 +24,7 @@ func (c *upgradeAgentCmd) GetSteps(ctx context.Context, host *models.Host) (resu request := models.UpgradeAgentRequest{ AgentImage: c.agentImage, } - data, err := json.Marshal(request) + data, err := json.ConfigStd.Marshal(request) if err != nil { return } diff --git a/internal/host/hostcommands/verify_vips_cmd.go b/internal/host/hostcommands/verify_vips_cmd.go index b1f12a28804..679fbbfcffc 100644 --- a/internal/host/hostcommands/verify_vips_cmd.go +++ b/internal/host/hostcommands/verify_vips_cmd.go @@ -2,8 +2,8 @@ package hostcommands import ( "context" - "encoding/json" + json "github.com/bytedance/sonic" "github.com/openshift/assisted-service/internal/common" "github.com/openshift/assisted-service/models" "github.com/pkg/errors" @@ -56,7 +56,7 @@ func (f *verifyVipsCmd) prepareParam(host *models.Host) (string, error) { }) } - b, err := json.Marshal(&request) + b, err := json.ConfigStd.Marshal(&request) if err != nil { f.log.WithError(err).Warn("Json marshal") return "", err diff --git a/internal/host/hostcommands/verify_vips_cmd_test.go b/internal/host/hostcommands/verify_vips_cmd_test.go index 05342ce51c7..5ebbbfcb603 100644 --- a/internal/host/hostcommands/verify_vips_cmd_test.go +++ b/internal/host/hostcommands/verify_vips_cmd_test.go @@ -2,8 +2,8 @@ package hostcommands import ( "context" - "encoding/json" + json "github.com/bytedance/sonic" "github.com/go-openapi/strfmt" "github.com/google/uuid" . "github.com/onsi/ginkgo" diff --git a/internal/host/hostutil/host_utils.go b/internal/host/hostutil/host_utils.go index df46d7b7fc6..a1e52158f06 100644 --- a/internal/host/hostutil/host_utils.go +++ b/internal/host/hostutil/host_utils.go @@ -1,7 +1,6 @@ package hostutil import ( - "encoding/json" "fmt" "net" "net/http" @@ -10,6 +9,7 @@ import ( "regexp" "strings" + json "github.com/bytedance/sonic" "github.com/coreos/ignition/v2/config/v3_2" ignition_types "github.com/coreos/ignition/v2/config/v3_2/types" "github.com/go-openapi/swag" @@ -251,7 +251,7 @@ func IsUnboundHost(h *models.Host) bool { } func MarshalConnectivityReport(report *models.ConnectivityReport) (string, error) { - if data, err := json.Marshal(report); err != nil { + if data, err := json.ConfigStd.Marshal(report); err != nil { return "", err } else { return string(data), nil diff --git a/internal/host/hostutil/test_utils.go b/internal/host/hostutil/test_utils.go index 93e201f7942..9443b62c73e 100644 --- a/internal/host/hostutil/test_utils.go +++ b/internal/host/hostutil/test_utils.go @@ -1,11 +1,11 @@ package hostutil import ( - "encoding/json" "fmt" "net" "time" + json "github.com/bytedance/sonic" "github.com/go-openapi/strfmt" "github.com/go-openapi/swag" . "github.com/onsi/gomega" @@ -159,7 +159,7 @@ func GenerateTestHostWithNetworkAddress(hostID, infraEnvID, clusterID strfmt.UUI func GenerateTestConnectivityReport() string { c := models.ConnectivityReport{RemoteHosts: []*models.ConnectivityRemoteHost{}} - b, err := json.Marshal(&c) + b, err := json.ConfigStd.Marshal(&c) Expect(err).NotTo(HaveOccurred()) return string(b) } @@ -191,7 +191,7 @@ func generateTestAPIVIpConnectivityResponseString(ignition string, isSuccess boo IsSuccess: isSuccess, Ignition: ignition, } - bytes, err := json.Marshal(checkAPIResponse) + bytes, err := json.ConfigStd.Marshal(checkAPIResponse) Expect(err).To(Not(HaveOccurred())) return string(bytes) } @@ -235,7 +235,7 @@ func GenerateTestTangConnectivity(success bool) string { } } - bytes, err := json.Marshal(checkAPIResponse) + bytes, err := json.ConfigStd.Marshal(checkAPIResponse) Expect(err).To(Not(HaveOccurred())) return string(bytes) } @@ -296,7 +296,7 @@ func GenerateMasterInventoryWithHostnameAndCpuFlags(hostname string, cpuflags [] SystemVendor: &models.SystemVendor{Manufacturer: "Red Hat", ProductName: systemPlatform, SerialNumber: "3534"}, Routes: common.TestDefaultRouteConfiguration, } - b, err := json.Marshal(&inventory) + b, err := json.ConfigStd.Marshal(&inventory) Expect(err).To(Not(HaveOccurred())) return string(b) } @@ -329,7 +329,7 @@ func GenerateMasterInventoryWithNetworks(ips ...string) string { SystemVendor: &models.SystemVendor{Manufacturer: "Red Hat", ProductName: "RHEL", SerialNumber: "3534"}, Routes: common.TestDefaultRouteConfiguration, } - b, err := json.Marshal(&inventory) + b, err := json.ConfigStd.Marshal(&inventory) Expect(err).To(Not(HaveOccurred())) return string(b) } @@ -360,7 +360,7 @@ func GenerateMasterInventoryWithNetworksOnSameInterface(v4ips []string, v6ips [] SystemVendor: &models.SystemVendor{Manufacturer: "Red Hat", ProductName: "RHEL", SerialNumber: "3534"}, Routes: common.TestDefaultRouteConfiguration, } - b, err := json.Marshal(&inventory) + b, err := json.ConfigStd.Marshal(&inventory) Expect(err).To(Not(HaveOccurred())) return string(b) } @@ -387,7 +387,7 @@ func GenerateMasterInventoryWithHostnameAndCpuFlagsV6(hostname string, cpuflags SystemVendor: &models.SystemVendor{Manufacturer: "Red Hat", ProductName: "RHEL", SerialNumber: "3534"}, Routes: common.TestDefaultRouteConfiguration, } - b, err := json.Marshal(&inventory) + b, err := json.ConfigStd.Marshal(&inventory) Expect(err).To(Not(HaveOccurred())) return string(b) } @@ -417,7 +417,7 @@ func GenerateMasterInventoryWithHostnameAndCpuFlagsDualStack(hostname string, cp SystemVendor: &models.SystemVendor{Manufacturer: "Red Hat", ProductName: "RHEL", SerialNumber: "3534"}, Routes: common.TestDefaultRouteConfiguration, } - b, err := json.Marshal(&inventory) + b, err := json.ConfigStd.Marshal(&inventory) Expect(err).To(Not(HaveOccurred())) return string(b) } @@ -445,7 +445,7 @@ func GenerateInventoryWithResources(cpu, memory int64, hostname string, gpus ... SystemVendor: &models.SystemVendor{Manufacturer: "Red Hat", ProductName: "RHEL", SerialNumber: "3534"}, Routes: common.TestDefaultRouteConfiguration, } - b, err := json.Marshal(&inventory) + b, err := json.ConfigStd.Marshal(&inventory) Expect(err).To(Not(HaveOccurred())) return string(b) } @@ -477,7 +477,7 @@ func GenerateInventoryWithResourcesAndMultipleDisk(cpu, memory int64, hostname s SystemVendor: &models.SystemVendor{Manufacturer: "Red Hat", ProductName: "RHEL", SerialNumber: "3534"}, Routes: common.TestDefaultRouteConfiguration, } - b, err := json.Marshal(&inventory) + b, err := json.ConfigStd.Marshal(&inventory) Expect(err).To(Not(HaveOccurred())) return string(b) } @@ -504,7 +504,7 @@ func GenerateInventoryWithResourcesWithBytes(cpu, physicalMemory int64, usableMe SystemVendor: &models.SystemVendor{Manufacturer: "Red Hat", ProductName: "RHEL", SerialNumber: "3534"}, Routes: common.TestDefaultRouteConfiguration, } - b, err := json.Marshal(&inventory) + b, err := json.ConfigStd.Marshal(&inventory) Expect(err).To(Not(HaveOccurred())) return string(b) } diff --git a/internal/host/refresh_status_preprocessor.go b/internal/host/refresh_status_preprocessor.go index 25a09e60030..4605c234996 100644 --- a/internal/host/refresh_status_preprocessor.go +++ b/internal/host/refresh_status_preprocessor.go @@ -2,11 +2,11 @@ package host import ( "context" - "encoding/json" "fmt" "sort" "strings" + json "github.com/bytedance/sonic" "github.com/openshift/assisted-service/internal/common" "github.com/openshift/assisted-service/internal/hardware" "github.com/openshift/assisted-service/internal/operators" diff --git a/internal/host/transition_test.go b/internal/host/transition_test.go index e09d1b97607..27bb4e0d45f 100644 --- a/internal/host/transition_test.go +++ b/internal/host/transition_test.go @@ -2,7 +2,6 @@ package host import ( "context" - "encoding/json" "fmt" "net" "net/http" @@ -10,6 +9,7 @@ import ( "strings" "time" + json "github.com/bytedance/sonic" "github.com/filanov/stateswitch" "github.com/go-openapi/strfmt" "github.com/go-openapi/swag" @@ -964,7 +964,7 @@ var _ = Describe("Unbind", func() { Expect(h.MachineConfigPoolName).Should(BeEmpty()) Expect(h.Role).Should(Equal(models.HostRoleAutoAssign)) Expect(h.SuggestedRole).Should(BeEmpty()) - bytes, err := json.Marshal(defaultNTPSources) + bytes, err := json.ConfigStd.Marshal(defaultNTPSources) Expect(err).ShouldNot(HaveOccurred()) Expect(h.NtpSources).Should(Equal(string(bytes))) resetPogress := &models.HostProgressInfo{ @@ -1174,7 +1174,7 @@ var _ = Describe("Unbind", func() { dstState := t.dstState - bytes, err := json.Marshal(defaultNTPSources) + bytes, err := json.ConfigStd.Marshal(defaultNTPSources) Expect(err).ShouldNot(HaveOccurred()) host.NtpSources = string(bytes) Expect(db.Create(&host).Error).ShouldNot(HaveOccurred()) @@ -1546,7 +1546,7 @@ var _ = Describe("Refresh Host", func() { createHost := func(hostStatus string) { host = hostutil.GenerateTestHostByKind(hostId, infraEnvId, &clusterId, hostStatus, models.HostKindHost, models.HostRoleMaster) host.Inventory = hostutil.GenerateMasterInventory() - bytes, err := json.Marshal(common.TestDomainNameResolutionsSuccess) + bytes, err := json.ConfigStd.Marshal(common.TestDomainNameResolutionsSuccess) Expect(err).ShouldNot(HaveOccurred()) host.DomainNameResolutions = string(bytes) host.Timestamp = time.Now().Unix() @@ -4627,14 +4627,14 @@ var _ = Describe("Refresh Host", func() { host.Role = t.role host.CheckedInAt = hostCheckInAt host.Kind = swag.String(t.kind) - bytes, err := json.Marshal(t.ntpSources) + bytes, err := json.ConfigStd.Marshal(t.ntpSources) Expect(err).ShouldNot(HaveOccurred()) host.NtpSources = string(bytes) - bytes, err = json.Marshal(t.imageStatuses) + bytes, err = json.ConfigStd.Marshal(t.imageStatuses) Expect(err).ShouldNot(HaveOccurred()) host.ImagesStatus = string(bytes) host.DisksInfo = t.disksInfo - bytes, err = json.Marshal(t.domainResolutions) + bytes, err = json.ConfigStd.Marshal(t.domainResolutions) Expect(err).ShouldNot(HaveOccurred()) host.DomainNameResolutions = string(bytes) if t.timestamp == 0 { @@ -4652,7 +4652,7 @@ var _ = Describe("Refresh Host", func() { h.CheckedInAt = hostCheckInAt h.Kind = swag.String(t.kind) h.RequestedHostname = fmt.Sprintf("additional-host-%d", i) - bytes, err = json.Marshal(t.ntpSources) + bytes, err = json.ConfigStd.Marshal(t.ntpSources) Expect(err).ShouldNot(HaveOccurred()) h.NtpSources = string(bytes) Expect(db.Create(&h).Error).ShouldNot(HaveOccurred()) @@ -5186,15 +5186,15 @@ var _ = Describe("Refresh Host", func() { host.Role = t.role host.CheckedInAt = strfmt.DateTime(time.Now()) host.RequestedHostname = t.requestedHostname - bytes, err := json.Marshal(ntpSources) + bytes, err := json.ConfigStd.Marshal(ntpSources) Expect(err).ShouldNot(HaveOccurred()) host.NtpSources = string(bytes) - bytes, err = json.Marshal(imageStatuses) + bytes, err = json.ConfigStd.Marshal(imageStatuses) Expect(err).ShouldNot(HaveOccurred()) host.ImagesStatus = string(bytes) host.DisksInfo = t.disksInfo domainNameResolutions := common.TestDomainNameResolutionsSuccess - bytes, err = json.Marshal(domainNameResolutions) + bytes, err = json.ConfigStd.Marshal(domainNameResolutions) Expect(err).ShouldNot(HaveOccurred()) host.DomainNameResolutions = string(bytes) Expect(db.Create(&host).Error).ShouldNot(HaveOccurred()) @@ -5321,7 +5321,7 @@ var _ = Describe("Refresh Host", func() { mockDefaultClusterHostRequirements(mockHwValidator) pr.EXPECT().IsHostSupported(commontesting.EqPlatformType(models.PlatformTypeVsphere), gomock.Any()).Return(false, nil).AnyTimes() - defaultNTPSourcesInBytes, err := json.Marshal(defaultNTPSources) + defaultNTPSourcesInBytes, err := json.ConfigStd.Marshal(defaultNTPSources) Expect(err).NotTo(HaveOccurred()) cluster = hostutil.GenerateTestCluster(clusterId) cluster.Name = common.TestDefaultConfig.ClusterName @@ -5332,7 +5332,7 @@ var _ = Describe("Refresh Host", func() { host.Role = models.HostRoleMaster host.NtpSources = string(defaultNTPSourcesInBytes) domainNameResolutions := common.TestDomainNameResolutionsSuccess - bytes, err := json.Marshal(domainNameResolutions) + bytes, err := json.ConfigStd.Marshal(domainNameResolutions) Expect(err).ShouldNot(HaveOccurred()) host.DomainNameResolutions = string(bytes) Expect(db.Create(&host).Error).ShouldNot(HaveOccurred()) @@ -5427,11 +5427,11 @@ var _ = Describe("Refresh Host", func() { host = hostutil.GenerateTestHost(hostId, infraEnvId, clusterId, models.HostStatusDiscovering) host.Inventory = hostutil.GenerateMasterInventoryWithSystemPlatform(t.hostPlatform) host.Role = models.HostRoleMaster - defaultNTPSourcesInBytes, err := json.Marshal(defaultNTPSources) + defaultNTPSourcesInBytes, err := json.ConfigStd.Marshal(defaultNTPSources) Expect(err).ShouldNot(HaveOccurred()) host.NtpSources = string(defaultNTPSourcesInBytes) defaultDomainNameResolutions := common.TestDomainNameResolutionsSuccess - domainNameResolutions, err := json.Marshal(defaultDomainNameResolutions) + domainNameResolutions, err := json.ConfigStd.Marshal(defaultDomainNameResolutions) Expect(err).ShouldNot(HaveOccurred()) host.DomainNameResolutions = string(domainNameResolutions) Expect(db.Create(&host).Error).ShouldNot(HaveOccurred()) @@ -5452,7 +5452,7 @@ var _ = Describe("Refresh Host", func() { }) Context("L3 network latency and packet loss validation", func() { - defaultNTPSourcesInBytes, err := json.Marshal(defaultNTPSources) + defaultNTPSourcesInBytes, err := json.ConfigStd.Marshal(defaultNTPSources) Expect(err).ShouldNot(HaveOccurred()) BeforeEach(func() { mockDefaultClusterHostRequirements(mockHwValidator) @@ -5631,7 +5631,7 @@ var _ = Describe("Refresh Host", func() { cluster.BaseDNSDomain = common.TestDefaultConfig.BaseDNSDomain domainNameResolutions := common.TestDomainNameResolutionsSuccess for _, h := range hosts { - b, err := json.Marshal(domainNameResolutions) + b, err := json.ConfigStd.Marshal(domainNameResolutions) Expect(err).ShouldNot(HaveOccurred()) h.DomainNameResolutions = string(b) } @@ -5643,7 +5643,7 @@ var _ = Describe("Refresh Host", func() { tmpHosts = append(tmpHosts, hosts[:n]...) tmpHosts = append(tmpHosts, hosts[n+1:]...) rep := hostutil.GenerateL3ConnectivityReport(tmpHosts, t.latencyInMs, t.packetLossInPercentage) - b, err := json.Marshal(&rep) + b, err := json.ConfigStd.Marshal(&rep) Expect(err).ShouldNot(HaveOccurred()) h.Connectivity = string(b) Expect(db.Create(h).Error).ShouldNot(HaveOccurred()) @@ -5688,7 +5688,7 @@ var _ = Describe("Refresh Host", func() { invalidGateway := []*models.Route{ {Family: int32(common.IPv4), Destination: "0.0.0.0", Gateway: "invalid"}, } - defaultNTPSourcesInBytes, err := json.Marshal(defaultNTPSources) + defaultNTPSourcesInBytes, err := json.ConfigStd.Marshal(defaultNTPSources) domainNameResolutions := common.TestDomainNameResolutionsSuccess Expect(err).ShouldNot(HaveOccurred()) BeforeEach(func() { @@ -5827,7 +5827,7 @@ var _ = Describe("Refresh Host", func() { host = hostutil.GenerateTestHost(hostId, infraEnvId, clusterId, models.HostStatusDiscovering) host.Inventory = inventory host.NtpSources = string(defaultNTPSourcesInBytes) - bytes, err := json.Marshal(domainNameResolutions) + bytes, err := json.ConfigStd.Marshal(domainNameResolutions) Expect(err).ShouldNot(HaveOccurred()) host.DomainNameResolutions = string(bytes) Expect(db.Create(&host).Error).ShouldNot(HaveOccurred()) @@ -6173,7 +6173,7 @@ var _ = Describe("Refresh Host", func() { } host.CheckedInAt = hostCheckInAt if t.sourceState != "" { - b, err := json.Marshal(&[]*models.NtpSource{ + b, err := json.ConfigStd.Marshal(&[]*models.NtpSource{ { SourceName: "source", SourceState: t.sourceState, @@ -6444,7 +6444,7 @@ func generateMajorityGroup(machineNetworks []*models.MachineNetwork, hostId strf for _, mnet := range machineNetworks { connectivity.MajorityGroups[string(mnet.Cidr)] = append(connectivity.MajorityGroups[string(mnet.Cidr)], hostId) } - tmp, err := json.Marshal(connectivity) + tmp, err := json.ConfigStd.Marshal(connectivity) if err != nil { return "" } @@ -6465,7 +6465,7 @@ func generateL3Counts(isIpv4, isIpv6 bool, hostIds ...strfmt.UUID) string { } connectivity.L3ConnectedAddresses[hostId] = addresses } - tmp, err := json.Marshal(connectivity) + tmp, err := json.ConfigStd.Marshal(connectivity) Expect(err).ToNot(HaveOccurred()) return string(tmp) } @@ -6478,7 +6478,7 @@ func addL3Connectivity(connectivityStr string, hostId strfmt.UUID) string { Expect(json.Unmarshal([]byte(connectivityStr), &connectivity)).ToNot(HaveOccurred()) } connectivity.MajorityGroups[network.IPv4.String()] = []strfmt.UUID{hostId} - tmp, err := json.Marshal(connectivity) + tmp, err := json.ConfigStd.Marshal(connectivity) Expect(err).ToNot(HaveOccurred()) return string(tmp) } diff --git a/internal/host/validations_test.go b/internal/host/validations_test.go index b93c39db045..7202baca8a1 100644 --- a/internal/host/validations_test.go +++ b/internal/host/validations_test.go @@ -3,9 +3,9 @@ package host import ( "context" _ "embed" - "encoding/json" "fmt" + json "github.com/bytedance/sonic" ignition_types "github.com/coreos/ignition/v2/config/v3_2/types" "github.com/go-openapi/strfmt" "github.com/go-openapi/swag" @@ -239,7 +239,7 @@ var _ = Describe("Validations test", func() { } setHostDomainResolutions := func(host *models.Host, domainNameResolutions *models.DomainResolutionResponse) { - bytes, err := json.Marshal(domainNameResolutions) + bytes, err := json.ConfigStd.Marshal(domainNameResolutions) Expect(err).ShouldNot(HaveOccurred()) host.DomainNameResolutions = string(bytes) } @@ -298,7 +298,7 @@ var _ = Describe("Validations test", func() { createDay2Cluster() h := getDay2Host() - configBytes, err := json.Marshal(getIgnitionConfigTPM2Enabled()) + configBytes, err := json.ConfigStd.Marshal(getIgnitionConfigTPM2Enabled()) Expect(err).ShouldNot(HaveOccurred()) h.APIVipConnectivity = hostutil.GenerateTestAPIConnectivityResponseSuccessString(string(configBytes)) Expect(db.Create(&h).Error).ShouldNot(HaveOccurred()) @@ -330,7 +330,7 @@ var _ = Describe("Validations test", func() { createDay2Cluster() h := getDay2Host() - apivip, err := json.Marshal(models.APIVipConnectivityResponse{ + apivip, err := json.ConfigStd.Marshal(models.APIVipConnectivityResponse{ IsSuccess: false, Ignition: "ignition", }) @@ -350,7 +350,7 @@ var _ = Describe("Validations test", func() { createDay2Cluster() h := getDay2Host() - apivip, err := json.Marshal(models.APIVipConnectivityResponse{ + apivip, err := json.ConfigStd.Marshal(models.APIVipConnectivityResponse{ IsSuccess: false, Ignition: "ignition", URL: "http://cluster.example.com:22624/config/worker", @@ -406,7 +406,7 @@ var _ = Describe("Validations test", func() { Fail("This should not happen") } - configBytes, err := json.Marshal(config) + configBytes, err := json.ConfigStd.Marshal(config) Expect(err).ShouldNot(HaveOccurred()) return configBytes @@ -1333,7 +1333,7 @@ var _ = Describe("Validations test", func() { h := getDay2Host() h.Inventory = common.GenerateTestInventoryWithTpmVersion(models.InventoryTpmVersionNr20) - configBytes, err := json.Marshal(getIgnitionConfigTPM2Enabled()) + configBytes, err := json.ConfigStd.Marshal(getIgnitionConfigTPM2Enabled()) Expect(err).To(Not(HaveOccurred())) h.APIVipConnectivity = hostutil.GenerateTestAPIConnectivityResponseSuccessString(string(configBytes)) Expect(db.Create(&h).Error).ShouldNot(HaveOccurred()) @@ -1355,7 +1355,7 @@ var _ = Describe("Validations test", func() { config := getIgnitionConfigTPM2Disabled() config.Storage.Luks[0].Clevis = &ignition_types.Clevis{ Tang: []ignition_types.Tang{{URL: "http://test", Thumbprint: swag.String("test")}}} - configBytes, err := json.Marshal(config) + configBytes, err := json.ConfigStd.Marshal(config) Expect(err).To(Not(HaveOccurred())) h.APIVipConnectivity = hostutil.GenerateTestAPIConnectivityResponseSuccessString(string(configBytes)) h.TangConnectivity = hostutil.GenerateTestTangConnectivity(true) @@ -1373,7 +1373,7 @@ var _ = Describe("Validations test", func() { h := getDay2Host() h.Inventory = common.GenerateTestInventoryWithTpmVersion(models.InventoryTpmVersionNr20) - configBytes, err := json.Marshal(getIgnitionConfigTPM2Disabled()) + configBytes, err := json.ConfigStd.Marshal(getIgnitionConfigTPM2Disabled()) Expect(err).To(Not(HaveOccurred())) h.APIVipConnectivity = hostutil.GenerateTestAPIConnectivityResponseSuccessString(string(configBytes)) Expect(db.Create(&h).Error).ShouldNot(HaveOccurred()) @@ -1392,7 +1392,7 @@ var _ = Describe("Validations test", func() { h := getDay2Host() h.Inventory = common.GenerateTestInventoryWithTpmVersion(models.InventoryTpmVersionNone) - configBytes, err := json.Marshal(getIgnitionConfigTPM2Enabled()) + configBytes, err := json.ConfigStd.Marshal(getIgnitionConfigTPM2Enabled()) Expect(err).To(Not(HaveOccurred())) h.APIVipConnectivity = hostutil.GenerateTestAPIConnectivityResponseSuccessString(string(configBytes)) @@ -1412,7 +1412,7 @@ var _ = Describe("Validations test", func() { h := getDay2Host() h.Inventory = common.GenerateTestInventoryWithTpmVersion(models.InventoryTpmVersionNone) - configBytes, err := json.Marshal(getIgnitionConfigTPM2Enabled()) + configBytes, err := json.ConfigStd.Marshal(getIgnitionConfigTPM2Enabled()) Expect(err).To(Not(HaveOccurred())) h.APIVipConnectivity = hostutil.GenerateTestAPIConnectivityResponseSuccessString(string(configBytes)) @@ -1432,7 +1432,7 @@ var _ = Describe("Validations test", func() { h := getDay2Host() //explicit set the role to worker h.Inventory = common.GenerateTestInventoryWithTpmVersion("") - configBytes, err := json.Marshal(ignition_types.Config{}) + configBytes, err := json.ConfigStd.Marshal(ignition_types.Config{}) Expect(err).To(Not(HaveOccurred())) h.APIVipConnectivity = hostutil.GenerateTestAPIConnectivityResponseSuccessString(string(configBytes)) Expect(db.Create(&h).Error).ShouldNot(HaveOccurred()) @@ -1481,7 +1481,7 @@ var _ = Describe("Validations test", func() { h := getDay2Host() h.Inventory = common.GenerateTestInventoryWithTpmVersion(models.InventoryTpmVersionNone) ignitionConfig := ignition_types.Config{Ignition: ignition_types.Ignition{Version: "3.2.0"}} - configBytes, err := json.Marshal(ignitionConfig) + configBytes, err := json.ConfigStd.Marshal(ignitionConfig) Expect(err).To(Not(HaveOccurred())) h.APIVipConnectivity = hostutil.GenerateTestAPIConnectivityResponseSuccessString(string(configBytes)) Expect(db.Create(&h).Error).ShouldNot(HaveOccurred()) @@ -1510,7 +1510,7 @@ var _ = Describe("Validations test", func() { h := getDay2Host() h.Inventory = common.GenerateTestInventory() - configBytes, err := json.Marshal(ignition_types.Config{}) + configBytes, err := json.ConfigStd.Marshal(ignition_types.Config{}) Expect(err).To(Not(HaveOccurred())) h.APIVipConnectivity = hostutil.GenerateTestAPIConnectivityResponseSuccessString(string(configBytes)) Expect(db.Create(&h).Error).ShouldNot(HaveOccurred()) @@ -1533,7 +1533,7 @@ var _ = Describe("Validations test", func() { h := getDay2Host() h.Inventory = common.GenerateTestInventory() - configBytes, err := json.Marshal(ignition_types.Config{}) + configBytes, err := json.ConfigStd.Marshal(ignition_types.Config{}) Expect(err).To(Not(HaveOccurred())) h.APIVipConnectivity = hostutil.GenerateTestAPIConnectivityResponseSuccessString(string(configBytes)) Expect(db.Create(&h).Error).ShouldNot(HaveOccurred()) @@ -1576,7 +1576,7 @@ var _ = Describe("Validations test", func() { inventory, err := common.UnmarshalInventory(host.Inventory) Expect(err).ToNot(HaveOccurred()) updateFunc(inventory) - inventoryByte, err := json.Marshal(inventory) + inventoryByte, err := json.ConfigStd.Marshal(inventory) Expect(err).ToNot(HaveOccurred()) updates := map[string]interface{}{} updates["inventory"] = string(inventoryByte) @@ -1989,7 +1989,7 @@ var _ = Describe("Validations test", func() { ID: inventoryDiskID, }) } - inventoryBytes, err := json.Marshal(inventory) + inventoryBytes, err := json.ConfigStd.Marshal(inventory) Expect(err).ShouldNot(HaveOccurred()) if test.noInventory { inventoryBytes = []byte("") @@ -2090,7 +2090,7 @@ var _ = Describe("Validations test", func() { mockIPCollisions := func(ipCollisions map[string][]string) { cluster.IPCollisions = "" if ipCollisions != nil { - collisionStr, err := json.Marshal(&ipCollisions) + collisionStr, err := json.ConfigStd.Marshal(&ipCollisions) Expect(err).ToNot(HaveOccurred()) cluster.IPCollisions = string(collisionStr) } @@ -2278,7 +2278,7 @@ var _ = Describe("Validations test", func() { err := json.Unmarshal([]byte(host.Inventory), &inventory) Expect(err).ShouldNot(HaveOccurred()) inventory.Disks = []*models.Disk{} - inventoryByte, _ := json.Marshal(inventory) + inventoryByte, _ := json.ConfigStd.Marshal(inventory) host.Inventory = string(inventoryByte) Expect(db.Create(&host).Error).ShouldNot(HaveOccurred()) @@ -2292,7 +2292,7 @@ var _ = Describe("Validations test", func() { inventory.Disks = []*models.Disk{&disk} - inventoryByte, _ := json.Marshal(inventory) + inventoryByte, _ := json.ConfigStd.Marshal(inventory) h.Inventory = string(inventoryByte) Expect(db.Save(&host).Error).ShouldNot(HaveOccurred()) @@ -2375,7 +2375,7 @@ var _ = Describe("Validations test", func() { {ID: "foo", DriveType: models.DriveTypeSSD}, } - inventoryByte, _ := json.Marshal(inventory) + inventoryByte, _ := json.ConfigStd.Marshal(inventory) host.Inventory = string(inventoryByte) Expect(db.Create(&cluster).Error).ToNot(HaveOccurred()) @@ -2397,7 +2397,7 @@ var _ = Describe("Validations test", func() { {ID: "install-disk", DriveType: models.DriveTypeSSD}, } - inventoryByte, _ := json.Marshal(inventory) + inventoryByte, _ := json.ConfigStd.Marshal(inventory) host.Inventory = string(inventoryByte) Expect(db.Create(&cluster).Error).ToNot(HaveOccurred()) @@ -2417,7 +2417,7 @@ var _ = Describe("Validations test", func() { host.InstallationDiskID = "install-disk" inventory.Disks = disks - inventoryByte, _ := json.Marshal(inventory) + inventoryByte, _ := json.ConfigStd.Marshal(inventory) host.Inventory = string(inventoryByte) Expect(db.Create(&cluster).Error).ToNot(HaveOccurred()) @@ -2444,7 +2444,7 @@ var _ = Describe("Validations test", func() { host.InstallationDiskID = "install-disk" inventory.Disks = disks - inventoryByte, _ := json.Marshal(inventory) + inventoryByte, _ := json.ConfigStd.Marshal(inventory) host.Inventory = string(inventoryByte) Expect(db.Create(&cluster).Error).ToNot(HaveOccurred()) @@ -2476,7 +2476,7 @@ var _ = Describe("Validations test", func() { inventory.Disks = disks inventory.Interfaces = interfaces - inventoryByte, _ := json.Marshal(inventory) + inventoryByte, _ := json.ConfigStd.Marshal(inventory) host.Inventory = string(inventoryByte) cluster.MachineNetworks = machineNetworks Expect(db.Create(&cluster).Error).ToNot(HaveOccurred()) @@ -3154,7 +3154,7 @@ var _ = Describe("Validations test", func() { hostID3: {host3IP}, }, } - bytes, err := json.Marshal(connectivity) + bytes, err := json.ConfigStd.Marshal(connectivity) Expect(err).NotTo(HaveOccurred()) cluster := &common.Cluster{ @@ -3230,7 +3230,7 @@ var _ = Describe("Validations test", func() { hostID3: {host3IP}, }, } - bytes, err := json.Marshal(connectivity) + bytes, err := json.ConfigStd.Marshal(connectivity) Expect(err).NotTo(HaveOccurred()) cluster := &common.Cluster{ @@ -3307,7 +3307,7 @@ var _ = Describe("Validations test", func() { hostID3: {host3IP}, }, } - bytes, err := json.Marshal(connectivity) + bytes, err := json.ConfigStd.Marshal(connectivity) Expect(err).NotTo(HaveOccurred()) cluster := &common.Cluster{ @@ -3383,7 +3383,7 @@ var _ = Describe("Validations test", func() { hostID3: {host3IP}, }, } - bytes, err := json.Marshal(connectivity) + bytes, err := json.ConfigStd.Marshal(connectivity) Expect(err).NotTo(HaveOccurred()) cluster := &common.Cluster{ @@ -3458,7 +3458,7 @@ var _ = Describe("Validations test", func() { "192.168.127.0/24": {hostID1, hostID2, hostID3}, }, } - bytes, err := json.Marshal(connectivity) + bytes, err := json.ConfigStd.Marshal(connectivity) Expect(err).NotTo(HaveOccurred()) cluster := &common.Cluster{ @@ -3533,7 +3533,7 @@ var _ = Describe("Validations test", func() { "192.168.127.0/24": {hostID2, hostID3}, }, } - bytes, err := json.Marshal(connectivity) + bytes, err := json.ConfigStd.Marshal(connectivity) Expect(err).NotTo(HaveOccurred()) cluster := &common.Cluster{ diff --git a/internal/host/validator.go b/internal/host/validator.go index 0082fa215ee..94ac4a46fc1 100644 --- a/internal/host/validator.go +++ b/internal/host/validator.go @@ -3,7 +3,6 @@ package host import ( "bytes" "context" - "encoding/json" "fmt" "math" "net" @@ -11,6 +10,7 @@ import ( "strings" "time" + json "github.com/bytedance/sonic" "github.com/coreos/ignition/v2/config/v3_2" ignition_types "github.com/coreos/ignition/v2/config/v3_2/types" "github.com/go-openapi/strfmt" diff --git a/internal/ignition/discovery.go b/internal/ignition/discovery.go index 09f13f64e20..c22895a4c22 100644 --- a/internal/ignition/discovery.go +++ b/internal/ignition/discovery.go @@ -4,7 +4,6 @@ import ( "bytes" "context" "encoding/base64" - "encoding/json" "fmt" "net/url" "os" @@ -14,6 +13,7 @@ import ( "text/template" "time" + json "github.com/bytedance/sonic" clusterPkg "github.com/openshift/assisted-service/internal/cluster" "github.com/openshift/assisted-service/internal/common" "github.com/openshift/assisted-service/internal/constants" @@ -507,7 +507,7 @@ func SetHostnameForNodeIgnition(ignition []byte, host *models.Host) ([]byte, err setFileInIgnition(config, "/etc/hostname", fmt.Sprintf("data:,%s", hostname), false, 420, true) - configBytes, err := json.Marshal(config) + configBytes, err := json.ConfigStd.Marshal(config) if err != nil { return nil, err } @@ -524,7 +524,7 @@ func getUserSSHKey(sshKey string) (string, error) { userAuthBlock["passwordHash"] = "!" userAuthBlock["sshAuthorizedKeys"] = keys userAuthBlock["groups"] = [1]string{"sudo"} - blockByte, err := json.Marshal(userAuthBlock) + blockByte, err := json.ConfigStd.Marshal(userAuthBlock) if err != nil { return "", fmt.Errorf("failed to build user ssh key block: %w", err) } @@ -561,7 +561,7 @@ func proxySettingsForIgnition(httpProxy, httpsProxy, noProxy string) (string, er httpsProxyAttr += `"httpsProxy": "` + httpsProxy + `"` } if noProxy != "" { - noProxyStr, err := json.Marshal(strings.Split(noProxy, ",")) + noProxyStr, err := json.ConfigStd.Marshal(strings.Split(noProxy, ",")) if err != nil { return "", err } diff --git a/internal/ignition/ignition.go b/internal/ignition/ignition.go index 6f2082e6aeb..629c5436c5a 100644 --- a/internal/ignition/ignition.go +++ b/internal/ignition/ignition.go @@ -1,9 +1,9 @@ package ignition import ( - "encoding/json" "os" + json "github.com/bytedance/sonic" "github.com/coreos/ignition/v2/config/merge" config_31 "github.com/coreos/ignition/v2/config/v3_1" config_latest "github.com/coreos/ignition/v2/config/v3_2" @@ -42,7 +42,7 @@ func parseIgnitionFile(path string) (*config_latest_types.Config, error) { // writeIgnitionFile writes an ignition config to a given path on disk func writeIgnitionFile(path string, config *config_latest_types.Config) error { - updatedBytes, err := json.Marshal(config) + updatedBytes, err := json.ConfigStd.Marshal(config) if err != nil { return err } @@ -95,7 +95,7 @@ func MergeIgnitionConfig(base []byte, overrides []byte) (string, error) { } mergeResult, _ := merge.MergeStructTranscribe(*baseConfig, *overrideConfig) - res, err := json.Marshal(mergeResult) + res, err := json.ConfigStd.Marshal(mergeResult) if err != nil { return "", err } diff --git a/internal/ignition/ignition_test.go b/internal/ignition/ignition_test.go index f678032940d..c4891004cb8 100644 --- a/internal/ignition/ignition_test.go +++ b/internal/ignition/ignition_test.go @@ -1,8 +1,7 @@ package ignition import ( - "encoding/json" - + json "github.com/bytedance/sonic" config_31 "github.com/coreos/ignition/v2/config/v3_1" config_32 "github.com/coreos/ignition/v2/config/v3_2" . "github.com/onsi/ginkgo" @@ -25,7 +24,7 @@ var _ = Context("with test ignitions", func() { Expect(err).ToNot(HaveOccurred()) Expect(config.Ignition.Version).To(Equal("3.2.0")) - bytes, err := json.Marshal(config) + bytes, err := json.ConfigStd.Marshal(config) Expect(err).ToNot(HaveOccurred()) v32Config, _, err := config_32.Parse(bytes) Expect(err).ToNot(HaveOccurred()) @@ -37,7 +36,7 @@ var _ = Context("with test ignitions", func() { Expect(err).ToNot(HaveOccurred()) Expect(config.Ignition.Version).To(Equal("3.1.0")) - bytes, err := json.Marshal(config) + bytes, err := json.ConfigStd.Marshal(config) Expect(err).ToNot(HaveOccurred()) v31Config, _, err := config_31.Parse(bytes) Expect(err).ToNot(HaveOccurred()) @@ -69,7 +68,7 @@ var _ = Context("with test ignitions", func() { Expect(err).ToNot(HaveOccurred()) Expect(config.Ignition.Version).To(Equal("3.1.0")) - bytes, err := json.Marshal(config) + bytes, err := json.ConfigStd.Marshal(config) Expect(err).ToNot(HaveOccurred()) v31Config, _, err := config_31.Parse(bytes) Expect(err).ToNot(HaveOccurred()) @@ -84,7 +83,7 @@ var _ = Context("with test ignitions", func() { Expect(err).ToNot(HaveOccurred()) Expect(config.Ignition.Version).To(Equal("3.2.0")) - bytes, err := json.Marshal(config) + bytes, err := json.ConfigStd.Marshal(config) Expect(err).ToNot(HaveOccurred()) v32Config, _, err := config_32.Parse(bytes) Expect(err).ToNot(HaveOccurred()) @@ -101,7 +100,7 @@ var _ = Context("with test ignitions", func() { Expect(err).ToNot(HaveOccurred()) Expect(config.Ignition.Version).To(Equal("3.1.0")) - bytes, err := json.Marshal(config) + bytes, err := json.ConfigStd.Marshal(config) Expect(err).ToNot(HaveOccurred()) v31Config, _, err := config_31.Parse(bytes) Expect(err).ToNot(HaveOccurred()) diff --git a/internal/ignition/installmanifests.go b/internal/ignition/installmanifests.go index 9f57d412045..cd1b5bb7e1a 100644 --- a/internal/ignition/installmanifests.go +++ b/internal/ignition/installmanifests.go @@ -4,7 +4,6 @@ import ( "bytes" "context" "encoding/base64" - "encoding/json" "fmt" "io" "net" @@ -15,6 +14,7 @@ import ( "sort" "strings" + json "github.com/bytedance/sonic" config_latest_types "github.com/coreos/ignition/v2/config/v3_2/types" "github.com/go-openapi/strfmt" "github.com/go-openapi/swag" @@ -880,7 +880,7 @@ func (g *installerGenerator) modifyBMHFile(file *config_latest_types.File, bmh * HardwareDetails: &hw, PoweredOn: true, } - statusJSON, err := json.Marshal(status) + statusJSON, err := json.ConfigStd.Marshal(status) if err != nil { return err } @@ -1094,7 +1094,7 @@ func (g *installerGenerator) updatePointerIgnitionMCP(poolName string, ignitionS r.Source = swag.String(strings.Replace(swag.StringValue(r.Source), "config/worker", "config/"+poolName, 1)) } } - b, err := json.Marshal(config) + b, err := json.ConfigStd.Marshal(config) if err != nil { return "", err } @@ -1150,7 +1150,7 @@ func (g *installerGenerator) writeSingleHostFile(host *models.Host, baseFile str } } - configBytes, err := json.Marshal(config) + configBytes, err := json.ConfigStd.Marshal(config) if err != nil { return err } diff --git a/internal/ignition/installmanifests_test.go b/internal/ignition/installmanifests_test.go index b84bfd93413..27129448ada 100644 --- a/internal/ignition/installmanifests_test.go +++ b/internal/ignition/installmanifests_test.go @@ -2,7 +2,6 @@ package ignition import ( "context" - "encoding/json" "errors" "fmt" "io" @@ -12,6 +11,7 @@ import ( "strings" "time" + json "github.com/bytedance/sonic" config_32 "github.com/coreos/ignition/v2/config/v3_2" config_32_types "github.com/coreos/ignition/v2/config/v3_2/types" "github.com/go-openapi/strfmt" @@ -133,7 +133,7 @@ var _ = Describe("Bootstrap Ignition Update", func() { config, err1 = ParseToLatest(bootstrapBytes) Expect(err1).NotTo(HaveOccurred()) Expect(config.Ignition.Version).To(Equal("3.2.0")) - bytes, err1 := json.Marshal(config) + bytes, err1 := json.ConfigStd.Marshal(config) Expect(err1).ToNot(HaveOccurred()) v32Config, _, err1 := config_32.Parse(bytes) Expect(err1).ToNot(HaveOccurred()) @@ -1839,7 +1839,7 @@ var _ = Describe("Bare metal host generation", func() { inventoryObject.Interfaces = []*models.Interface{ interfaceObject, } - inventoryJSON, err := json.Marshal(inventoryObject) + inventoryJSON, err := json.ConfigStd.Marshal(inventoryObject) Expect(err).ToNot(HaveOccurred()) host := &models.Host{ Inventory: string(inventoryJSON), diff --git a/internal/ignition/ironic.go b/internal/ignition/ironic.go index eb3f97cac59..ed6e6ef7365 100644 --- a/internal/ignition/ironic.go +++ b/internal/ignition/ironic.go @@ -1,8 +1,7 @@ package ignition import ( - "encoding/json" - + json "github.com/bytedance/sonic" ignition_config_types_32 "github.com/coreos/ignition/v2/config/v3_2/types" "github.com/openshift/assisted-service/internal/common" iccignition "github.com/openshift/image-customization-controller/pkg/ignition" @@ -26,5 +25,5 @@ func GenerateIronicConfig(ironicBaseURL string, ironicInspectorURL string, infra config.Storage.Files = []ignition_config_types_32.File{ib.IronicAgentConf(ironicInspectorVlanInterfaces)} // TODO: sort out the flags (authfile...) and copy network config.Systemd.Units = []ignition_config_types_32.Unit{ib.IronicAgentService(false)} - return json.Marshal(config) + return json.ConfigStd.Marshal(config) } diff --git a/internal/installcfg/builder/builder.go b/internal/installcfg/builder/builder.go index 2bbb180410b..743767d1091 100644 --- a/internal/installcfg/builder/builder.go +++ b/internal/installcfg/builder/builder.go @@ -1,8 +1,8 @@ package builder import ( - "encoding/json" "fmt" + json "github.com/bytedance/sonic" "strings" "github.com/go-openapi/swag" @@ -232,7 +232,7 @@ func (i *installConfigBuilder) applyConfigOverrides(overrides string, cfg *insta return nil } - overrideDecoder := json.NewDecoder(strings.NewReader(overrides)) + overrideDecoder := json.ConfigStd.NewDecoder(strings.NewReader(overrides)) overrideDecoder.DisallowUnknownFields() if err := overrideDecoder.Decode(cfg); err != nil { @@ -284,7 +284,7 @@ func (i *installConfigBuilder) GetInstallConfig(cluster *common.Cluster, cluster return nil, err } - return json.Marshal(*cfg) + return json.ConfigStd.Marshal(*cfg) } func (i *installConfigBuilder) ValidateInstallConfigPatch(cluster *common.Cluster, clusterInfraenvs []*common.InfraEnv, patch string) error { diff --git a/internal/installcfg/builder/builder_test.go b/internal/installcfg/builder/builder_test.go index fe26b228539..aeea72a27f3 100644 --- a/internal/installcfg/builder/builder_test.go +++ b/internal/installcfg/builder/builder_test.go @@ -1,7 +1,7 @@ package builder import ( - "encoding/json" + json "github.com/bytedance/sonic" "fmt" "strings" "testing" @@ -979,7 +979,7 @@ func getInventoryStr(hostname, bootMode string, ipv4 bool, ipv6 bool) string { inventory.Interfaces[0].IPV6Addresses = []string{"1001:db8::1/120"} } - ret, _ := json.Marshal(&inventory) + ret, _ := json.ConfigStd.Marshal(&inventory) return string(ret) } diff --git a/internal/json/consumers.go b/internal/json/consumers.go index 7de65914941..0372034ab9e 100644 --- a/internal/json/consumers.go +++ b/internal/json/consumers.go @@ -1,9 +1,9 @@ package json import ( - "encoding/json" "io" + json "github.com/bytedance/sonic" "github.com/go-openapi/runtime" ) @@ -11,7 +11,7 @@ import ( // documents while rejecting requests that contain unknown fields. func UnknownFieldsRejectingConsumer() runtime.Consumer { return runtime.ConsumerFunc(func(reader io.Reader, data interface{}) error { - dec := json.NewDecoder(reader) + dec := json.ConfigStd.NewDecoder(reader) dec.UseNumber() dec.DisallowUnknownFields() return dec.Decode(data) diff --git a/internal/manifests/manifests.go b/internal/manifests/manifests.go index 2f3f10597a6..49d680fbb79 100644 --- a/internal/manifests/manifests.go +++ b/internal/manifests/manifests.go @@ -4,7 +4,6 @@ import ( "bytes" "context" "encoding/base64" - "encoding/json" "fmt" "io" "net/http" @@ -12,6 +11,7 @@ import ( "path/filepath" "strings" + json "github.com/bytedance/sonic" "github.com/go-openapi/runtime/middleware" "github.com/go-openapi/strfmt" "github.com/go-openapi/swag" diff --git a/internal/metrics/metricsManager.go b/internal/metrics/metricsManager.go index 57b8d7235ff..3d49147fe1b 100644 --- a/internal/metrics/metricsManager.go +++ b/internal/metrics/metricsManager.go @@ -2,10 +2,10 @@ package metrics import ( "context" - "encoding/json" "time" "github.com/alecthomas/units" + json "github.com/bytedance/sonic" "github.com/go-openapi/strfmt" eventsapi "github.com/openshift/assisted-service/internal/events/api" "github.com/openshift/assisted-service/models" diff --git a/internal/migrations/20220221193600_change_static_config_format.go b/internal/migrations/20220221193600_change_static_config_format.go index a3237502f5c..16f913cd248 100644 --- a/internal/migrations/20220221193600_change_static_config_format.go +++ b/internal/migrations/20220221193600_change_static_config_format.go @@ -1,11 +1,11 @@ package migrations import ( - "encoding/json" "fmt" "sort" "strings" + json "github.com/bytedance/sonic" "github.com/go-gormigrate/gormigrate/v2" "github.com/openshift/assisted-service/models" "github.com/pkg/errors" @@ -66,7 +66,7 @@ func migrateInfraenvStaticConfigFormat(db *gorm.DB, infraEnv *models.InfraEnv) e if len(staticNetworkConfig) == 0 { staticNetworkConfigStr = "" } else { - b, err := json.Marshal(&staticNetworkConfig) + b, err := json.ConfigStd.Marshal(&staticNetworkConfig) if err != nil { return err } diff --git a/internal/migrations/20220221193600_change_static_config_format_test.go b/internal/migrations/20220221193600_change_static_config_format_test.go index 0774427a21c..04f879c3c8c 100644 --- a/internal/migrations/20220221193600_change_static_config_format_test.go +++ b/internal/migrations/20220221193600_change_static_config_format_test.go @@ -1,8 +1,7 @@ package migrations import ( - "encoding/json" - + json "github.com/bytedance/sonic" "github.com/go-gormigrate/gormigrate/v2" "github.com/go-openapi/strfmt" "github.com/google/uuid" @@ -67,7 +66,7 @@ var _ = Describe("Migrate static config format", func() { common.FormatStaticConfigHostYAML("nic10", "02000048ba38", "192.168.126.30", "192.168.141.30", "192.168.126.1", map1), common.FormatStaticConfigHostYAML("nic20", "02000048ba48", "192.168.126.31", "192.168.141.31", "192.168.126.1", map2), } - b, err := json.Marshal(&staticNetworkConfig) + b, err := json.ConfigStd.Marshal(&staticNetworkConfig) Expect(err).ToNot(HaveOccurred()) staticNetworkConfigJSONStr = string(b) infraEnvID1 = strfmt.UUID(uuid.New().String()) diff --git a/internal/network/connectivity_groups.go b/internal/network/connectivity_groups.go index 6a63dcbeec6..dba541ce257 100644 --- a/internal/network/connectivity_groups.go +++ b/internal/network/connectivity_groups.go @@ -1,9 +1,9 @@ package network import ( - "encoding/json" "net" + json "github.com/bytedance/sonic" "github.com/go-openapi/strfmt" "github.com/golang-collections/go-datastructures/bitarray" "github.com/openshift/assisted-service/internal/common" diff --git a/internal/network/connectivity_groups_test.go b/internal/network/connectivity_groups_test.go index 35db0fb1f19..a82a2096573 100644 --- a/internal/network/connectivity_groups_test.go +++ b/internal/network/connectivity_groups_test.go @@ -1,11 +1,11 @@ package network import ( - "encoding/json" "fmt" "net" "strings" + json "github.com/bytedance/sonic" "github.com/go-openapi/strfmt" "github.com/google/uuid" . "github.com/onsi/ginkgo" @@ -143,7 +143,7 @@ func createConnectivityReport(remoteHosts ...*models.ConnectivityRemoteHost) str report := models.ConnectivityReport{ RemoteHosts: remoteHosts, } - b, err := json.Marshal(&report) + b, err := json.ConfigStd.Marshal(&report) Expect(err).ToNot(HaveOccurred()) return string(b) } @@ -165,7 +165,7 @@ func makeInventory(node *node) string { } inventory.Interfaces = append(inventory.Interfaces, newInterface) } - b, err := json.Marshal(&inventory) + b, err := json.ConfigStd.Marshal(&inventory) Expect(err).ToNot(HaveOccurred()) return string(b) } diff --git a/internal/network/machine_network_cidr.go b/internal/network/machine_network_cidr.go index fa6a310bf6a..55ff578c46d 100644 --- a/internal/network/machine_network_cidr.go +++ b/internal/network/machine_network_cidr.go @@ -1,13 +1,13 @@ package network import ( - "encoding/json" "fmt" "math" "net" "strings" "github.com/IBM/netaddr" + json "github.com/bytedance/sonic" "github.com/go-openapi/strfmt" "github.com/hashicorp/go-multierror" "github.com/openshift/assisted-service/internal/common" diff --git a/internal/network/machine_network_cidr_test.go b/internal/network/machine_network_cidr_test.go index ea86969c93b..f4e583fe550 100644 --- a/internal/network/machine_network_cidr_test.go +++ b/internal/network/machine_network_cidr_test.go @@ -1,10 +1,10 @@ package network import ( - "encoding/json" "fmt" "testing" + json "github.com/bytedance/sonic" "github.com/go-openapi/strfmt" . "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo/extensions/table" @@ -16,7 +16,7 @@ import ( func createInventory(interfaces ...*models.Interface) string { inventory := models.Inventory{Interfaces: interfaces} - ret, _ := json.Marshal(&inventory) + ret, _ := json.ConfigStd.Marshal(&inventory) return string(ret) } @@ -156,7 +156,7 @@ var _ = Describe("inventory", func() { apiVip := "192.168.127.1" ingressVIP := "192.168.127.2" - bytes, err := json.Marshal([]FreeAddress{{ + bytes, err := json.ConfigStd.Marshal([]FreeAddress{{ Network: "192.168.127.0/24", FreeAddresses: []string{"192.168.127.1", "192.168.127.2"}, }}) @@ -179,7 +179,7 @@ var _ = Describe("inventory", func() { apiVip := "192.168.127.255" ingressVIP := "192.168.127.2" - bytes, err := json.Marshal([]FreeAddress{{ + bytes, err := json.ConfigStd.Marshal([]FreeAddress{{ Network: "192.168.127.0/24", FreeAddresses: []string{"192.168.127.1", "192.168.127.2"}, }}) @@ -202,7 +202,7 @@ var _ = Describe("inventory", func() { apiVip := "192.168.127.1" ingressVIP := "192.168.127.2" - bytes, err := json.Marshal([]FreeAddress{{ + bytes, err := json.ConfigStd.Marshal([]FreeAddress{{ Network: "192.168.127.0/24", FreeAddresses: []string{"192.168.127.2"}, }}) @@ -225,7 +225,7 @@ var _ = Describe("inventory", func() { apiVip := "192.168.127.1" ingressVIP := "192.168.127.2" - bytes, err := json.Marshal([]FreeAddress{{ + bytes, err := json.ConfigStd.Marshal([]FreeAddress{{ Network: "192.168.127.0/24", FreeAddresses: []string{"192.168.127.1"}, }}) @@ -248,7 +248,7 @@ var _ = Describe("inventory", func() { apiVip := "192.168.127.1" ingressVIP := "192.168.127.1" - bytes, err := json.Marshal([]FreeAddress{{ + bytes, err := json.ConfigStd.Marshal([]FreeAddress{{ Network: "192.168.127.0/24", FreeAddresses: []string{"192.168.127.1"}, }}) @@ -273,7 +273,7 @@ var _ = Describe("inventory", func() { apiVip := "192.168.127.1" ingressVIP := "192.168.127.2" - bytes, err := json.Marshal([]FreeAddress{{ + bytes, err := json.ConfigStd.Marshal([]FreeAddress{{ Network: "192.168.127.0/24", FreeAddresses: []string{"192.168.127.1", "192.168.127.2"}, }}) @@ -311,7 +311,7 @@ var _ = Describe("inventory", func() { apiVip := "192.168.127.1" ingressVIP := "192.168.127.2" - bytes, err := json.Marshal([]FreeAddress{{ + bytes, err := json.ConfigStd.Marshal([]FreeAddress{{ Network: "192.168.127.0/24", FreeAddresses: []string{"192.168.127.1", "192.168.127.2"}, }}) @@ -343,7 +343,7 @@ var _ = Describe("inventory", func() { apiVip := "192.168.127.1" ingressVIP := "192.168.127.2" - bytes, err := json.Marshal([]FreeAddress{{ + bytes, err := json.ConfigStd.Marshal([]FreeAddress{{ Network: "192.168.127.0/24", FreeAddresses: []string{"192.168.127.1", "192.168.127.2"}, }}) @@ -369,7 +369,7 @@ var _ = Describe("inventory", func() { apiVip := "192.168.126.1" ingressVIP := "192.168.127.2" - bytes, err := json.Marshal([]FreeAddress{{ + bytes, err := json.ConfigStd.Marshal([]FreeAddress{{ Network: "192.168.127.0/24", FreeAddresses: []string{"192.168.127.1", "192.168.126.2"}, }}) @@ -395,7 +395,7 @@ var _ = Describe("inventory", func() { apiVip := "192.168.127.1" ingressVIP := "192.168.126.2" - bytes, err := json.Marshal([]FreeAddress{{ + bytes, err := json.ConfigStd.Marshal([]FreeAddress{{ Network: "192.168.127.0/24", FreeAddresses: []string{"192.168.127.1", "192.168.126.2"}, }}) @@ -420,7 +420,7 @@ var _ = Describe("inventory", func() { apiVip := "192.168.127.255" ingressVIP := "192.168.127.1" - bytes, err := json.Marshal([]FreeAddress{{ + bytes, err := json.ConfigStd.Marshal([]FreeAddress{{ Network: "192.168.127.0/24", FreeAddresses: []string{"192.168.127.1", "192.168.127.2"}, }}) @@ -445,7 +445,7 @@ var _ = Describe("inventory", func() { apiVip := "192.168.127.1" ingressVIP := "192.168.127.255" - bytes, err := json.Marshal([]FreeAddress{{ + bytes, err := json.ConfigStd.Marshal([]FreeAddress{{ Network: "192.168.127.0/24", FreeAddresses: []string{"192.168.127.1", "192.168.127.2"}, }}) @@ -499,7 +499,7 @@ var _ = Describe("inventory", func() { apiVip := "192.168.127.1" ingressVIP := "192.168.127.2" - bytes, err := json.Marshal([]FreeAddress{{ + bytes, err := json.ConfigStd.Marshal([]FreeAddress{{ Network: "192.168.127.0/24", FreeAddresses: []string{"192.168.127.2"}, }}) @@ -523,7 +523,7 @@ var _ = Describe("inventory", func() { apiVip := "192.168.127.1" ingressVIP := "192.168.127.2" - bytes, err := json.Marshal([]FreeAddress{{ + bytes, err := json.ConfigStd.Marshal([]FreeAddress{{ Network: "192.168.127.0/24", FreeAddresses: []string{"192.168.127.1"}, }}) @@ -547,7 +547,7 @@ var _ = Describe("inventory", func() { apiVip := "192.168.127.1" ingressVIP := "192.168.127.1" - bytes, err := json.Marshal([]FreeAddress{{ + bytes, err := json.ConfigStd.Marshal([]FreeAddress{{ Network: "192.168.127.0/24", FreeAddresses: []string{"192.168.127.1"}, }}) @@ -979,7 +979,7 @@ func createHost(bootstrap bool, ipv4Addresses, ipv6Addresses []string) *models.H }, }, } - invBytes, _ := json.Marshal(&inv) + invBytes, _ := json.ConfigStd.Marshal(&inv) return &models.Host{ Bootstrap: bootstrap, Inventory: string(invBytes), diff --git a/internal/network/manifests_generator.go b/internal/network/manifests_generator.go index 19a2d0accff..c2dd9e7f97a 100644 --- a/internal/network/manifests_generator.go +++ b/internal/network/manifests_generator.go @@ -4,11 +4,11 @@ import ( "bytes" "context" "encoding/base64" - "encoding/json" "fmt" "strings" "text/template" + json "github.com/bytedance/sonic" "github.com/go-openapi/swag" "github.com/kelseyhightower/envconfig" "github.com/openshift/assisted-service/internal/common" diff --git a/internal/network/manifests_generator_test.go b/internal/network/manifests_generator_test.go index 6806fd1cf1b..e0b480d1842 100644 --- a/internal/network/manifests_generator_test.go +++ b/internal/network/manifests_generator_test.go @@ -3,10 +3,10 @@ package network import ( "context" "encoding/base64" - "encoding/json" "fmt" "regexp" + json "github.com/bytedance/sonic" configv31 "github.com/coreos/ignition/v2/config/v3_1" "github.com/go-openapi/strfmt" "github.com/go-openapi/swag" @@ -43,7 +43,7 @@ var _ = Describe("chrony manifest", func() { createHost := func(sources []*models.NtpSource) *models.Host { var sourcesText string if sources != nil { - sourcesBytes, err := json.Marshal(&sources) + sourcesBytes, err := json.ConfigStd.Marshal(&sources) Expect(err).ToNot(HaveOccurred()) sourcesText = string(sourcesBytes) } diff --git a/internal/network/none_platform_node_ips_allocation.go b/internal/network/none_platform_node_ips_allocation.go index 9aa35ec4903..645fd7d955e 100644 --- a/internal/network/none_platform_node_ips_allocation.go +++ b/internal/network/none_platform_node_ips_allocation.go @@ -1,13 +1,13 @@ package network import ( - "encoding/json" stderrors "errors" "fmt" "net" "sort" "strings" + json "github.com/bytedance/sonic" "github.com/go-openapi/strfmt" "github.com/openshift/assisted-service/internal/common" "github.com/openshift/assisted-service/models" diff --git a/internal/network/none_platform_node_ips_allocation_test.go b/internal/network/none_platform_node_ips_allocation_test.go index 822936122cb..62aa9177ca1 100644 --- a/internal/network/none_platform_node_ips_allocation_test.go +++ b/internal/network/none_platform_node_ips_allocation_test.go @@ -1,8 +1,7 @@ package network import ( - "encoding/json" - + json "github.com/bytedance/sonic" "github.com/go-openapi/strfmt" "github.com/google/uuid" . "github.com/onsi/ginkgo" @@ -66,7 +65,7 @@ var _ = Describe("none platform node ips allocation", func() { Interfaces: interfaces, Routes: routes, } - b, err := json.Marshal(&inventory) + b, err := json.ConfigStd.Marshal(&inventory) Expect(err).ToNot(HaveOccurred()) return string(b) } @@ -83,7 +82,7 @@ var _ = Describe("none platform node ips allocation", func() { majorityGroups := &Connectivity{ L3ConnectedAddresses: connectedAddresses, } - tmp, err := json.Marshal(majorityGroups) + tmp, err := json.ConfigStd.Marshal(majorityGroups) Expect(err).ToNot(HaveOccurred()) return string(tmp) } diff --git a/internal/oc/release.go b/internal/oc/release.go index 5327225524a..76f7f818027 100644 --- a/internal/oc/release.go +++ b/internal/oc/release.go @@ -1,7 +1,6 @@ package oc import ( - "encoding/json" "fmt" "os" "path/filepath" @@ -13,6 +12,7 @@ import ( "time" "github.com/buger/jsonparser" + json "github.com/bytedance/sonic" "github.com/hashicorp/go-version" configv1 "github.com/openshift/api/config/v1" operatorv1alpha1 "github.com/openshift/api/operator/v1alpha1" @@ -585,7 +585,7 @@ func getIdmsContents(mirrorConfig []mirrorregistries.RegistriesConf) ([]byte, er } // Convert to json first so json tags are handled - jsonData, err := json.Marshal(&idms) + jsonData, err := json.ConfigStd.Marshal(&idms) if err != nil { return nil, err } @@ -645,7 +645,7 @@ func getIcspContents(mirrorConfig []mirrorregistries.RegistriesConf) ([]byte, er } // Convert to json first so json tags are handled - jsonData, err := json.Marshal(&icsp) + jsonData, err := json.ConfigStd.Marshal(&icsp) if err != nil { return nil, err } diff --git a/internal/operators/authorino/test_utils.go b/internal/operators/authorino/test_utils.go index 9ee91e9bec0..52f5bfb9575 100644 --- a/internal/operators/authorino/test_utils.go +++ b/internal/operators/authorino/test_utils.go @@ -1,8 +1,7 @@ package authorino import ( - "encoding/json" - + json "github.com/bytedance/sonic" . "github.com/onsi/gomega" "github.com/openshift/assisted-service/models" ) @@ -34,7 +33,7 @@ func Inventory(r *InventoryResources) string { }, Disks: r.Disks, } - b, err := json.Marshal(&inventory) + b, err := json.ConfigStd.Marshal(&inventory) Expect(err).To(Not(HaveOccurred())) return string(b) } diff --git a/internal/operators/lvm/test_utils.go b/internal/operators/lvm/test_utils.go index ca9297ccca7..7546c0a47ed 100644 --- a/internal/operators/lvm/test_utils.go +++ b/internal/operators/lvm/test_utils.go @@ -1,8 +1,7 @@ package lvm import ( - "encoding/json" - + json "github.com/bytedance/sonic" . "github.com/onsi/gomega" "github.com/openshift/assisted-service/models" ) @@ -34,7 +33,7 @@ func Inventory(r *InventoryResources) string { }, Disks: r.Disks, } - b, err := json.Marshal(&inventory) + b, err := json.ConfigStd.Marshal(&inventory) Expect(err).To(Not(HaveOccurred())) return string(b) } diff --git a/internal/operators/manager.go b/internal/operators/manager.go index 0a5c68f5e92..f6baa548f63 100644 --- a/internal/operators/manager.go +++ b/internal/operators/manager.go @@ -4,11 +4,11 @@ import ( "container/list" "context" "encoding/base64" - "encoding/json" "fmt" "path" "strings" + json "github.com/bytedance/sonic" "github.com/go-openapi/swag" "github.com/openshift/assisted-service/internal/common" "github.com/openshift/assisted-service/internal/featuresupport" @@ -193,7 +193,7 @@ func (mgr *Manager) GenerateManifests(ctx context.Context, cluster *common.Clust } if len(controllerManifests) > 0 { - content, err := json.Marshal(controllerManifests) + content, err := json.ConfigStd.Marshal(controllerManifests) if err != nil { return err } diff --git a/internal/operators/manager_test.go b/internal/operators/manager_test.go index c9d6906481e..f86f2468579 100644 --- a/internal/operators/manager_test.go +++ b/internal/operators/manager_test.go @@ -3,11 +3,11 @@ package operators_test import ( "context" "encoding/base64" - "encoding/json" "errors" "fmt" "regexp" + json "github.com/bytedance/sonic" "github.com/go-openapi/strfmt" "github.com/go-openapi/swag" "github.com/golang/mock/gomock" diff --git a/internal/operators/mce/test_utils.go b/internal/operators/mce/test_utils.go index cd7af33c698..634a7d2142d 100644 --- a/internal/operators/mce/test_utils.go +++ b/internal/operators/mce/test_utils.go @@ -1,8 +1,7 @@ package mce import ( - "encoding/json" - + json "github.com/bytedance/sonic" . "github.com/onsi/gomega" "github.com/openshift/assisted-service/models" ) @@ -34,7 +33,7 @@ func Inventory(r *InventoryResources) string { }, Disks: r.Disks, } - b, err := json.Marshal(&inventory) + b, err := json.ConfigStd.Marshal(&inventory) Expect(err).To(Not(HaveOccurred())) return string(b) } diff --git a/internal/operators/nvidiagpu/nvidia_gpu_operator_test.go b/internal/operators/nvidiagpu/nvidia_gpu_operator_test.go index 1ddb695d0de..134589a5f20 100644 --- a/internal/operators/nvidiagpu/nvidia_gpu_operator_test.go +++ b/internal/operators/nvidiagpu/nvidia_gpu_operator_test.go @@ -2,8 +2,8 @@ package nvidiagpu import ( "context" - "encoding/json" + json "github.com/bytedance/sonic" . "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo/extensions/table" . "github.com/onsi/gomega" @@ -26,7 +26,7 @@ var _ = Describe("Operator", func() { DescribeTable( "Validate hosts", func(inventory *models.Inventory, expected api.ValidationResult) { - data, err := json.Marshal(inventory) + data, err := json.ConfigStd.Marshal(inventory) Expect(err).ToNot(HaveOccurred()) host := &models.Host{ Inventory: string(data), diff --git a/internal/operators/odf/test_utils.go b/internal/operators/odf/test_utils.go index 2e24b0b09ec..761aaf3ca71 100644 --- a/internal/operators/odf/test_utils.go +++ b/internal/operators/odf/test_utils.go @@ -1,8 +1,7 @@ package odf import ( - "encoding/json" - + json "github.com/bytedance/sonic" . "github.com/onsi/gomega" "github.com/openshift/assisted-service/models" ) @@ -34,7 +33,7 @@ func Inventory(r *InventoryResources) string { }, Disks: r.Disks, } - b, err := json.Marshal(&inventory) + b, err := json.ConfigStd.Marshal(&inventory) Expect(err).To(Not(HaveOccurred())) return string(b) } diff --git a/internal/operators/odf/validation_test.go b/internal/operators/odf/validation_test.go index 947a16e3ec8..6720c70f511 100644 --- a/internal/operators/odf/validation_test.go +++ b/internal/operators/odf/validation_test.go @@ -2,9 +2,9 @@ package odf_test import ( "context" - "encoding/json" "fmt" + json "github.com/bytedance/sonic" "github.com/go-openapi/strfmt" "github.com/go-openapi/swag" "github.com/golang/mock/gomock" diff --git a/internal/operators/openshiftai/test_utils.go b/internal/operators/openshiftai/test_utils.go index c1480218194..d7476be0c2f 100644 --- a/internal/operators/openshiftai/test_utils.go +++ b/internal/operators/openshiftai/test_utils.go @@ -1,8 +1,7 @@ package openshiftai import ( - "encoding/json" - + json "github.com/bytedance/sonic" . "github.com/onsi/gomega" "github.com/openshift/assisted-service/models" ) @@ -34,7 +33,7 @@ func Inventory(r *InventoryResources) string { }, Disks: r.Disks, } - b, err := json.Marshal(&inventory) + b, err := json.ConfigStd.Marshal(&inventory) Expect(err).To(Not(HaveOccurred())) return string(b) } diff --git a/internal/provider/baremetal/installConfig.go b/internal/provider/baremetal/installConfig.go index 5abeecff0c3..4e1ab05611a 100644 --- a/internal/provider/baremetal/installConfig.go +++ b/internal/provider/baremetal/installConfig.go @@ -1,12 +1,12 @@ package baremetal import ( - "encoding/json" "fmt" "slices" "sort" "strings" + json "github.com/bytedance/sonic" "github.com/go-openapi/swag" configv1 "github.com/openshift/api/config/v1" "github.com/openshift/assisted-service/internal/common" diff --git a/internal/provider/external/oci_test.go b/internal/provider/external/oci_test.go index b6c5675b995..b2a91aeef1f 100644 --- a/internal/provider/external/oci_test.go +++ b/internal/provider/external/oci_test.go @@ -1,8 +1,7 @@ package external import ( - "encoding/json" - + json "github.com/bytedance/sonic" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" "github.com/openshift/assisted-service/internal/common" @@ -21,7 +20,7 @@ var _ = Describe("oci", func() { }) setHostInventory := func(inventory *models.Inventory, host *models.Host) { - data, err := json.Marshal(inventory) + data, err := json.ConfigStd.Marshal(inventory) Expect(err).To(BeNil()) host.Inventory = string(data) } diff --git a/internal/provider/nutanix/base_test.go b/internal/provider/nutanix/base_test.go index 3abcb8e2af3..83bd0deaba8 100644 --- a/internal/provider/nutanix/base_test.go +++ b/internal/provider/nutanix/base_test.go @@ -1,8 +1,7 @@ package nutanix import ( - "encoding/json" - + json "github.com/bytedance/sonic" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" "github.com/openshift/assisted-service/internal/common" @@ -21,7 +20,7 @@ var _ = Describe("base", func() { }) setHostInventory := func(inventory *models.Inventory, host *models.Host) { - data, err := json.Marshal(inventory) + data, err := json.ConfigStd.Marshal(inventory) Expect(err).To(BeNil()) host.Inventory = string(data) } diff --git a/internal/provider/registry/registry_test.go b/internal/provider/registry/registry_test.go index 8e9cfba698a..9eec1067fca 100644 --- a/internal/provider/registry/registry_test.go +++ b/internal/provider/registry/registry_test.go @@ -1,10 +1,10 @@ package registry import ( - "encoding/json" "fmt" "testing" + json "github.com/bytedance/sonic" "github.com/go-openapi/strfmt" "github.com/go-openapi/swag" "github.com/golang/mock/gomock" @@ -603,7 +603,7 @@ func getVsphereInventoryStr(hostname, bootMode string, ipv4, ipv6 bool) string { SerialNumber: "VMware-12 34 56 78 90 12 ab cd-ef gh 12 34 56 67 89 90", Virtual: true, } - ret, _ := json.Marshal(&inventory) + ret, _ := json.ConfigStd.Marshal(&inventory) return string(ret) } @@ -615,7 +615,7 @@ func getBaremetalInventoryStr(hostname, bootMode string, ipv4, ipv6 bool) string SerialNumber: "", Virtual: false, } - ret, _ := json.Marshal(&inventory) + ret, _ := json.ConfigStd.Marshal(&inventory) return string(ret) } @@ -627,7 +627,7 @@ func getNutanixInventoryStr(hostname, bootMode string, ipv4, ipv6 bool) string { SerialNumber: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", Virtual: true, } - ret, _ := json.Marshal(&inventory) + ret, _ := json.ConfigStd.Marshal(&inventory) return string(ret) } diff --git a/internal/provider/vsphere/base_test.go b/internal/provider/vsphere/base_test.go index 86d606f9354..46db1082621 100644 --- a/internal/provider/vsphere/base_test.go +++ b/internal/provider/vsphere/base_test.go @@ -1,8 +1,7 @@ package vsphere import ( - "encoding/json" - + json "github.com/bytedance/sonic" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" "github.com/openshift/assisted-service/internal/common" @@ -21,7 +20,7 @@ var _ = Describe("base", func() { }) setHostInventory := func(inventory *models.Inventory, host *models.Host) { - data, err := json.Marshal(inventory) + data, err := json.ConfigStd.Marshal(inventory) Expect(err).To(BeNil()) host.Inventory = string(data) } diff --git a/internal/releasesources/clients.go b/internal/releasesources/clients.go index 26274ba72f3..87a92923e9f 100644 --- a/internal/releasesources/clients.go +++ b/internal/releasesources/clients.go @@ -1,11 +1,11 @@ package releasesources import ( - "encoding/json" "fmt" "net/http" "net/url" + json "github.com/bytedance/sonic" "github.com/openshift/assisted-service/models" "github.com/pkg/errors" ) @@ -92,7 +92,7 @@ func requestAndDecode(rawUrl string, decodeInto any) error { return errors.Wrapf(err, "an error occurred while making http request to %s", rawUrl) } - err = json.NewDecoder(response.Body).Decode(&decodeInto) + err = json.ConfigStd.NewDecoder(response.Body).Decode(&decodeInto) if err != nil { return errors.Wrapf(err, "an error occurred while decoding the response to a request made to %s", rawUrl) } diff --git a/internal/spec/spec_test.go b/internal/spec/spec_test.go index 3004d7310ad..25fc07de408 100644 --- a/internal/spec/spec_test.go +++ b/internal/spec/spec_test.go @@ -1,7 +1,6 @@ package spec import ( - "encoding/json" "fmt" "io" "log" @@ -9,6 +8,7 @@ import ( "net/http/httptest" "testing" + json "github.com/bytedance/sonic" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" ) diff --git a/internal/templating/templating.go b/internal/templating/templating.go index 41dc9b004a9..5a1643ef7c0 100644 --- a/internal/templating/templating.go +++ b/internal/templating/templating.go @@ -3,10 +3,11 @@ package templating import ( "bytes" "encoding/base64" - "encoding/json" "fmt" "io/fs" "text/template" + + json "github.com/bytedance/sonic" ) // LoadTemplates loads the templates from the given file system. @@ -114,7 +115,7 @@ func toStringFunc(data interface{}) (result string, err error) { // Note how that the value of that 'content' field doesn't need to sorrounded by quotes, because the 'toJson' function // will generate a valid JSON string, including those quotes. func toJsonFunc(data interface{}) (result string, err error) { - text, err := json.Marshal(data) + text, err := json.ConfigStd.Marshal(data) if err != nil { return } diff --git a/internal/uploader/events_uploader.go b/internal/uploader/events_uploader.go index 47124196c7e..73fe1142fae 100644 --- a/internal/uploader/events_uploader.go +++ b/internal/uploader/events_uploader.go @@ -6,7 +6,6 @@ import ( "compress/gzip" "context" "crypto/tls" - "encoding/json" "fmt" "io" "mime/multipart" @@ -14,6 +13,7 @@ import ( "net/textproto" "time" + json "github.com/bytedance/sonic" "github.com/go-openapi/strfmt" "github.com/openshift/assisted-service/internal/common" eventsapi "github.com/openshift/assisted-service/internal/events/api" @@ -181,7 +181,7 @@ func prepareFiles(ctx context.Context, db *gorm.DB, cluster *common.Cluster, eve } // Add versions file to bundle - if versionsJson, err := json.Marshal(versions.GetModelVersions(config.Versions)); err == nil { + if versionsJson, err := json.ConfigStd.Marshal(versions.GetModelVersions(config.Versions)); err == nil { addFile(tw, versionsJson, fmt.Sprintf("%s/versions.json", *cluster.ID)) //nolint:errcheck // errors adding this file shouldn't prevent the data from being sent } @@ -202,7 +202,7 @@ func prepareFiles(ctx context.Context, db *gorm.DB, cluster *common.Cluster, eve func metadataFile(tw *tar.Writer, clusterID *strfmt.UUID, config Config) { metadata := createMetadataContent(config) - if metadataJson, err := json.Marshal(metadata); err == nil { + if metadataJson, err := json.ConfigStd.Marshal(metadata); err == nil { addFile(tw, metadataJson, fmt.Sprintf("%s/%s", *clusterID, metadataFileName)) //nolint:errcheck // errors adding this file shouldn't prevent the data from being sent } } @@ -235,7 +235,7 @@ func eventsFile(ctx context.Context, clusterID *strfmt.UUID, eventsHandler event events = append(events, &dbEvent.Event) } - contents, err := json.MarshalIndent(events, "", " ") + contents, err := json.ConfigStd.MarshalIndent(events, "", " ") if err != nil { return errors.Wrapf(err, "failed to marshal events") } @@ -263,7 +263,7 @@ func clusterFile(tw *tar.Writer, cluster *common.Cluster) error { return errors.New("no cluster provided for cluster file") } - clusterJson, err := json.Marshal(cluster.Cluster) + clusterJson, err := json.ConfigStd.Marshal(cluster.Cluster) if err != nil { return errors.Wrapf(err, "failed to marshal cluster %s", *cluster.ID) } @@ -281,7 +281,7 @@ func infraEnvFile(db *gorm.DB, tw *tar.Writer, infraEnvID *strfmt.UUID, clusterI return errors.Wrapf(err, "error getting infra-env %s from db", *infraEnvID) } - iJson, err := json.Marshal(infraEnv.InfraEnv) + iJson, err := json.ConfigStd.Marshal(infraEnv.InfraEnv) if err != nil { return errors.Wrapf(err, "error marshalling infra-env %s", *infraEnvID) } @@ -306,7 +306,7 @@ func hostsFile(db *gorm.DB, tw *tar.Writer, cluster *common.Cluster) (*strfmt.UU return nil, errors.Errorf("no hosts found for cluster %s", *cluster.ID) } - hostJson, err := json.MarshalIndent(hosts, "", " ") + hostJson, err := json.ConfigStd.MarshalIndent(hosts, "", " ") if err != nil { return nil, errors.Wrapf(err, "failed marshalling hosts for cluster %s for events file", *cluster.ID) } diff --git a/internal/uploader/events_uploader_test.go b/internal/uploader/events_uploader_test.go index e6e2da30ae0..bd5bd260e19 100644 --- a/internal/uploader/events_uploader_test.go +++ b/internal/uploader/events_uploader_test.go @@ -6,7 +6,6 @@ import ( "compress/gzip" "context" "encoding/base64" - "encoding/json" "fmt" "io" "mime" @@ -16,6 +15,7 @@ import ( "strings" "time" + json "github.com/bytedance/sonic" "github.com/go-openapi/strfmt" "github.com/golang/mock/gomock" "github.com/google/uuid" @@ -112,7 +112,7 @@ var _ = Describe("prepareBody", func() { cluster := models.Cluster{ ID: &clusterID, } - clusterJson, err := json.Marshal(cluster) + clusterJson, err := json.ConfigStd.Marshal(cluster) Expect(err).NotTo(HaveOccurred()) buffer := &bytes.Buffer{} @@ -601,7 +601,7 @@ func checkHostsFile(db *gorm.DB, hostFile *testFile, clusterID strfmt.UUID) { if hostFile.expected { cluster, err := common.GetClusterFromDBWithHosts(db, clusterID) Expect(err).NotTo(HaveOccurred()) - expectedContents, err = json.MarshalIndent(cluster.Cluster.Hosts, "", " ") + expectedContents, err = json.ConfigStd.MarshalIndent(cluster.Cluster.Hosts, "", " ") Expect(err).NotTo(HaveOccurred()) } checkFileContents(hostFile, expectedContents) @@ -611,7 +611,7 @@ func checkInfraEnvFile(db *gorm.DB, infraenvFile *testFile, infraEnvID strfmt.UU if infraenvFile.expected { infraEnv, err := common.GetInfraEnvFromDB(db, infraEnvID) Expect(err).NotTo(HaveOccurred()) - expectedContents, err = json.Marshal(infraEnv.InfraEnv) + expectedContents, err = json.ConfigStd.Marshal(infraEnv.InfraEnv) Expect(err).NotTo(HaveOccurred()) } checkFileContents(infraenvFile, expectedContents) @@ -628,7 +628,7 @@ func checkClusterFile(db *gorm.DB, clusterFile *testFile, clusterID strfmt.UUID, cluster.EmailDomain = emailDomain } Expect(err).NotTo(HaveOccurred()) - expectedContents, err = json.Marshal(cluster.Cluster) + expectedContents, err = json.ConfigStd.Marshal(cluster.Cluster) Expect(err).NotTo(HaveOccurred()) } checkFileContents(clusterFile, expectedContents) diff --git a/internal/uploader/extract.go b/internal/uploader/extract.go index 22d74fa4416..4bcc5b6a9e2 100644 --- a/internal/uploader/extract.go +++ b/internal/uploader/extract.go @@ -3,12 +3,12 @@ package uploader import ( "archive/tar" "compress/gzip" - "encoding/json" "errors" "fmt" "io" "strings" + json "github.com/bytedance/sonic" eventModels "github.com/openshift/assisted-service/pkg/uploader/models" ) diff --git a/internal/usage/manager.go b/internal/usage/manager.go index 6011210b331..50f47b093ae 100644 --- a/internal/usage/manager.go +++ b/internal/usage/manager.go @@ -2,9 +2,9 @@ package usage import ( "context" - "encoding/json" "strings" + json "github.com/bytedance/sonic" "github.com/go-openapi/strfmt" "github.com/openshift/assisted-service/internal/common" "github.com/openshift/assisted-service/internal/stream" @@ -54,7 +54,7 @@ func (m *UsageManager) Remove(usages FeatureUsage, name string) { } func (m *UsageManager) Save(db *gorm.DB, clusterId strfmt.UUID, usages FeatureUsage) { - b, err := json.Marshal(usages) + b, err := json.ConfigStd.Marshal(usages) if err == nil { err = db.Model(&common.Cluster{}).Where("id = ?", clusterId).Update("feature_usage", string(b)).Error if err != nil { diff --git a/internal/versions/api_test.go b/internal/versions/api_test.go index 2743c20e5f4..ef2971e5532 100644 --- a/internal/versions/api_test.go +++ b/internal/versions/api_test.go @@ -2,12 +2,12 @@ package versions import ( "context" - "encoding/json" "errors" "os" "strings" "time" + json "github.com/bytedance/sonic" "github.com/go-openapi/swag" gomock "github.com/golang/mock/gomock" . "github.com/onsi/ginkgo" diff --git a/internal/versions/common.go b/internal/versions/common.go index cff0e8926ac..ca4f3c94aad 100644 --- a/internal/versions/common.go +++ b/internal/versions/common.go @@ -2,11 +2,11 @@ package versions import ( context "context" - "encoding/json" "fmt" "strings" "sync" + json "github.com/bytedance/sonic" "github.com/go-openapi/strfmt" "github.com/go-openapi/swag" "github.com/openshift/assisted-service/internal/common" diff --git a/vendor/github.com/bytedance/sonic/.codespellrc b/vendor/github.com/bytedance/sonic/.codespellrc new file mode 100644 index 00000000000..1ccef98d57e --- /dev/null +++ b/vendor/github.com/bytedance/sonic/.codespellrc @@ -0,0 +1,5 @@ +[codespell] +# ignore test files, go project names, binary files via `skip` and special var/regex via `ignore-words` +skip = fuzz,*_test.tmpl,testdata,*_test.go,go.mod,go.sum,*.gz +ignore-words = .github/workflows/.ignore_words +check-filenames = true diff --git a/vendor/github.com/bytedance/sonic/.gitignore b/vendor/github.com/bytedance/sonic/.gitignore new file mode 100644 index 00000000000..fa60f43a290 --- /dev/null +++ b/vendor/github.com/bytedance/sonic/.gitignore @@ -0,0 +1,55 @@ +*.o +*.swp +*.swm +*.swn +*.a +*.so +_obj +_test +*.[568vq] +[568vq].out +*.cgo1.go +*.cgo2.c +_cgo_defun.c +_cgo_gotypes.go +_cgo_export.* +_testmain.go +*.exe +*.exe~ +*.test +*.prof +*.rar +*.zip +*.gz +*.psd +*.bmd +*.cfg +*.pptx +*.log +*nohup.out +*settings.pyc +*.sublime-project +*.sublime-workspace +.DS_Store +/.idea/ +/.vscode/ +/output/ +/vendor/ +/Gopkg.lock +/Gopkg.toml +coverage.html +coverage.out +coverage.xml +junit.xml +*.profile +*.svg +*.out +ast/test.out +ast/bench.sh + +!testdata/*.json.gz +fuzz/testdata +*__debug_bin* +*pprof +*coverage.txt +tools/venv/* \ No newline at end of file diff --git a/vendor/github.com/bytedance/sonic/.gitmodules b/vendor/github.com/bytedance/sonic/.gitmodules new file mode 100644 index 00000000000..ea84b991a97 --- /dev/null +++ b/vendor/github.com/bytedance/sonic/.gitmodules @@ -0,0 +1,6 @@ +[submodule "cloudwego"] + path = tools/asm2asm + url = https://github.com/cloudwego/asm2asm.git +[submodule "tools/simde"] + path = tools/simde + url = https://github.com/simd-everywhere/simde.git diff --git a/vendor/github.com/bytedance/sonic/.licenserc.yaml b/vendor/github.com/bytedance/sonic/.licenserc.yaml new file mode 100644 index 00000000000..1cb993e3985 --- /dev/null +++ b/vendor/github.com/bytedance/sonic/.licenserc.yaml @@ -0,0 +1,24 @@ +header: + license: + spdx-id: Apache-2.0 + copyright-owner: ByteDance Inc. + + paths: + - '**/*.go' + - '**/*.s' + + paths-ignore: + - 'ast/asm.s' # empty file + - 'decoder/asm.s' # empty file + - 'encoder/asm.s' # empty file + - 'internal/caching/asm.s' # empty file + - 'internal/jit/asm.s' # empty file + - 'internal/native/avx/native_amd64.s' # auto-generated by asm2asm + - 'internal/native/avx/native_subr_amd64.go' # auto-generated by asm2asm + - 'internal/native/avx2/native_amd64.s' # auto-generated by asm2asm + - 'internal/native/avx2/native_subr_amd64.go' # auto-generated by asm2asm + - 'internal/resolver/asm.s' # empty file + - 'internal/rt/asm.s' # empty file + - 'internal/loader/asm.s' # empty file + + comment: on-failure \ No newline at end of file diff --git a/vendor/github.com/bytedance/sonic/CODE_OF_CONDUCT.md b/vendor/github.com/bytedance/sonic/CODE_OF_CONDUCT.md new file mode 100644 index 00000000000..8505feb1c8e --- /dev/null +++ b/vendor/github.com/bytedance/sonic/CODE_OF_CONDUCT.md @@ -0,0 +1,128 @@ +# Contributor Covenant Code of Conduct + +## Our Pledge + +We as members, contributors, and leaders pledge to make participation in our +community a harassment-free experience for everyone, regardless of age, body +size, visible or invisible disability, ethnicity, sex characteristics, gender +identity and expression, level of experience, education, socio-economic status, +nationality, personal appearance, race, religion, or sexual identity +and orientation. + +We pledge to act and interact in ways that contribute to an open, welcoming, +diverse, inclusive, and healthy community. + +## Our Standards + +Examples of behavior that contributes to a positive environment for our +community include: + +* Demonstrating empathy and kindness toward other people +* Being respectful of differing opinions, viewpoints, and experiences +* Giving and gracefully accepting constructive feedback +* Accepting responsibility and apologizing to those affected by our mistakes, + and learning from the experience +* Focusing on what is best not just for us as individuals, but for the + overall community + +Examples of unacceptable behavior include: + +* The use of sexualized language or imagery, and sexual attention or + advances of any kind +* Trolling, insulting or derogatory comments, and personal or political attacks +* Public or private harassment +* Publishing others' private information, such as a physical or email + address, without their explicit permission +* Other conduct which could reasonably be considered inappropriate in a + professional setting + +## Enforcement Responsibilities + +Community leaders are responsible for clarifying and enforcing our standards of +acceptable behavior and will take appropriate and fair corrective action in +response to any behavior that they deem inappropriate, threatening, offensive, +or harmful. + +Community leaders have the right and responsibility to remove, edit, or reject +comments, commits, code, wiki edits, issues, and other contributions that are +not aligned to this Code of Conduct, and will communicate reasons for moderation +decisions when appropriate. + +## Scope + +This Code of Conduct applies within all community spaces, and also applies when +an individual is officially representing the community in public spaces. +Examples of representing our community include using an official e-mail address, +posting via an official social media account, or acting as an appointed +representative at an online or offline event. + +## Enforcement + +Instances of abusive, harassing, or otherwise unacceptable behavior may be +reported to the community leaders responsible for enforcement at +wudi.daniel@bytedance.com. +All complaints will be reviewed and investigated promptly and fairly. + +All community leaders are obligated to respect the privacy and security of the +reporter of any incident. + +## Enforcement Guidelines + +Community leaders will follow these Community Impact Guidelines in determining +the consequences for any action they deem in violation of this Code of Conduct: + +### 1. Correction + +**Community Impact**: Use of inappropriate language or other behavior deemed +unprofessional or unwelcome in the community. + +**Consequence**: A private, written warning from community leaders, providing +clarity around the nature of the violation and an explanation of why the +behavior was inappropriate. A public apology may be requested. + +### 2. Warning + +**Community Impact**: A violation through a single incident or series +of actions. + +**Consequence**: A warning with consequences for continued behavior. No +interaction with the people involved, including unsolicited interaction with +those enforcing the Code of Conduct, for a specified period of time. This +includes avoiding interactions in community spaces as well as external channels +like social media. Violating these terms may lead to a temporary or +permanent ban. + +### 3. Temporary Ban + +**Community Impact**: A serious violation of community standards, including +sustained inappropriate behavior. + +**Consequence**: A temporary ban from any sort of interaction or public +communication with the community for a specified period of time. No public or +private interaction with the people involved, including unsolicited interaction +with those enforcing the Code of Conduct, is allowed during this period. +Violating these terms may lead to a permanent ban. + +### 4. Permanent Ban + +**Community Impact**: Demonstrating a pattern of violation of community +standards, including sustained inappropriate behavior, harassment of an +individual, or aggression toward or disparagement of classes of individuals. + +**Consequence**: A permanent ban from any sort of public interaction within +the community. + +## Attribution + +This Code of Conduct is adapted from the [Contributor Covenant][homepage], +version 2.0, available at +https://www.contributor-covenant.org/version/2/0/code_of_conduct.html. + +Community Impact Guidelines were inspired by [Mozilla's code of conduct +enforcement ladder](https://github.com/mozilla/diversity). + +[homepage]: https://www.contributor-covenant.org + +For answers to common questions about this code of conduct, see the FAQ at +https://www.contributor-covenant.org/faq. Translations are available at +https://www.contributor-covenant.org/translations. diff --git a/vendor/github.com/bytedance/sonic/CONTRIBUTING.md b/vendor/github.com/bytedance/sonic/CONTRIBUTING.md new file mode 100644 index 00000000000..7f63c661a85 --- /dev/null +++ b/vendor/github.com/bytedance/sonic/CONTRIBUTING.md @@ -0,0 +1,63 @@ +# How to Contribute + +## Your First Pull Request +We use GitHub for our codebase. You can start by reading [How To Pull Request](https://docs.github.com/en/github/collaborating-with-issues-and-pull-requests/about-pull-requests). + +## Without Semantic Versioning +We keep the stable code in branch `main` like `golang.org/x`. Development base on branch `develop`. We promise the **Forward Compatibility** by adding new package directory with suffix `v2/v3` when code has break changes. + +## Branch Organization +We use [git-flow](https://nvie.com/posts/a-successful-git-branching-model/) as our branch organization, as known as [FDD](https://en.wikipedia.org/wiki/Feature-driven_development) + + +## Bugs +### 1. How to Find Known Issues +We are using [Github Issues](https://github.com/bytedance/sonic/issues) for our public bugs. We keep a close eye on this and try to make it clear when we have an internal fix in progress. Before filing a new task, try to make sure your problem doesn’t already exist. + +### 2. Reporting New Issues +Providing a reduced test code is a recommended way for reporting issues. Then can be placed in: +- Just in issues +- [Golang Playground](https://play.golang.org/) + +### 3. Security Bugs +Please do not report the safe disclosure of bugs to public issues. Contact us by [Support Email](mailto:sonic@bytedance.com) + +## How to Get in Touch +- [Email](mailto:wudi.daniel@bytedance.com) + +## Submit a Pull Request +Before you submit your Pull Request (PR) consider the following guidelines: +1. Search [GitHub](https://github.com/bytedance/sonic/pulls) for an open or closed PR that relates to your submission. You don't want to duplicate existing efforts. +2. Be sure that an issue describes the problem you're fixing, or documents the design for the feature you'd like to add. Discussing the design upfront helps to ensure that we're ready to accept your work. +3. [Fork](https://docs.github.com/en/github/getting-started-with-github/fork-a-repo) the bytedance/sonic repo. +4. In your forked repository, make your changes in a new git branch: + ``` + git checkout -b bugfix/security_bug develop + ``` +5. Create your patch, including appropriate test cases. +6. Follow our [Style Guides](#code-style-guides). +7. Commit your changes using a descriptive commit message that follows [AngularJS Git Commit Message Conventions](https://docs.google.com/document/d/1QrDFcIiPjSLDn3EL15IJygNPiHORgU1_OOAqWjiDU5Y/edit). + Adherence to these conventions is necessary because release notes will be automatically generated from these messages. +8. Push your branch to GitHub: + ``` + git push origin bugfix/security_bug + ``` +9. In GitHub, send a pull request to `sonic:main` + +Note: you must use one of `optimize/feature/bugfix/doc/ci/test/refactor` following a slash(`/`) as the branch prefix. + +Your pr title and commit message should follow https://www.conventionalcommits.org/. + +## Contribution Prerequisites +- Our development environment keeps up with [Go Official](https://golang.org/project/). +- You need fully checking with lint tools before submit your pull request. [gofmt](https://golang.org/pkg/cmd/gofmt/) & [golangci-lint](https://github.com/golangci/golangci-lint) +- You are familiar with [Github](https://github.com) +- Maybe you need familiar with [Actions](https://github.com/features/actions)(our default workflow tool). + +## Code Style Guides +See [Go Code Review Comments](https://github.com/golang/go/wiki/CodeReviewComments). + +Good resources: +- [Effective Go](https://golang.org/doc/effective_go) +- [Pingcap General advice](https://pingcap.github.io/style-guide/general.html) +- [Uber Go Style Guide](https://github.com/uber-go/guide/blob/master/style.md) diff --git a/vendor/github.com/bytedance/sonic/CREDITS b/vendor/github.com/bytedance/sonic/CREDITS new file mode 100644 index 00000000000..e69de29bb2d diff --git a/vendor/github.com/bytedance/sonic/LICENSE b/vendor/github.com/bytedance/sonic/LICENSE new file mode 100644 index 00000000000..261eeb9e9f8 --- /dev/null +++ b/vendor/github.com/bytedance/sonic/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/vendor/github.com/bytedance/sonic/README.md b/vendor/github.com/bytedance/sonic/README.md new file mode 100644 index 00000000000..576c15bce1e --- /dev/null +++ b/vendor/github.com/bytedance/sonic/README.md @@ -0,0 +1,500 @@ +# Sonic + +English | [中文](README_ZH_CN.md) + +A blazingly fast JSON serializing & deserializing library, accelerated by JIT (just-in-time compiling) and SIMD (single-instruction-multiple-data). + +## Requirement + +- Go: 1.17~1.23 +- OS: Linux / MacOS / Windows +- CPU: AMD64 / ARM64(need go1.20 above) + +## Features + +- Runtime object binding without code generation +- Complete APIs for JSON value manipulation +- Fast, fast, fast! + +## APIs + +see [go.dev](https://pkg.go.dev/github.com/bytedance/sonic) + +## Benchmarks + +For **all sizes** of json and **all scenarios** of usage, **Sonic performs best**. + +- [Medium](https://github.com/bytedance/sonic/blob/main/decoder/testdata_test.go#L19) (13KB, 300+ key, 6 layers) + +```powershell +goversion: 1.17.1 +goos: darwin +goarch: amd64 +cpu: Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz +BenchmarkEncoder_Generic_Sonic-16 32393 ns/op 402.40 MB/s 11965 B/op 4 allocs/op +BenchmarkEncoder_Generic_Sonic_Fast-16 21668 ns/op 601.57 MB/s 10940 B/op 4 allocs/op +BenchmarkEncoder_Generic_JsonIter-16 42168 ns/op 309.12 MB/s 14345 B/op 115 allocs/op +BenchmarkEncoder_Generic_GoJson-16 65189 ns/op 199.96 MB/s 23261 B/op 16 allocs/op +BenchmarkEncoder_Generic_StdLib-16 106322 ns/op 122.60 MB/s 49136 B/op 789 allocs/op +BenchmarkEncoder_Binding_Sonic-16 6269 ns/op 2079.26 MB/s 14173 B/op 4 allocs/op +BenchmarkEncoder_Binding_Sonic_Fast-16 5281 ns/op 2468.16 MB/s 12322 B/op 4 allocs/op +BenchmarkEncoder_Binding_JsonIter-16 20056 ns/op 649.93 MB/s 9488 B/op 2 allocs/op +BenchmarkEncoder_Binding_GoJson-16 8311 ns/op 1568.32 MB/s 9481 B/op 1 allocs/op +BenchmarkEncoder_Binding_StdLib-16 16448 ns/op 792.52 MB/s 9479 B/op 1 allocs/op +BenchmarkEncoder_Parallel_Generic_Sonic-16 6681 ns/op 1950.93 MB/s 12738 B/op 4 allocs/op +BenchmarkEncoder_Parallel_Generic_Sonic_Fast-16 4179 ns/op 3118.99 MB/s 10757 B/op 4 allocs/op +BenchmarkEncoder_Parallel_Generic_JsonIter-16 9861 ns/op 1321.84 MB/s 14362 B/op 115 allocs/op +BenchmarkEncoder_Parallel_Generic_GoJson-16 18850 ns/op 691.52 MB/s 23278 B/op 16 allocs/op +BenchmarkEncoder_Parallel_Generic_StdLib-16 45902 ns/op 283.97 MB/s 49174 B/op 789 allocs/op +BenchmarkEncoder_Parallel_Binding_Sonic-16 1480 ns/op 8810.09 MB/s 13049 B/op 4 allocs/op +BenchmarkEncoder_Parallel_Binding_Sonic_Fast-16 1209 ns/op 10785.23 MB/s 11546 B/op 4 allocs/op +BenchmarkEncoder_Parallel_Binding_JsonIter-16 6170 ns/op 2112.58 MB/s 9504 B/op 2 allocs/op +BenchmarkEncoder_Parallel_Binding_GoJson-16 3321 ns/op 3925.52 MB/s 9496 B/op 1 allocs/op +BenchmarkEncoder_Parallel_Binding_StdLib-16 3739 ns/op 3486.49 MB/s 9480 B/op 1 allocs/op + +BenchmarkDecoder_Generic_Sonic-16 66812 ns/op 195.10 MB/s 57602 B/op 723 allocs/op +BenchmarkDecoder_Generic_Sonic_Fast-16 54523 ns/op 239.07 MB/s 49786 B/op 313 allocs/op +BenchmarkDecoder_Generic_StdLib-16 124260 ns/op 104.90 MB/s 50869 B/op 772 allocs/op +BenchmarkDecoder_Generic_JsonIter-16 91274 ns/op 142.81 MB/s 55782 B/op 1068 allocs/op +BenchmarkDecoder_Generic_GoJson-16 88569 ns/op 147.17 MB/s 66367 B/op 973 allocs/op +BenchmarkDecoder_Binding_Sonic-16 32557 ns/op 400.38 MB/s 28302 B/op 137 allocs/op +BenchmarkDecoder_Binding_Sonic_Fast-16 28649 ns/op 455.00 MB/s 24999 B/op 34 allocs/op +BenchmarkDecoder_Binding_StdLib-16 111437 ns/op 116.97 MB/s 10576 B/op 208 allocs/op +BenchmarkDecoder_Binding_JsonIter-16 35090 ns/op 371.48 MB/s 14673 B/op 385 allocs/op +BenchmarkDecoder_Binding_GoJson-16 28738 ns/op 453.59 MB/s 22039 B/op 49 allocs/op +BenchmarkDecoder_Parallel_Generic_Sonic-16 12321 ns/op 1057.91 MB/s 57233 B/op 723 allocs/op +BenchmarkDecoder_Parallel_Generic_Sonic_Fast-16 10644 ns/op 1224.64 MB/s 49362 B/op 313 allocs/op +BenchmarkDecoder_Parallel_Generic_StdLib-16 57587 ns/op 226.35 MB/s 50874 B/op 772 allocs/op +BenchmarkDecoder_Parallel_Generic_JsonIter-16 38666 ns/op 337.12 MB/s 55789 B/op 1068 allocs/op +BenchmarkDecoder_Parallel_Generic_GoJson-16 30259 ns/op 430.79 MB/s 66370 B/op 974 allocs/op +BenchmarkDecoder_Parallel_Binding_Sonic-16 5965 ns/op 2185.28 MB/s 27747 B/op 137 allocs/op +BenchmarkDecoder_Parallel_Binding_Sonic_Fast-16 5170 ns/op 2521.31 MB/s 24715 B/op 34 allocs/op +BenchmarkDecoder_Parallel_Binding_StdLib-16 27582 ns/op 472.58 MB/s 10576 B/op 208 allocs/op +BenchmarkDecoder_Parallel_Binding_JsonIter-16 13571 ns/op 960.51 MB/s 14685 B/op 385 allocs/op +BenchmarkDecoder_Parallel_Binding_GoJson-16 10031 ns/op 1299.51 MB/s 22111 B/op 49 allocs/op + +BenchmarkGetOne_Sonic-16 3276 ns/op 3975.78 MB/s 24 B/op 1 allocs/op +BenchmarkGetOne_Gjson-16 9431 ns/op 1380.81 MB/s 0 B/op 0 allocs/op +BenchmarkGetOne_Jsoniter-16 51178 ns/op 254.46 MB/s 27936 B/op 647 allocs/op +BenchmarkGetOne_Parallel_Sonic-16 216.7 ns/op 60098.95 MB/s 24 B/op 1 allocs/op +BenchmarkGetOne_Parallel_Gjson-16 1076 ns/op 12098.62 MB/s 0 B/op 0 allocs/op +BenchmarkGetOne_Parallel_Jsoniter-16 17741 ns/op 734.06 MB/s 27945 B/op 647 allocs/op +BenchmarkSetOne_Sonic-16 9571 ns/op 1360.61 MB/s 1584 B/op 17 allocs/op +BenchmarkSetOne_Sjson-16 36456 ns/op 357.22 MB/s 52180 B/op 9 allocs/op +BenchmarkSetOne_Jsoniter-16 79475 ns/op 163.86 MB/s 45862 B/op 964 allocs/op +BenchmarkSetOne_Parallel_Sonic-16 850.9 ns/op 15305.31 MB/s 1584 B/op 17 allocs/op +BenchmarkSetOne_Parallel_Sjson-16 18194 ns/op 715.77 MB/s 52247 B/op 9 allocs/op +BenchmarkSetOne_Parallel_Jsoniter-16 33560 ns/op 388.05 MB/s 45892 B/op 964 allocs/op +BenchmarkLoadNode/LoadAll()-16 11384 ns/op 1143.93 MB/s 6307 B/op 25 allocs/op +BenchmarkLoadNode_Parallel/LoadAll()-16 5493 ns/op 2370.68 MB/s 7145 B/op 25 allocs/op +BenchmarkLoadNode/Interface()-16 17722 ns/op 734.85 MB/s 13323 B/op 88 allocs/op +BenchmarkLoadNode_Parallel/Interface()-16 10330 ns/op 1260.70 MB/s 15178 B/op 88 allocs/op +``` + +- [Small](https://github.com/bytedance/sonic/blob/main/testdata/small.go) (400B, 11 keys, 3 layers) +![small benchmarks](./docs/imgs/bench-small.png) +- [Large](https://github.com/bytedance/sonic/blob/main/testdata/twitter.json) (635KB, 10000+ key, 6 layers) +![large benchmarks](./docs/imgs/bench-large.png) + +See [bench.sh](https://github.com/bytedance/sonic/blob/main/scripts/bench.sh) for benchmark codes. + +## How it works + +See [INTRODUCTION.md](./docs/INTRODUCTION.md). + +## Usage + +### Marshal/Unmarshal + +Default behaviors are mostly consistent with `encoding/json`, except HTML escaping form (see [Escape HTML](https://github.com/bytedance/sonic/blob/main/README.md#escape-html)) and `SortKeys` feature (optional support see [Sort Keys](https://github.com/bytedance/sonic/blob/main/README.md#sort-keys)) that is **NOT** in conformity to [RFC8259](https://datatracker.ietf.org/doc/html/rfc8259). + + ```go +import "github.com/bytedance/sonic" + +var data YourSchema +// Marshal +output, err := sonic.Marshal(&data) +// Unmarshal +err := sonic.Unmarshal(output, &data) + ``` + +### Streaming IO + +Sonic supports decoding json from `io.Reader` or encoding objects into `io.Writer`, aims at handling multiple values as well as reducing memory consumption. + +- encoder + +```go +var o1 = map[string]interface{}{ + "a": "b", +} +var o2 = 1 +var w = bytes.NewBuffer(nil) +var enc = sonic.ConfigDefault.NewEncoder(w) +enc.Encode(o1) +enc.Encode(o2) +fmt.Println(w.String()) +// Output: +// {"a":"b"} +// 1 +``` + +- decoder + +```go +var o = map[string]interface{}{} +var r = strings.NewReader(`{"a":"b"}{"1":"2"}`) +var dec = sonic.ConfigDefault.NewDecoder(r) +dec.Decode(&o) +dec.Decode(&o) +fmt.Printf("%+v", o) +// Output: +// map[1:2 a:b] +``` + +### Use Number/Use Int64 + + ```go +import "github.com/bytedance/sonic/decoder" + +var input = `1` +var data interface{} + +// default float64 +dc := decoder.NewDecoder(input) +dc.Decode(&data) // data == float64(1) +// use json.Number +dc = decoder.NewDecoder(input) +dc.UseNumber() +dc.Decode(&data) // data == json.Number("1") +// use int64 +dc = decoder.NewDecoder(input) +dc.UseInt64() +dc.Decode(&data) // data == int64(1) + +root, err := sonic.GetFromString(input) +// Get json.Number +jn := root.Number() +jm := root.InterfaceUseNumber().(json.Number) // jn == jm +// Get float64 +fn := root.Float64() +fm := root.Interface().(float64) // jn == jm + ``` + +### Sort Keys + +On account of the performance loss from sorting (roughly 10%), sonic doesn't enable this feature by default. If your component depends on it to work (like [zstd](https://github.com/facebook/zstd)), Use it like this: + +```go +import "github.com/bytedance/sonic" +import "github.com/bytedance/sonic/encoder" + +// Binding map only +m := map[string]interface{}{} +v, err := encoder.Encode(m, encoder.SortMapKeys) + +// Or ast.Node.SortKeys() before marshal +var root := sonic.Get(JSON) +err := root.SortKeys() +``` + +### Escape HTML + +On account of the performance loss (roughly 15%), sonic doesn't enable this feature by default. You can use `encoder.EscapeHTML` option to open this feature (align with `encoding/json.HTMLEscape`). + +```go +import "github.com/bytedance/sonic" + +v := map[string]string{"&&":"<>"} +ret, err := Encode(v, EscapeHTML) // ret == `{"\u0026\u0026":{"X":"\u003c\u003e"}}` +``` + +### Compact Format + +Sonic encodes primitive objects (struct/map...) as compact-format JSON by default, except marshaling `json.RawMessage` or `json.Marshaler`: sonic ensures validating their output JSON but **DO NOT** compacting them for performance concerns. We provide the option `encoder.CompactMarshaler` to add compacting process. + +### Print Error + +If there invalid syntax in input JSON, sonic will return `decoder.SyntaxError`, which supports pretty-printing of error position + +```go +import "github.com/bytedance/sonic" +import "github.com/bytedance/sonic/decoder" + +var data interface{} +err := sonic.UnmarshalString("[[[}]]", &data) +if err != nil { + /* One line by default */ + println(e.Error()) // "Syntax error at index 3: invalid char\n\n\t[[[}]]\n\t...^..\n" + /* Pretty print */ + if e, ok := err.(decoder.SyntaxError); ok { + /*Syntax error at index 3: invalid char + + [[[}]] + ...^.. + */ + print(e.Description()) + } else if me, ok := err.(*decoder.MismatchTypeError); ok { + // decoder.MismatchTypeError is new to Sonic v1.6.0 + print(me.Description()) + } +} +``` + +#### Mismatched Types [Sonic v1.6.0] + +If there a **mismatch-typed** value for a given key, sonic will report `decoder.MismatchTypeError` (if there are many, report the last one), but still skip wrong the value and keep decoding next JSON. + +```go +import "github.com/bytedance/sonic" +import "github.com/bytedance/sonic/decoder" + +var data = struct{ + A int + B int +}{} +err := UnmarshalString(`{"A":"1","B":1}`, &data) +println(err.Error()) // Mismatch type int with value string "at index 5: mismatched type with value\n\n\t{\"A\":\"1\",\"B\":1}\n\t.....^.........\n" +fmt.Printf("%+v", data) // {A:0 B:1} +``` + +### Ast.Node + +Sonic/ast.Node is a completely self-contained AST for JSON. It implements serialization and deserialization both and provides robust APIs for obtaining and modification of generic data. + +#### Get/Index + +Search partial JSON by given paths, which must be non-negative integer or string, or nil + +```go +import "github.com/bytedance/sonic" + +input := []byte(`{"key1":[{},{"key2":{"key3":[1,2,3]}}]}`) + +// no path, returns entire json +root, err := sonic.Get(input) +raw := root.Raw() // == string(input) + +// multiple paths +root, err := sonic.Get(input, "key1", 1, "key2") +sub := root.Get("key3").Index(2).Int64() // == 3 +``` + +**Tip**: since `Index()` uses offset to locate data, which is much faster than scanning like `Get()`, we suggest you use it as much as possible. And sonic also provides another API `IndexOrGet()` to underlying use offset as well as ensure the key is matched. + +#### SearchOption + +`Searcher` provides some options for user to meet different needs: + +```go +opts := ast.SearchOption{ CopyReturn: true ... } +val, err := sonic.GetWithOptions(JSON, opts, "key") +``` + +- CopyReturn +Indicate the searcher to copy the result JSON string instead of refer from the input. This can help to reduce memory usage if you cache the results +- ConcurentRead +Since `ast.Node` use `Lazy-Load` design, it doesn't support Concurrently-Read by default. If you want to read it concurrently, please specify it. +- ValidateJSON +Indicate the searcher to validate the entire JSON. This option is enabled by default, which slow down the search speed a little. + +#### Set/Unset + +Modify the json content by Set()/Unset() + +```go +import "github.com/bytedance/sonic" + +// Set +exist, err := root.Set("key4", NewBool(true)) // exist == false +alias1 := root.Get("key4") +println(alias1.Valid()) // true +alias2 := root.Index(1) +println(alias1 == alias2) // true + +// Unset +exist, err := root.UnsetByIndex(1) // exist == true +println(root.Get("key4").Check()) // "value not exist" +``` + +#### Serialize + +To encode `ast.Node` as json, use `MarshalJson()` or `json.Marshal()` (MUST pass the node's pointer) + +```go +import ( + "encoding/json" + "github.com/bytedance/sonic" +) + +buf, err := root.MarshalJson() +println(string(buf)) // {"key1":[{},{"key2":{"key3":[1,2,3]}}]} +exp, err := json.Marshal(&root) // WARN: use pointer +println(string(buf) == string(exp)) // true +``` + +#### APIs + +- validation: `Check()`, `Error()`, `Valid()`, `Exist()` +- searching: `Index()`, `Get()`, `IndexPair()`, `IndexOrGet()`, `GetByPath()` +- go-type casting: `Int64()`, `Float64()`, `String()`, `Number()`, `Bool()`, `Map[UseNumber|UseNode]()`, `Array[UseNumber|UseNode]()`, `Interface[UseNumber|UseNode]()` +- go-type packing: `NewRaw()`, `NewNumber()`, `NewNull()`, `NewBool()`, `NewString()`, `NewObject()`, `NewArray()` +- iteration: `Values()`, `Properties()`, `ForEach()`, `SortKeys()` +- modification: `Set()`, `SetByIndex()`, `Add()` + +### Ast.Visitor + +Sonic provides an advanced API for fully parsing JSON into non-standard types (neither `struct` not `map[string]interface{}`) without using any intermediate representation (`ast.Node` or `interface{}`). For example, you might have the following types which are like `interface{}` but actually not `interface{}`: + +```go +type UserNode interface {} + +// the following types implement the UserNode interface. +type ( + UserNull struct{} + UserBool struct{ Value bool } + UserInt64 struct{ Value int64 } + UserFloat64 struct{ Value float64 } + UserString struct{ Value string } + UserObject struct{ Value map[string]UserNode } + UserArray struct{ Value []UserNode } +) +``` + +Sonic provides the following API to return **the preorder traversal of a JSON AST**. The `ast.Visitor` is a SAX style interface which is used in some C++ JSON library. You should implement `ast.Visitor` by yourself and pass it to `ast.Preorder()` method. In your visitor you can make your custom types to represent JSON values. There may be an O(n) space container (such as stack) in your visitor to record the object / array hierarchy. + +```go +func Preorder(str string, visitor Visitor, opts *VisitorOptions) error + +type Visitor interface { + OnNull() error + OnBool(v bool) error + OnString(v string) error + OnInt64(v int64, n json.Number) error + OnFloat64(v float64, n json.Number) error + OnObjectBegin(capacity int) error + OnObjectKey(key string) error + OnObjectEnd() error + OnArrayBegin(capacity int) error + OnArrayEnd() error +} +``` + +See [ast/visitor.go](https://github.com/bytedance/sonic/blob/main/ast/visitor.go) for detailed usage. We also implement a demo visitor for `UserNode` in [ast/visitor_test.go](https://github.com/bytedance/sonic/blob/main/ast/visitor_test.go). + +## Compatibility + +For developers who want to use sonic to meet diffirent scenarios, we provide some integrated configs as `sonic.API` + +- `ConfigDefault`: the sonic's default config (`EscapeHTML=false`,`SortKeys=false`...) to run sonic fast meanwhile ensure security. +- `ConfigStd`: the std-compatible config (`EscapeHTML=true`,`SortKeys=true`...) +- `ConfigFastest`: the fastest config (`NoQuoteTextMarshaler=true`) to run on sonic as fast as possible. +Sonic **DOES NOT** ensure to support all environments, due to the difficulty of developing high-performance codes. On non-sonic-supporting environment, the implementation will fall back to `encoding/json`. Thus beflow configs will all equal to `ConfigStd`. + +## Tips + +### Pretouch + +Since Sonic uses [golang-asm](https://github.com/twitchyliquid64/golang-asm) as a JIT assembler, which is NOT very suitable for runtime compiling, first-hit running of a huge schema may cause request-timeout or even process-OOM. For better stability, we advise **using `Pretouch()` for huge-schema or compact-memory applications** before `Marshal()/Unmarshal()`. + +```go +import ( + "reflect" + "github.com/bytedance/sonic" + "github.com/bytedance/sonic/option" +) + +func init() { + var v HugeStruct + + // For most large types (nesting depth <= option.DefaultMaxInlineDepth) + err := sonic.Pretouch(reflect.TypeOf(v)) + + // with more CompileOption... + err := sonic.Pretouch(reflect.TypeOf(v), + // If the type is too deep nesting (nesting depth > option.DefaultMaxInlineDepth), + // you can set compile recursive loops in Pretouch for better stability in JIT. + option.WithCompileRecursiveDepth(loop), + // For a large nested struct, try to set a smaller depth to reduce compiling time. + option.WithCompileMaxInlineDepth(depth), + ) +} +``` + +### Copy string + +When decoding **string values without any escaped characters**, sonic references them from the origin JSON buffer instead of mallocing a new buffer to copy. This helps a lot for CPU performance but may leave the whole JSON buffer in memory as long as the decoded objects are being used. In practice, we found the extra memory introduced by referring JSON buffer is usually 20% ~ 80% of decoded objects. Once an application holds these objects for a long time (for example, cache the decoded objects for reusing), its in-use memory on the server may go up. - `Config.CopyString`/`decoder.CopyString()`: We provide the option for `Decode()` / `Unmarshal()` users to choose not to reference the JSON buffer, which may cause a decline in CPU performance to some degree. + +- `GetFromStringNoCopy()`: For memory safety, `sonic.Get()` / `sonic.GetFromString()` now copies return JSON. If users want to get json more quickly and not care about memory usage, you can use `GetFromStringNoCopy()` to return a JSON directly referenced from source. + +### Pass string or []byte? + +For alignment to `encoding/json`, we provide API to pass `[]byte` as an argument, but the string-to-bytes copy is conducted at the same time considering safety, which may lose performance when the origin JSON is huge. Therefore, you can use `UnmarshalString()` and `GetFromString()` to pass a string, as long as your origin data is a string or **nocopy-cast** is safe for your []byte. We also provide API `MarshalString()` for convenient **nocopy-cast** of encoded JSON []byte, which is safe since sonic's output bytes is always duplicated and unique. + +### Accelerate `encoding.TextMarshaler` + +To ensure data security, sonic.Encoder quotes and escapes string values from `encoding.TextMarshaler` interfaces by default, which may degrade performance much if most of your data is in form of them. We provide `encoder.NoQuoteTextMarshaler` to skip these operations, which means you **MUST** ensure their output string escaped and quoted following [RFC8259](https://datatracker.ietf.org/doc/html/rfc8259). + +### Better performance for generic data + +In **fully-parsed** scenario, `Unmarshal()` performs better than `Get()`+`Node.Interface()`. But if you only have a part of the schema for specific json, you can combine `Get()` and `Unmarshal()` together: + +```go +import "github.com/bytedance/sonic" + +node, err := sonic.GetFromString(_TwitterJson, "statuses", 3, "user") +var user User // your partial schema... +err = sonic.UnmarshalString(node.Raw(), &user) +``` + +Even if you don't have any schema, use `ast.Node` as the container of generic values instead of `map` or `interface`: + +```go +import "github.com/bytedance/sonic" + +root, err := sonic.GetFromString(_TwitterJson) +user := root.GetByPath("statuses", 3, "user") // === root.Get("status").Index(3).Get("user") +err = user.Check() + +// err = user.LoadAll() // only call this when you want to use 'user' concurrently... +go someFunc(user) +``` + +Why? Because `ast.Node` stores its children using `array`: + +- `Array`'s performance is **much better** than `Map` when Inserting (Deserialize) and Scanning (Serialize) data; +- **Hashing** (`map[x]`) is not as efficient as **Indexing** (`array[x]`), which `ast.Node` can conduct on **both array and object**; +- Using `Interface()`/`Map()` means Sonic must parse all the underlying values, while `ast.Node` can parse them **on demand**. + +**CAUTION:** `ast.Node` **DOESN'T** ensure concurrent security directly, due to its **lazy-load** design. However, you can call `Node.Load()`/`Node.LoadAll()` to achieve that, which may bring performance reduction while it still works faster than converting to `map` or `interface{}` + +### Ast.Node or Ast.Visitor? + +For generic data, `ast.Node` should be enough for your needs in most cases. + +However, `ast.Node` is designed for partially processing JSON string. It has some special designs such as lazy-load which might not be suitable for directly parsing the whole JSON string like `Unmarshal()`. Although `ast.Node` is better then `map` or `interface{}`, it's also a kind of intermediate representation after all if your final types are customized and you have to convert the above types to your custom types after parsing. + +For better performance, in previous case the `ast.Visitor` will be the better choice. It performs JSON decoding like `Unmarshal()` and you can directly use your final types to represents a JSON AST without any intermediate representations. + +But `ast.Visitor` is not a very handy API. You might need to write a lot of code to implement your visitor and carefully maintain the tree hierarchy during decoding. Please read the comments in [ast/visitor.go](https://github.com/bytedance/sonic/blob/main/ast/visitor.go) carefully if you decide to use this API. + +### Buffer Size + +Sonic use memory pool in many places like `encoder.Encode`, `ast.Node.MarshalJSON` to improve performance, which may produce more memory usage (in-use) when server's load is high. See [issue 614](https://github.com/bytedance/sonic/issues/614). Therefore, we introduce some options to let user control the behavior of memory pool. See [option](https://pkg.go.dev/github.com/bytedance/sonic@v1.11.9/option#pkg-variables) package. + +### Faster JSON Skip + +For security, sonic use [FSM](native/skip_one.c) algorithm to validate JSON when decoding raw JSON or encoding `json.Marshaler`, which is much slower (1~10x) than [SIMD-searching-pair](native/skip_one_fast.c) algorithm. If user has many redundant JSON value and DO NOT NEED to strictly validate JSON correctness, you can enable below options: + +- `Config.NoValidateSkipJSON`: for faster skipping JSON when decoding, such as unknown fields, json.Unmarshaler(json.RawMessage), mismatched values, and redundant array elements +- `Config.NoValidateJSONMarshaler`: avoid validating JSON when encoding `json.Marshaler` +- `SearchOption.ValidateJSON`: indicates if validate located JSON value when `Get` + +## JSON-Path Support (GJSON) + +[tidwall/gjson](https://github.com/tidwall/gjson) has provided a comprehensive and popular JSON-Path API, and + a lot of older codes heavily relies on it. Therefore, we provides a wrapper library, which combines gjson's API with sonic's SIMD algorithm to boost up the performance. See [cloudwego/gjson](https://github.com/cloudwego/gjson). + +## Community + +Sonic is a subproject of [CloudWeGo](https://www.cloudwego.io/). We are committed to building a cloud native ecosystem. diff --git a/vendor/github.com/bytedance/sonic/README_ZH_CN.md b/vendor/github.com/bytedance/sonic/README_ZH_CN.md new file mode 100644 index 00000000000..cf6e8076407 --- /dev/null +++ b/vendor/github.com/bytedance/sonic/README_ZH_CN.md @@ -0,0 +1,493 @@ +# Sonic + +[English](README.md) | 中文 + +一个速度奇快的 JSON 序列化/反序列化库,由 JIT (即时编译)和 SIMD (单指令流多数据流)加速。 + +## 依赖 + +- Go: 1.17~1.23 +- OS: Linux / MacOS / Windows +- CPU: AMD64 / ARM64(需要 Go1.20 以上) + +## 接口 + +详见 [go.dev](https://pkg.go.dev/github.com/bytedance/sonic) + +## 特色 + +- 运行时对象绑定,无需代码生成 +- 完备的 JSON 操作 API +- 快,更快,还要更快! + +## 基准测试 + +对于**所有大小**的 json 和**所有使用场景**, **Sonic 表现均为最佳**。 + +- [中型](https://github.com/bytedance/sonic/blob/main/decoder/testdata_test.go#L19) (13kB, 300+ 键, 6 层) + +```powershell +goversion: 1.17.1 +goos: darwin +goarch: amd64 +cpu: Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz +BenchmarkEncoder_Generic_Sonic-16 32393 ns/op 402.40 MB/s 11965 B/op 4 allocs/op +BenchmarkEncoder_Generic_Sonic_Fast-16 21668 ns/op 601.57 MB/s 10940 B/op 4 allocs/op +BenchmarkEncoder_Generic_JsonIter-16 42168 ns/op 309.12 MB/s 14345 B/op 115 allocs/op +BenchmarkEncoder_Generic_GoJson-16 65189 ns/op 199.96 MB/s 23261 B/op 16 allocs/op +BenchmarkEncoder_Generic_StdLib-16 106322 ns/op 122.60 MB/s 49136 B/op 789 allocs/op +BenchmarkEncoder_Binding_Sonic-16 6269 ns/op 2079.26 MB/s 14173 B/op 4 allocs/op +BenchmarkEncoder_Binding_Sonic_Fast-16 5281 ns/op 2468.16 MB/s 12322 B/op 4 allocs/op +BenchmarkEncoder_Binding_JsonIter-16 20056 ns/op 649.93 MB/s 9488 B/op 2 allocs/op +BenchmarkEncoder_Binding_GoJson-16 8311 ns/op 1568.32 MB/s 9481 B/op 1 allocs/op +BenchmarkEncoder_Binding_StdLib-16 16448 ns/op 792.52 MB/s 9479 B/op 1 allocs/op +BenchmarkEncoder_Parallel_Generic_Sonic-16 6681 ns/op 1950.93 MB/s 12738 B/op 4 allocs/op +BenchmarkEncoder_Parallel_Generic_Sonic_Fast-16 4179 ns/op 3118.99 MB/s 10757 B/op 4 allocs/op +BenchmarkEncoder_Parallel_Generic_JsonIter-16 9861 ns/op 1321.84 MB/s 14362 B/op 115 allocs/op +BenchmarkEncoder_Parallel_Generic_GoJson-16 18850 ns/op 691.52 MB/s 23278 B/op 16 allocs/op +BenchmarkEncoder_Parallel_Generic_StdLib-16 45902 ns/op 283.97 MB/s 49174 B/op 789 allocs/op +BenchmarkEncoder_Parallel_Binding_Sonic-16 1480 ns/op 8810.09 MB/s 13049 B/op 4 allocs/op +BenchmarkEncoder_Parallel_Binding_Sonic_Fast-16 1209 ns/op 10785.23 MB/s 11546 B/op 4 allocs/op +BenchmarkEncoder_Parallel_Binding_JsonIter-16 6170 ns/op 2112.58 MB/s 9504 B/op 2 allocs/op +BenchmarkEncoder_Parallel_Binding_GoJson-16 3321 ns/op 3925.52 MB/s 9496 B/op 1 allocs/op +BenchmarkEncoder_Parallel_Binding_StdLib-16 3739 ns/op 3486.49 MB/s 9480 B/op 1 allocs/op + +BenchmarkDecoder_Generic_Sonic-16 66812 ns/op 195.10 MB/s 57602 B/op 723 allocs/op +BenchmarkDecoder_Generic_Sonic_Fast-16 54523 ns/op 239.07 MB/s 49786 B/op 313 allocs/op +BenchmarkDecoder_Generic_StdLib-16 124260 ns/op 104.90 MB/s 50869 B/op 772 allocs/op +BenchmarkDecoder_Generic_JsonIter-16 91274 ns/op 142.81 MB/s 55782 B/op 1068 allocs/op +BenchmarkDecoder_Generic_GoJson-16 88569 ns/op 147.17 MB/s 66367 B/op 973 allocs/op +BenchmarkDecoder_Binding_Sonic-16 32557 ns/op 400.38 MB/s 28302 B/op 137 allocs/op +BenchmarkDecoder_Binding_Sonic_Fast-16 28649 ns/op 455.00 MB/s 24999 B/op 34 allocs/op +BenchmarkDecoder_Binding_StdLib-16 111437 ns/op 116.97 MB/s 10576 B/op 208 allocs/op +BenchmarkDecoder_Binding_JsonIter-16 35090 ns/op 371.48 MB/s 14673 B/op 385 allocs/op +BenchmarkDecoder_Binding_GoJson-16 28738 ns/op 453.59 MB/s 22039 B/op 49 allocs/op +BenchmarkDecoder_Parallel_Generic_Sonic-16 12321 ns/op 1057.91 MB/s 57233 B/op 723 allocs/op +BenchmarkDecoder_Parallel_Generic_Sonic_Fast-16 10644 ns/op 1224.64 MB/s 49362 B/op 313 allocs/op +BenchmarkDecoder_Parallel_Generic_StdLib-16 57587 ns/op 226.35 MB/s 50874 B/op 772 allocs/op +BenchmarkDecoder_Parallel_Generic_JsonIter-16 38666 ns/op 337.12 MB/s 55789 B/op 1068 allocs/op +BenchmarkDecoder_Parallel_Generic_GoJson-16 30259 ns/op 430.79 MB/s 66370 B/op 974 allocs/op +BenchmarkDecoder_Parallel_Binding_Sonic-16 5965 ns/op 2185.28 MB/s 27747 B/op 137 allocs/op +BenchmarkDecoder_Parallel_Binding_Sonic_Fast-16 5170 ns/op 2521.31 MB/s 24715 B/op 34 allocs/op +BenchmarkDecoder_Parallel_Binding_StdLib-16 27582 ns/op 472.58 MB/s 10576 B/op 208 allocs/op +BenchmarkDecoder_Parallel_Binding_JsonIter-16 13571 ns/op 960.51 MB/s 14685 B/op 385 allocs/op +BenchmarkDecoder_Parallel_Binding_GoJson-16 10031 ns/op 1299.51 MB/s 22111 B/op 49 allocs/op + +BenchmarkGetOne_Sonic-16 3276 ns/op 3975.78 MB/s 24 B/op 1 allocs/op +BenchmarkGetOne_Gjson-16 9431 ns/op 1380.81 MB/s 0 B/op 0 allocs/op +BenchmarkGetOne_Jsoniter-16 51178 ns/op 254.46 MB/s 27936 B/op 647 allocs/op +BenchmarkGetOne_Parallel_Sonic-16 216.7 ns/op 60098.95 MB/s 24 B/op 1 allocs/op +BenchmarkGetOne_Parallel_Gjson-16 1076 ns/op 12098.62 MB/s 0 B/op 0 allocs/op +BenchmarkGetOne_Parallel_Jsoniter-16 17741 ns/op 734.06 MB/s 27945 B/op 647 allocs/op +BenchmarkSetOne_Sonic-16 9571 ns/op 1360.61 MB/s 1584 B/op 17 allocs/op +BenchmarkSetOne_Sjson-16 36456 ns/op 357.22 MB/s 52180 B/op 9 allocs/op +BenchmarkSetOne_Jsoniter-16 79475 ns/op 163.86 MB/s 45862 B/op 964 allocs/op +BenchmarkSetOne_Parallel_Sonic-16 850.9 ns/op 15305.31 MB/s 1584 B/op 17 allocs/op +BenchmarkSetOne_Parallel_Sjson-16 18194 ns/op 715.77 MB/s 52247 B/op 9 allocs/op +BenchmarkSetOne_Parallel_Jsoniter-16 33560 ns/op 388.05 MB/s 45892 B/op 964 allocs/op +BenchmarkLoadNode/LoadAll()-16 11384 ns/op 1143.93 MB/s 6307 B/op 25 allocs/op +BenchmarkLoadNode_Parallel/LoadAll()-16 5493 ns/op 2370.68 MB/s 7145 B/op 25 allocs/op +BenchmarkLoadNode/Interface()-16 17722 ns/op 734.85 MB/s 13323 B/op 88 allocs/op +BenchmarkLoadNode_Parallel/Interface()-16 10330 ns/op 1260.70 MB/s 15178 B/op 88 allocs/op +``` + +- [小型](https://github.com/bytedance/sonic/blob/main/testdata/small.go) (400B, 11 个键, 3 层) +![small benchmarks](./docs/imgs/bench-small.png) +- [大型](https://github.com/bytedance/sonic/blob/main/testdata/twitter.json) (635kB, 10000+ 个键, 6 层) +![large benchmarks](./docs/imgs/bench-large.png) + +要查看基准测试代码,请参阅 [bench.sh](https://github.com/bytedance/sonic/blob/main/scripts/bench.sh) 。 + +## 工作原理 + +请参阅 [INTRODUCTION_ZH_CN.md](./docs/INTRODUCTION_ZH_CN.md). + +## 使用方式 + +### 序列化/反序列化 + +默认的行为基本上与 `encoding/json` 相一致,除了 HTML 转义形式(参见 [Escape HTML](https://github.com/bytedance/sonic/blob/main/README.md#escape-html)) 和 `SortKeys` 功能(参见 [Sort Keys](https://github.com/bytedance/sonic/blob/main/README.md#sort-keys))**没有**遵循 [RFC8259](https://datatracker.ietf.org/doc/html/rfc8259) 。 + + ```go +import "github.com/bytedance/sonic" + +var data YourSchema +// Marshal +output, err := sonic.Marshal(&data) +// Unmarshal +err := sonic.Unmarshal(output, &data) + ``` + +### 流式输入输出 + +Sonic 支持解码 `io.Reader` 中输入的 json,或将对象编码为 json 后输出至 `io.Writer`,以处理多个值并减少内存消耗。 + +- 编码器 + +```go +var o1 = map[string]interface{}{ + "a": "b", +} +var o2 = 1 +var w = bytes.NewBuffer(nil) +var enc = sonic.ConfigDefault.NewEncoder(w) +enc.Encode(o1) +enc.Encode(o2) +fmt.Println(w.String()) +// Output: +// {"a":"b"} +// 1 +``` + +- 解码器 + +```go +var o = map[string]interface{}{} +var r = strings.NewReader(`{"a":"b"}{"1":"2"}`) +var dec = sonic.ConfigDefault.NewDecoder(r) +dec.Decode(&o) +dec.Decode(&o) +fmt.Printf("%+v", o) +// Output: +// map[1:2 a:b] +``` + +### 使用 `Number` / `int64` + +```go +import "github.com/bytedance/sonic/decoder" + +var input = `1` +var data interface{} + +// default float64 +dc := decoder.NewDecoder(input) +dc.Decode(&data) // data == float64(1) +// use json.Number +dc = decoder.NewDecoder(input) +dc.UseNumber() +dc.Decode(&data) // data == json.Number("1") +// use int64 +dc = decoder.NewDecoder(input) +dc.UseInt64() +dc.Decode(&data) // data == int64(1) + +root, err := sonic.GetFromString(input) +// Get json.Number +jn := root.Number() +jm := root.InterfaceUseNumber().(json.Number) // jn == jm +// Get float64 +fn := root.Float64() +fm := root.Interface().(float64) // jn == jm + ``` + +### 对键排序 + +考虑到排序带来的性能损失(约 10% ), sonic 默认不会启用这个功能。如果你的组件依赖这个行为(如 [zstd](https://github.com/facebook/zstd)) ,可以仿照下面的例子: + +```go +import "github.com/bytedance/sonic" +import "github.com/bytedance/sonic/encoder" + +// Binding map only +m := map[string]interface{}{} +v, err := encoder.Encode(m, encoder.SortMapKeys) + +// Or ast.Node.SortKeys() before marshal +var root := sonic.Get(JSON) +err := root.SortKeys() +``` + +### HTML 转义 + +考虑到性能损失(约15%), sonic 默认不会启用这个功能。你可以使用 `encoder.EscapeHTML` 选项来开启(与 `encoding/json.HTMLEscape` 行为一致)。 + +```go +import "github.com/bytedance/sonic" + +v := map[string]string{"&&":"<>"} +ret, err := Encode(v, EscapeHTML) // ret == `{"\u0026\u0026":{"X":"\u003c\u003e"}}` +``` + +### 紧凑格式 + +Sonic 默认将基本类型( `struct` , `map` 等)编码为紧凑格式的 JSON ,除非使用 `json.RawMessage` or `json.Marshaler` 进行编码: sonic 确保输出的 JSON 合法,但出于性能考虑,**不会**加工成紧凑格式。我们提供选项 `encoder.CompactMarshaler` 来添加此过程, + +### 打印错误 + +如果输入的 JSON 存在无效的语法,sonic 将返回 `decoder.SyntaxError`,该错误支持错误位置的美化输出。 + +```go +import "github.com/bytedance/sonic" +import "github.com/bytedance/sonic/decoder" + +var data interface{} +err := sonic.UnmarshalString("[[[}]]", &data) +if err != nil { + /* One line by default */ + println(e.Error()) // "Syntax error at index 3: invalid char\n\n\t[[[}]]\n\t...^..\n" + /* Pretty print */ + if e, ok := err.(decoder.SyntaxError); ok { + /*Syntax error at index 3: invalid char + + [[[}]] + ...^.. + */ + print(e.Description()) + } else if me, ok := err.(*decoder.MismatchTypeError); ok { + // decoder.MismatchTypeError is new to Sonic v1.6.0 + print(me.Description()) + } +} +``` + +#### 类型不匹配 [Sonic v1.6.0] + +如果给定键中存在**类型不匹配**的值, sonic 会抛出 `decoder.MismatchTypeError` (如果有多个,只会报告最后一个),但仍会跳过错误的值并解码下一个 JSON 。 + +```go +import "github.com/bytedance/sonic" +import "github.com/bytedance/sonic/decoder" + +var data = struct{ + A int + B int +}{} +err := UnmarshalString(`{"A":"1","B":1}`, &data) +println(err.Error()) // Mismatch type int with value string "at index 5: mismatched type with value\n\n\t{\"A\":\"1\",\"B\":1}\n\t.....^.........\n" +fmt.Printf("%+v", data) // {A:0 B:1} +``` + +### `Ast.Node` + +Sonic/ast.Node 是完全独立的 JSON 抽象语法树库。它实现了序列化和反序列化,并提供了获取和修改JSON数据的鲁棒的 API。 + +#### 查找/索引 + +通过给定的路径搜索 JSON 片段,路径必须为非负整数,字符串或 `nil` 。 + +```go +import "github.com/bytedance/sonic" + +input := []byte(`{"key1":[{},{"key2":{"key3":[1,2,3]}}]}`) + +// no path, returns entire json +root, err := sonic.Get(input) +raw := root.Raw() // == string(input) + +// multiple paths +root, err := sonic.Get(input, "key1", 1, "key2") +sub := root.Get("key3").Index(2).Int64() // == 3 +``` + +**注意**:由于 `Index()` 使用偏移量来定位数据,比使用扫描的 `Get()` 要快的多,建议尽可能的使用 `Index` 。 Sonic 也提供了另一个 API, `IndexOrGet()` ,以偏移量为基础并且也确保键的匹配。 + +#### 查找选项 + +`ast.Searcher`提供了一些选项,以满足用户的不同需求: + +```go +opts := ast.SearchOption{CopyReturn: true…} +val, err := sonic.GetWithOptions(JSON, opts, "key") +``` + +- CopyReturn +指示搜索器复制结果JSON字符串,而不是从输入引用。如果用户缓存结果,这有助于减少内存使用 +- ConcurentRead +因为`ast.Node`使用`Lazy-Load`设计,默认不支持并发读取。如果您想同时读取,请指定它。 +- ValidateJSON +指示搜索器来验证整个JSON。默认情况下启用该选项, 但是对于查找速度有一定影响。 + +#### 修改 + +使用 `Set()` / `Unset()` 修改 json 的内容 + +```go +import "github.com/bytedance/sonic" + +// Set +exist, err := root.Set("key4", NewBool(true)) // exist == false +alias1 := root.Get("key4") +println(alias1.Valid()) // true +alias2 := root.Index(1) +println(alias1 == alias2) // true + +// Unset +exist, err := root.UnsetByIndex(1) // exist == true +println(root.Get("key4").Check()) // "value not exist" +``` + +#### 序列化 + +要将 `ast.Node` 编码为 json ,使用 `MarshalJson()` 或者 `json.Marshal()` (必须传递指向节点的指针) + +```go +import ( + "encoding/json" + "github.com/bytedance/sonic" +) + +buf, err := root.MarshalJson() +println(string(buf)) // {"key1":[{},{"key2":{"key3":[1,2,3]}}]} +exp, err := json.Marshal(&root) // WARN: use pointer +println(string(buf) == string(exp)) // true +``` + +#### APIs + +- 合法性检查: `Check()`, `Error()`, `Valid()`, `Exist()` +- 索引: `Index()`, `Get()`, `IndexPair()`, `IndexOrGet()`, `GetByPath()` +- 转换至 go 内置类型: `Int64()`, `Float64()`, `String()`, `Number()`, `Bool()`, `Map[UseNumber|UseNode]()`, `Array[UseNumber|UseNode]()`, `Interface[UseNumber|UseNode]()` +- go 类型打包: `NewRaw()`, `NewNumber()`, `NewNull()`, `NewBool()`, `NewString()`, `NewObject()`, `NewArray()` +- 迭代: `Values()`, `Properties()`, `ForEach()`, `SortKeys()` +- 修改: `Set()`, `SetByIndex()`, `Add()` + +### `Ast.Visitor` + +Sonic 提供了一个高级的 API 用于直接全量解析 JSON 到非标准容器里 (既不是 `struct` 也不是 `map[string]interface{}`) 且不需要借助任何中间表示 (`ast.Node` 或 `interface{}`)。举个例子,你可能定义了下述的类型,它们看起来像 `interface{}`,但实际上并不是: + +```go +type UserNode interface {} + +// the following types implement the UserNode interface. +type ( + UserNull struct{} + UserBool struct{ Value bool } + UserInt64 struct{ Value int64 } + UserFloat64 struct{ Value float64 } + UserString struct{ Value string } + UserObject struct{ Value map[string]UserNode } + UserArray struct{ Value []UserNode } +) +``` + +Sonic 提供了下述的 API 来返回 **“对 JSON AST 的前序遍历”**。`ast.Visitor` 是一个 SAX 风格的接口,这在某些 C++ 的 JSON 解析库中被使用到。你需要自己实现一个 `ast.Visitor`,将它传递给 `ast.Preorder()` 方法。在你的实现中你可以使用自定义的类型来表示 JSON 的值。在你的 `ast.Visitor` 中,可能需要有一个 O(n) 空间复杂度的容器(比如说栈)来记录 object / array 的层级。 + +```go +func Preorder(str string, visitor Visitor, opts *VisitorOptions) error + +type Visitor interface { + OnNull() error + OnBool(v bool) error + OnString(v string) error + OnInt64(v int64, n json.Number) error + OnFloat64(v float64, n json.Number) error + OnObjectBegin(capacity int) error + OnObjectKey(key string) error + OnObjectEnd() error + OnArrayBegin(capacity int) error + OnArrayEnd() error +} +``` + +详细用法参看 [ast/visitor.go](https://github.com/bytedance/sonic/blob/main/ast/visitor.go),我们还为 `UserNode` 实现了一个示例 `ast.Visitor`,你可以在 [ast/visitor_test.go](https://github.com/bytedance/sonic/blob/main/ast/visitor_test.go) 中找到它。 + +## 兼容性 + +对于想要使用sonic来满足不同场景的开发人员,我们提供了一些集成配置: + +- `ConfigDefault`: sonic的默认配置 (`EscapeHTML=false`, `SortKeys=false`…) 保证性能同时兼顾安全性。 +- `ConfigStd`: 与 `encoding/json` 保证完全兼容的配置 +- `ConfigFastest`: 最快的配置(`NoQuoteTextMarshaler=true...`) 保证性能最优但是会缺少一些安全性检查(validate UTF8 等) +Sonic **不**确保支持所有环境,由于开发高性能代码的困难。在不支持sonic的环境中,实现将回落到 `encoding/json`。因此上述配置将全部等于`ConfigStd`。 + +## 注意事项 + +### 预热 + +由于 Sonic 使用 [golang-asm](https://github.com/twitchyliquid64/golang-asm) 作为 JIT 汇编器,这个库并不适用于运行时编译,第一次运行一个大型模式可能会导致请求超时甚至进程内存溢出。为了更好地稳定性,我们建议在运行大型模式或在内存有限的应用中,在使用 `Marshal()/Unmarshal()` 前运行 `Pretouch()`。 + +```go +import ( + "reflect" + "github.com/bytedance/sonic" + "github.com/bytedance/sonic/option" +) + +func init() { + var v HugeStruct + + // For most large types (nesting depth <= option.DefaultMaxInlineDepth) + err := sonic.Pretouch(reflect.TypeOf(v)) + + // with more CompileOption... + err := sonic.Pretouch(reflect.TypeOf(v), + // If the type is too deep nesting (nesting depth > option.DefaultMaxInlineDepth), + // you can set compile recursive loops in Pretouch for better stability in JIT. + option.WithCompileRecursiveDepth(loop), + // For a large nested struct, try to set a smaller depth to reduce compiling time. + option.WithCompileMaxInlineDepth(depth), + ) +} +``` + +### 拷贝字符串 + +当解码 **没有转义字符的字符串**时, sonic 会从原始的 JSON 缓冲区内引用而不是复制到新的一个缓冲区中。这对 CPU 的性能方面很有帮助,但是可能因此在解码后对象仍在使用的时候将整个 JSON 缓冲区保留在内存中。实践中我们发现,通过引用 JSON 缓冲区引入的额外内存通常是解码后对象的 20% 至 80% ,一旦应用长期保留这些对象(如缓存以备重用),服务器所使用的内存可能会增加。我们提供了选项 `decoder.CopyString()` 供用户选择,不引用 JSON 缓冲区。这可能在一定程度上降低 CPU 性能。 + +### 传递字符串还是字节数组? + +为了和 `encoding/json` 保持一致,我们提供了传递 `[]byte` 作为参数的 API ,但考虑到安全性,字符串到字节的复制是同时进行的,这在原始 JSON 非常大时可能会导致性能损失。因此,你可以使用 `UnmarshalString()` 和 `GetFromString()` 来传递字符串,只要你的原始数据是字符串,或**零拷贝类型转换**对于你的字节数组是安全的。我们也提供了 `MarshalString()` 的 API ,以便对编码的 JSON 字节数组进行**零拷贝类型转换**,因为 sonic 输出的字节始终是重复并且唯一的,所以这样是安全的。 + +### 加速 `encoding.TextMarshaler` + +为了保证数据安全性, `sonic.Encoder` 默认会对来自 `encoding.TextMarshaler` 接口的字符串进行引用和转义,如果大部分数据都是这种形式那可能会导致很大的性能损失。我们提供了 `encoder.NoQuoteTextMarshaler` 选项来跳过这些操作,但你**必须**保证他们的输出字符串依照 [RFC8259](https://datatracker.ietf.org/doc/html/rfc8259) 进行了转义和引用。 + +### 泛型的性能优化 + +在 **完全解析**的场景下, `Unmarshal()` 表现得比 `Get()`+`Node.Interface()` 更好。但是如果你只有特定 JSON 的部分模式,你可以将 `Get()` 和 `Unmarshal()` 结合使用: + +```go +import "github.com/bytedance/sonic" + +node, err := sonic.GetFromString(_TwitterJson, "statuses", 3, "user") +var user User // your partial schema... +err = sonic.UnmarshalString(node.Raw(), &user) +``` + +甚至如果你没有任何模式,可以用 `ast.Node` 代替 `map` 或 `interface` 作为泛型的容器: + +```go +import "github.com/bytedance/sonic" + +root, err := sonic.GetFromString(_TwitterJson) +user := root.GetByPath("statuses", 3, "user") // === root.Get("status").Index(3).Get("user") +err = user.Check() + +// err = user.LoadAll() // only call this when you want to use 'user' concurrently... +go someFunc(user) +``` + +为什么?因为 `ast.Node` 使用 `array` 来存储其子节点: + +- 在插入(反序列化)和扫描(序列化)数据时,`Array` 的性能比 `Map` **好得多**; +- **哈希**(`map[x]`)的效率不如**索引**(`array[x]`)高效,而 `ast.Node` 可以在数组和对象上使用索引; +- 使用 `Interface()` / `Map()` 意味着 sonic 必须解析所有的底层值,而 `ast.Node` 可以**按需解析**它们。 + +**注意**:由于 `ast.Node` 的惰性加载设计,其**不能**直接保证并发安全性,但你可以调用 `Node.Load()` / `Node.LoadAll()` 来实现并发安全。尽管可能会带来性能损失,但仍比转换成 `map` 或 `interface{}` 更为高效。 + +### 使用 `ast.Node` 还是 `ast.Visitor`? + +对于泛型数据的解析,`ast.Node` 在大多数场景上应该能够满足你的需求。 + +然而,`ast.Node` 是一种针对部分解析 JSON 而设计的泛型容器,它包含一些特殊设计,比如惰性加载,如果你希望像 `Unmarshal()` 那样直接解析整个 JSON,这些设计可能并不合适。尽管 `ast.Node` 相较于 `map` 或 `interface{}` 来说是更好的一种泛型容器,但它毕竟也是一种中间表示,如果你的最终类型是自定义的,你还得在解析完成后将上述类型转化成你自定义的类型。 + +在上述场景中,如果想要有更极致的性能,`ast.Visitor` 会是更好的选择。它采用和 `Unmarshal()` 类似的形式解析 JSON,并且你可以直接使用你的最终类型去表示 JSON AST,而不需要经过额外的任何中间表示。 + +但是,`ast.Visitor` 并不是一个很易用的 API。你可能需要写大量的代码去实现自己的 `ast.Visitor`,并且需要在解析过程中仔细维护树的层级。如果你决定要使用这个 API,请先仔细阅读 [ast/visitor.go](https://github.com/bytedance/sonic/blob/main/ast/visitor.go) 中的注释。 + +### 缓冲区大小 + +Sonic在许多地方使用内存池,如`encoder.Encode`, `ast.Node.MarshalJSON`等来提高性能,这可能会在服务器负载高时产生更多的内存使用(in-use)。参见[issue 614](https://github.com/bytedance/sonic/issues/614)。因此,我们引入了一些选项来让用户配置内存池的行为。参见[option](https://pkg.go.dev/github.com/bytedance/sonic@v1.11.9/option#pkg-variables)包。 + +### 更快的 JSON Skip + +为了安全起见,在跳过原始JSON 时,sonic decoder 默认使用[FSM](native/skip_one.c)算法扫描来跳过同时校验 JSON。它相比[SIMD-searching-pair](native/skip_one_fast.c)算法跳过要慢得多(1~10倍)。如果用户有很多冗余的JSON值,并且不需要严格验证JSON的正确性,你可以启用以下选项: + +- `Config.NoValidateSkipJSON`: 用于在解码时更快地跳过JSON,例如未知字段,`json.RawMessage`,不匹配的值和冗余的数组元素等 +- `Config.NoValidateJSONMarshaler`: 编码JSON时避免验证JSON。封送拆收器 +- `SearchOption.ValidateJSON`: 指示当`Get`时是否验证定位的JSON值 + +## 社区 + +Sonic 是 [CloudWeGo](https://www.cloudwego.io/) 下的一个子项目。我们致力于构建云原生生态系统。 diff --git a/vendor/github.com/bytedance/sonic/api.go b/vendor/github.com/bytedance/sonic/api.go new file mode 100644 index 00000000000..406715ecad9 --- /dev/null +++ b/vendor/github.com/bytedance/sonic/api.go @@ -0,0 +1,247 @@ +/* + * Copyright 2021 ByteDance Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package sonic + +import ( + `io` + + `github.com/bytedance/sonic/ast` + `github.com/bytedance/sonic/internal/rt` +) + +const ( + // UseStdJSON indicates you are using fallback implementation (encoding/json) + UseStdJSON = iota + // UseSonicJSON indicates you are using real sonic implementation + UseSonicJSON +) + +// APIKind is the kind of API, 0 is std json, 1 is sonic. +const APIKind = apiKind + +// Config is a combination of sonic/encoder.Options and sonic/decoder.Options +type Config struct { + // EscapeHTML indicates encoder to escape all HTML characters + // after serializing into JSON (see https://pkg.go.dev/encoding/json#HTMLEscape). + // WARNING: This hurts performance A LOT, USE WITH CARE. + EscapeHTML bool + + // SortMapKeys indicates encoder that the keys of a map needs to be sorted + // before serializing into JSON. + // WARNING: This hurts performance A LOT, USE WITH CARE. + SortMapKeys bool + + // CompactMarshaler indicates encoder that the output JSON from json.Marshaler + // is always compact and needs no validation + CompactMarshaler bool + + // NoQuoteTextMarshaler indicates encoder that the output text from encoding.TextMarshaler + // is always escaped string and needs no quoting + NoQuoteTextMarshaler bool + + // NoNullSliceOrMap indicates encoder that all empty Array or Object are encoded as '[]' or '{}', + // instead of 'null' + NoNullSliceOrMap bool + + // UseInt64 indicates decoder to unmarshal an integer into an interface{} as an + // int64 instead of as a float64. + UseInt64 bool + + // UseNumber indicates decoder to unmarshal a number into an interface{} as a + // json.Number instead of as a float64. + UseNumber bool + + // UseUnicodeErrors indicates decoder to return an error when encounter invalid + // UTF-8 escape sequences. + UseUnicodeErrors bool + + // DisallowUnknownFields indicates decoder to return an error when the destination + // is a struct and the input contains object keys which do not match any + // non-ignored, exported fields in the destination. + DisallowUnknownFields bool + + // CopyString indicates decoder to decode string values by copying instead of referring. + CopyString bool + + // ValidateString indicates decoder and encoder to validate string values: decoder will return errors + // when unescaped control chars(\u0000-\u001f) in the string value of JSON. + ValidateString bool + + // NoValidateJSONMarshaler indicates that the encoder should not validate the output string + // after encoding the JSONMarshaler to JSON. + NoValidateJSONMarshaler bool + + // NoValidateJSONSkip indicates the decoder should not validate the JSON value when skipping it, + // such as unknown-fields, mismatched-type, redundant elements.. + NoValidateJSONSkip bool + + // NoEncoderNewline indicates that the encoder should not add a newline after every message + NoEncoderNewline bool + + // Encode Infinity or Nan float into `null`, instead of returning an error. + EncodeNullForInfOrNan bool +} + +var ( + // ConfigDefault is the default config of APIs, aiming at efficiency and safety. + ConfigDefault = Config{}.Froze() + + // ConfigStd is the standard config of APIs, aiming at being compatible with encoding/json. + ConfigStd = Config{ + EscapeHTML : true, + SortMapKeys: true, + CompactMarshaler: true, + CopyString : true, + ValidateString : true, + }.Froze() + + // ConfigFastest is the fastest config of APIs, aiming at speed. + ConfigFastest = Config{ + NoQuoteTextMarshaler: true, + NoValidateJSONMarshaler: true, + NoValidateJSONSkip: true, + }.Froze() +) + + +// API is a binding of specific config. +// This interface is inspired by github.com/json-iterator/go, +// and has same behaviors under equivalent config. +type API interface { + // MarshalToString returns the JSON encoding string of v + MarshalToString(v interface{}) (string, error) + // Marshal returns the JSON encoding bytes of v. + Marshal(v interface{}) ([]byte, error) + // MarshalIndent returns the JSON encoding bytes with indent and prefix. + MarshalIndent(v interface{}, prefix, indent string) ([]byte, error) + // UnmarshalFromString parses the JSON-encoded bytes and stores the result in the value pointed to by v. + UnmarshalFromString(str string, v interface{}) error + // Unmarshal parses the JSON-encoded string and stores the result in the value pointed to by v. + Unmarshal(data []byte, v interface{}) error + // NewEncoder create a Encoder holding writer + NewEncoder(writer io.Writer) Encoder + // NewDecoder create a Decoder holding reader + NewDecoder(reader io.Reader) Decoder + // Valid validates the JSON-encoded bytes and reports if it is valid + Valid(data []byte) bool +} + +// Encoder encodes JSON into io.Writer +type Encoder interface { + // Encode writes the JSON encoding of v to the stream, followed by a newline character. + Encode(val interface{}) error + // SetEscapeHTML specifies whether problematic HTML characters + // should be escaped inside JSON quoted strings. + // The default behavior NOT ESCAPE + SetEscapeHTML(on bool) + // SetIndent instructs the encoder to format each subsequent encoded value + // as if indented by the package-level function Indent(dst, src, prefix, indent). + // Calling SetIndent("", "") disables indentation + SetIndent(prefix, indent string) +} + +// Decoder decodes JSON from io.Read +type Decoder interface { + // Decode reads the next JSON-encoded value from its input and stores it in the value pointed to by v. + Decode(val interface{}) error + // Buffered returns a reader of the data remaining in the Decoder's buffer. + // The reader is valid until the next call to Decode. + Buffered() io.Reader + // DisallowUnknownFields causes the Decoder to return an error when the destination is a struct + // and the input contains object keys which do not match any non-ignored, exported fields in the destination. + DisallowUnknownFields() + // More reports whether there is another element in the current array or object being parsed. + More() bool + // UseNumber causes the Decoder to unmarshal a number into an interface{} as a Number instead of as a float64. + UseNumber() +} + +// Marshal returns the JSON encoding bytes of v. +func Marshal(val interface{}) ([]byte, error) { + return ConfigDefault.Marshal(val) +} + +// MarshalIndent is like Marshal but applies Indent to format the output. +// Each JSON element in the output will begin on a new line beginning with prefix +// followed by one or more copies of indent according to the indentation nesting. +func MarshalIndent(v interface{}, prefix, indent string) ([]byte, error) { + return ConfigDefault.MarshalIndent(v, prefix, indent) +} + +// MarshalString returns the JSON encoding string of v. +func MarshalString(val interface{}) (string, error) { + return ConfigDefault.MarshalToString(val) +} + +// Unmarshal parses the JSON-encoded data and stores the result in the value pointed to by v. +// NOTICE: This API copies given buffer by default, +// if you want to pass JSON more efficiently, use UnmarshalString instead. +func Unmarshal(buf []byte, val interface{}) error { + return ConfigDefault.Unmarshal(buf, val) +} + +// UnmarshalString is like Unmarshal, except buf is a string. +func UnmarshalString(buf string, val interface{}) error { + return ConfigDefault.UnmarshalFromString(buf, val) +} + +// Get searches and locates the given path from src json, +// and returns a ast.Node representing the partially json. +// +// Each path arg must be integer or string: +// - Integer is target index(>=0), means searching current node as array. +// - String is target key, means searching current node as object. +// +// +// Notice: It expects the src json is **Well-formed** and **Immutable** when calling, +// otherwise it may return unexpected result. +// Considering memory safety, the returned JSON is **Copied** from the input +func Get(src []byte, path ...interface{}) (ast.Node, error) { + return GetCopyFromString(rt.Mem2Str(src), path...) +} + +//GetWithOptions searches and locates the given path from src json, +// with specific options of ast.Searcher +func GetWithOptions(src []byte, opts ast.SearchOptions, path ...interface{}) (ast.Node, error) { + s := ast.NewSearcher(rt.Mem2Str(src)) + s.SearchOptions = opts + return s.GetByPath(path...) +} + +// GetFromString is same with Get except src is string. +// +// WARNING: The returned JSON is **Referenced** from the input. +// Caching or long-time holding the returned node may cause OOM. +// If your src is big, consider use GetFromStringCopy(). +func GetFromString(src string, path ...interface{}) (ast.Node, error) { + return ast.NewSearcher(src).GetByPath(path...) +} + +// GetCopyFromString is same with Get except src is string +func GetCopyFromString(src string, path ...interface{}) (ast.Node, error) { + return ast.NewSearcher(src).GetByPathCopy(path...) +} + +// Valid reports whether data is a valid JSON encoding. +func Valid(data []byte) bool { + return ConfigDefault.Valid(data) +} + +// Valid reports whether data is a valid JSON encoding. +func ValidString(data string) bool { + return ConfigDefault.Valid(rt.Str2Mem(data)) +} diff --git a/vendor/github.com/bytedance/sonic/ast/api.go b/vendor/github.com/bytedance/sonic/ast/api.go new file mode 100644 index 00000000000..7c8253aa118 --- /dev/null +++ b/vendor/github.com/bytedance/sonic/ast/api.go @@ -0,0 +1,135 @@ +//go:build (amd64 && go1.17 && !go1.24) || (arm64 && go1.20 && !go1.24) +// +build amd64,go1.17,!go1.24 arm64,go1.20,!go1.24 + +/* + * Copyright 2022 ByteDance Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ast + +import ( + `runtime` + `unsafe` + + `github.com/bytedance/sonic/encoder` + `github.com/bytedance/sonic/internal/native` + `github.com/bytedance/sonic/internal/native/types` + `github.com/bytedance/sonic/internal/rt` + uq `github.com/bytedance/sonic/unquote` + `github.com/bytedance/sonic/utf8` +) + +var typeByte = rt.UnpackEface(byte(0)).Type + +//go:nocheckptr +func quote(buf *[]byte, val string) { + *buf = append(*buf, '"') + if len(val) == 0 { + *buf = append(*buf, '"') + return + } + + sp := rt.IndexChar(val, 0) + nb := len(val) + b := (*rt.GoSlice)(unsafe.Pointer(buf)) + + // input buffer + for nb > 0 { + // output buffer + dp := unsafe.Pointer(uintptr(b.Ptr) + uintptr(b.Len)) + dn := b.Cap - b.Len + // call native.Quote, dn is byte count it outputs + ret := native.Quote(sp, nb, dp, &dn, 0) + // update *buf length + b.Len += dn + + // no need more output + if ret >= 0 { + break + } + + // double buf size + *b = rt.GrowSlice(typeByte, *b, b.Cap*2) + // ret is the complement of consumed input + ret = ^ret + // update input buffer + nb -= ret + sp = unsafe.Pointer(uintptr(sp) + uintptr(ret)) + } + + runtime.KeepAlive(buf) + runtime.KeepAlive(sp) + *buf = append(*buf, '"') +} + +func unquote(src string) (string, types.ParsingError) { + return uq.String(src) +} + +func (self *Parser) decodeValue() (val types.JsonState) { + sv := (*rt.GoString)(unsafe.Pointer(&self.s)) + flag := types.F_USE_NUMBER + if self.dbuf != nil { + flag = 0 + val.Dbuf = self.dbuf + val.Dcap = types.MaxDigitNums + } + self.p = native.Value(sv.Ptr, sv.Len, self.p, &val, uint64(flag)) + return +} + +func (self *Parser) skip() (int, types.ParsingError) { + fsm := types.NewStateMachine() + start := native.SkipOne(&self.s, &self.p, fsm, 0) + types.FreeStateMachine(fsm) + + if start < 0 { + return self.p, types.ParsingError(-start) + } + return start, 0 +} + +func (self *Node) encodeInterface(buf *[]byte) error { + //WARN: NOT compatible with json.Encoder + return encoder.EncodeInto(buf, self.packAny(), encoder.NoEncoderNewline) +} + +func (self *Parser) skipFast() (int, types.ParsingError) { + start := native.SkipOneFast(&self.s, &self.p) + if start < 0 { + return self.p, types.ParsingError(-start) + } + return start, 0 +} + +func (self *Parser) getByPath(validate bool, path ...interface{}) (int, types.ParsingError) { + var fsm *types.StateMachine + if validate { + fsm = types.NewStateMachine() + } + start := native.GetByPath(&self.s, &self.p, &path, fsm) + if validate { + types.FreeStateMachine(fsm) + } + runtime.KeepAlive(path) + if start < 0 { + return self.p, types.ParsingError(-start) + } + return start, 0 +} + +func validate_utf8(str string) bool { + return utf8.ValidateString(str) +} diff --git a/vendor/github.com/bytedance/sonic/ast/api_compat.go b/vendor/github.com/bytedance/sonic/ast/api_compat.go new file mode 100644 index 00000000000..6541e219d6f --- /dev/null +++ b/vendor/github.com/bytedance/sonic/ast/api_compat.go @@ -0,0 +1,114 @@ +// +build !amd64,!arm64 go1.24 !go1.17 arm64,!go1.20 + +/* +* Copyright 2022 ByteDance Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +package ast + +import ( + `encoding/json` + `unicode/utf8` + + `github.com/bytedance/sonic/internal/native/types` + `github.com/bytedance/sonic/internal/rt` +) + +func init() { + println("WARNING:(ast) sonic only supports go1.17~1.23, but your environment is not suitable") +} + +func quote(buf *[]byte, val string) { + quoteString(buf, val) +} + +// unquote unescapes an internal JSON string (it doesn't count quotas at the beginning and end) +func unquote(src string) (string, types.ParsingError) { + sp := rt.IndexChar(src, -1) + out, ok := unquoteBytes(rt.BytesFrom(sp, len(src)+2, len(src)+2)) + if !ok { + return "", types.ERR_INVALID_ESCAPE + } + return rt.Mem2Str(out), 0 +} + + +func (self *Parser) decodeValue() (val types.JsonState) { + e, v := decodeValue(self.s, self.p, self.dbuf == nil) + if e < 0 { + return v + } + self.p = e + return v +} + +func (self *Parser) skip() (int, types.ParsingError) { + e, s := skipValue(self.s, self.p) + if e < 0 { + return self.p, types.ParsingError(-e) + } + self.p = e + return s, 0 +} + +func (self *Parser) skipFast() (int, types.ParsingError) { + e, s := skipValueFast(self.s, self.p) + if e < 0 { + return self.p, types.ParsingError(-e) + } + self.p = e + return s, 0 +} + +func (self *Node) encodeInterface(buf *[]byte) error { + out, err := json.Marshal(self.packAny()) + if err != nil { + return err + } + *buf = append(*buf, out...) + return nil +} + +func (self *Parser) getByPath(validate bool, path ...interface{}) (int, types.ParsingError) { + for _, p := range path { + if idx, ok := p.(int); ok && idx >= 0 { + if err := self.searchIndex(idx); err != 0 { + return self.p, err + } + } else if key, ok := p.(string); ok { + if err := self.searchKey(key); err != 0 { + return self.p, err + } + } else { + panic("path must be either int(>=0) or string") + } + } + + var start int + var e types.ParsingError + if validate { + start, e = self.skip() + } else { + start, e = self.skipFast() + } + if e != 0 { + return self.p, e + } + return start, 0 +} + +func validate_utf8(str string) bool { + return utf8.ValidString(str) +} diff --git a/vendor/github.com/bytedance/sonic/ast/asm.s b/vendor/github.com/bytedance/sonic/ast/asm.s new file mode 100644 index 00000000000..e69de29bb2d diff --git a/vendor/github.com/bytedance/sonic/ast/buffer.go b/vendor/github.com/bytedance/sonic/ast/buffer.go new file mode 100644 index 00000000000..04701ef5b3a --- /dev/null +++ b/vendor/github.com/bytedance/sonic/ast/buffer.go @@ -0,0 +1,470 @@ +/** + * Copyright 2023 ByteDance Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ast + +import ( + "sort" + "unsafe" + + "github.com/bytedance/sonic/internal/caching" +) + +type nodeChunk [_DEFAULT_NODE_CAP]Node + +type linkedNodes struct { + head nodeChunk + tail []*nodeChunk + size int +} + +func (self *linkedNodes) Cap() int { + if self == nil { + return 0 + } + return (len(self.tail)+1)*_DEFAULT_NODE_CAP +} + +func (self *linkedNodes) Len() int { + if self == nil { + return 0 + } + return self.size +} + +func (self *linkedNodes) At(i int) (*Node) { + if self == nil { + return nil + } + if i >= 0 && i= _DEFAULT_NODE_CAP && i= self.size || target < 0 || target >= self.size { + return + } + // reserve source + n := *self.At(source) + if source < target { + // move every element (source,target] one step back + for i:=source; itarget; i-- { + *self.At(i) = *self.At(i-1) + } + } + // set target + *self.At(target) = n +} + +func (self *linkedNodes) Pop() { + if self == nil || self.size == 0 { + return + } + self.Set(self.size-1, Node{}) + self.size-- +} + +func (self *linkedNodes) Push(v Node) { + self.Set(self.size, v) +} + + +func (self *linkedNodes) Set(i int, v Node) { + if i < _DEFAULT_NODE_CAP { + self.head[i] = v + if self.size <= i { + self.size = i+1 + } + return + } + a, b := i/_DEFAULT_NODE_CAP-1, i%_DEFAULT_NODE_CAP + if a < 0 { + self.head[b] = v + } else { + self.growTailLength(a+1) + var n = &self.tail[a] + if *n == nil { + *n = new(nodeChunk) + } + (*n)[b] = v + } + if self.size <= i { + self.size = i+1 + } +} + +func (self *linkedNodes) growTailLength(l int) { + if l <= len(self.tail) { + return + } + c := cap(self.tail) + for c < l { + c += 1 + c>>_APPEND_GROW_SHIFT + } + if c == cap(self.tail) { + self.tail = self.tail[:l] + return + } + tmp := make([]*nodeChunk, l, c) + copy(tmp, self.tail) + self.tail = tmp +} + +func (self *linkedNodes) ToSlice(con []Node) { + if len(con) < self.size { + return + } + i := (self.size-1) + a, b := i/_DEFAULT_NODE_CAP-1, i%_DEFAULT_NODE_CAP + if a < 0 { + copy(con, self.head[:b+1]) + return + } else { + copy(con, self.head[:]) + con = con[_DEFAULT_NODE_CAP:] + } + + for i:=0; i>_APPEND_GROW_SHIFT + self.tail = make([]*nodeChunk, a+1, c) + } + self.tail = self.tail[:a+1] + + for i:=0; i= 0 && i < _DEFAULT_NODE_CAP && i= _DEFAULT_NODE_CAP && i>_APPEND_GROW_SHIFT + } + if c == cap(self.tail) { + self.tail = self.tail[:l] + return + } + tmp := make([]*pairChunk, l, c) + copy(tmp, self.tail) + self.tail = tmp +} + +// linear search +func (self *linkedPairs) Get(key string) (*Pair, int) { + if self.index != nil { + // fast-path + i, ok := self.index[caching.StrHash(key)] + if ok { + n := self.At(i) + if n.Key == key { + return n, i + } + // hash conflicts + goto linear_search + } else { + return nil, -1 + } + } +linear_search: + for i:=0; i>_APPEND_GROW_SHIFT + self.tail = make([]*pairChunk, a+1, c) + } + self.tail = self.tail[:a+1] + + for i:=0; i len(b) { + l = len(b) + } + for i := d; i < l; i++ { + if a[i] == b[i] { + continue + } + return a[i] < b[i] + } + return len(a) < len(b) +} + +type parseObjectStack struct { + parser Parser + v linkedPairs +} + +type parseArrayStack struct { + parser Parser + v linkedNodes +} + +func newLazyArray(p *Parser) Node { + s := new(parseArrayStack) + s.parser = *p + return Node{ + t: _V_ARRAY_LAZY, + p: unsafe.Pointer(s), + } +} + +func newLazyObject(p *Parser) Node { + s := new(parseObjectStack) + s.parser = *p + return Node{ + t: _V_OBJECT_LAZY, + p: unsafe.Pointer(s), + } +} + +func (self *Node) getParserAndArrayStack() (*Parser, *parseArrayStack) { + stack := (*parseArrayStack)(self.p) + return &stack.parser, stack +} + +func (self *Node) getParserAndObjectStack() (*Parser, *parseObjectStack) { + stack := (*parseObjectStack)(self.p) + return &stack.parser, stack +} + diff --git a/vendor/github.com/bytedance/sonic/ast/decode.go b/vendor/github.com/bytedance/sonic/ast/decode.go new file mode 100644 index 00000000000..135ee6eb8ce --- /dev/null +++ b/vendor/github.com/bytedance/sonic/ast/decode.go @@ -0,0 +1,562 @@ +/* + * Copyright 2022 ByteDance Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ast + +import ( + "encoding/base64" + "runtime" + "strconv" + "unsafe" + + "github.com/bytedance/sonic/internal/native/types" + "github.com/bytedance/sonic/internal/rt" + "github.com/bytedance/sonic/internal/utils" +) + +// Hack: this is used for both checking space and cause friendly compile errors in 32-bit arch. +const _Sonic_Not_Support_32Bit_Arch__Checking_32Bit_Arch_Here = (1 << ' ') | (1 << '\t') | (1 << '\r') | (1 << '\n') + +var bytesNull = []byte("null") + +const ( + strNull = "null" + bytesTrue = "true" + bytesFalse = "false" + bytesObject = "{}" + bytesArray = "[]" +) + +func isSpace(c byte) bool { + return (int(1<= se { + return -int(types.ERR_EOF) + } + runtime.KeepAlive(src) + return int(sp - uintptr(rt.IndexChar(src, 0))) +} + +func decodeNull(src string, pos int) (ret int) { + ret = pos + 4 + if ret > len(src) { + return -int(types.ERR_EOF) + } + if src[pos:ret] == strNull { + return ret + } else { + return -int(types.ERR_INVALID_CHAR) + } +} + +func decodeTrue(src string, pos int) (ret int) { + ret = pos + 4 + if ret > len(src) { + return -int(types.ERR_EOF) + } + if src[pos:ret] == bytesTrue { + return ret + } else { + return -int(types.ERR_INVALID_CHAR) + } + +} + +func decodeFalse(src string, pos int) (ret int) { + ret = pos + 5 + if ret > len(src) { + return -int(types.ERR_EOF) + } + if src[pos:ret] == bytesFalse { + return ret + } + return -int(types.ERR_INVALID_CHAR) +} + +//go:nocheckptr +func decodeString(src string, pos int) (ret int, v string) { + ret, ep := skipString(src, pos) + if ep == -1 { + (*rt.GoString)(unsafe.Pointer(&v)).Ptr = rt.IndexChar(src, pos+1) + (*rt.GoString)(unsafe.Pointer(&v)).Len = ret - pos - 2 + return ret, v + } + + vv, ok := unquoteBytes(rt.Str2Mem(src[pos:ret])) + if !ok { + return -int(types.ERR_INVALID_CHAR), "" + } + + runtime.KeepAlive(src) + return ret, rt.Mem2Str(vv) +} + +func decodeBinary(src string, pos int) (ret int, v []byte) { + var vv string + ret, vv = decodeString(src, pos) + if ret < 0 { + return ret, nil + } + var err error + v, err = base64.StdEncoding.DecodeString(vv) + if err != nil { + return -int(types.ERR_INVALID_CHAR), nil + } + return ret, v +} + +func isDigit(c byte) bool { + return c >= '0' && c <= '9' +} + +//go:nocheckptr +func decodeInt64(src string, pos int) (ret int, v int64, err error) { + sp := uintptr(rt.IndexChar(src, pos)) + ss := uintptr(sp) + se := uintptr(rt.IndexChar(src, len(src))) + if uintptr(sp) >= se { + return -int(types.ERR_EOF), 0, nil + } + + if c := *(*byte)(unsafe.Pointer(sp)); c == '-' { + sp += 1 + } + if sp == se { + return -int(types.ERR_EOF), 0, nil + } + + for ; sp < se; sp += uintptr(1) { + if !isDigit(*(*byte)(unsafe.Pointer(sp))) { + break + } + } + + if sp < se { + if c := *(*byte)(unsafe.Pointer(sp)); c == '.' || c == 'e' || c == 'E' { + return -int(types.ERR_INVALID_NUMBER_FMT), 0, nil + } + } + + var vv string + ret = int(uintptr(sp) - uintptr((*rt.GoString)(unsafe.Pointer(&src)).Ptr)) + (*rt.GoString)(unsafe.Pointer(&vv)).Ptr = unsafe.Pointer(ss) + (*rt.GoString)(unsafe.Pointer(&vv)).Len = ret - pos + + v, err = strconv.ParseInt(vv, 10, 64) + if err != nil { + //NOTICE: allow overflow here + if err.(*strconv.NumError).Err == strconv.ErrRange { + return ret, 0, err + } + return -int(types.ERR_INVALID_CHAR), 0, err + } + + runtime.KeepAlive(src) + return ret, v, nil +} + +func isNumberChars(c byte) bool { + return (c >= '0' && c <= '9') || c == '+' || c == '-' || c == 'e' || c == 'E' || c == '.' +} + +//go:nocheckptr +func decodeFloat64(src string, pos int) (ret int, v float64, err error) { + sp := uintptr(rt.IndexChar(src, pos)) + ss := uintptr(sp) + se := uintptr(rt.IndexChar(src, len(src))) + if uintptr(sp) >= se { + return -int(types.ERR_EOF), 0, nil + } + + if c := *(*byte)(unsafe.Pointer(sp)); c == '-' { + sp += 1 + } + if sp == se { + return -int(types.ERR_EOF), 0, nil + } + + for ; sp < se; sp += uintptr(1) { + if !isNumberChars(*(*byte)(unsafe.Pointer(sp))) { + break + } + } + + var vv string + ret = int(uintptr(sp) - uintptr((*rt.GoString)(unsafe.Pointer(&src)).Ptr)) + (*rt.GoString)(unsafe.Pointer(&vv)).Ptr = unsafe.Pointer(ss) + (*rt.GoString)(unsafe.Pointer(&vv)).Len = ret - pos + + v, err = strconv.ParseFloat(vv, 64) + if err != nil { + //NOTICE: allow overflow here + if err.(*strconv.NumError).Err == strconv.ErrRange { + return ret, 0, err + } + return -int(types.ERR_INVALID_CHAR), 0, err + } + + runtime.KeepAlive(src) + return ret, v, nil +} + +func decodeValue(src string, pos int, skipnum bool) (ret int, v types.JsonState) { + pos = skipBlank(src, pos) + if pos < 0 { + return pos, types.JsonState{Vt: types.ValueType(pos)} + } + switch c := src[pos]; c { + case 'n': + ret = decodeNull(src, pos) + if ret < 0 { + return ret, types.JsonState{Vt: types.ValueType(ret)} + } + return ret, types.JsonState{Vt: types.V_NULL} + case '"': + var ep int + ret, ep = skipString(src, pos) + if ret < 0 { + return ret, types.JsonState{Vt: types.ValueType(ret)} + } + return ret, types.JsonState{Vt: types.V_STRING, Iv: int64(pos + 1), Ep: ep} + case '{': + return pos + 1, types.JsonState{Vt: types.V_OBJECT} + case '[': + return pos + 1, types.JsonState{Vt: types.V_ARRAY} + case 't': + ret = decodeTrue(src, pos) + if ret < 0 { + return ret, types.JsonState{Vt: types.ValueType(ret)} + } + return ret, types.JsonState{Vt: types.V_TRUE} + case 'f': + ret = decodeFalse(src, pos) + if ret < 0 { + return ret, types.JsonState{Vt: types.ValueType(ret)} + } + return ret, types.JsonState{Vt: types.V_FALSE} + case '-', '+', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9': + if skipnum { + ret = skipNumber(src, pos) + if ret >= 0 { + return ret, types.JsonState{Vt: types.V_DOUBLE, Iv: 0, Ep: pos} + } else { + return ret, types.JsonState{Vt: types.ValueType(ret)} + } + } else { + var iv int64 + ret, iv, _ = decodeInt64(src, pos) + if ret >= 0 { + return ret, types.JsonState{Vt: types.V_INTEGER, Iv: iv, Ep: pos} + } else if ret != -int(types.ERR_INVALID_NUMBER_FMT) { + return ret, types.JsonState{Vt: types.ValueType(ret)} + } + var fv float64 + ret, fv, _ = decodeFloat64(src, pos) + if ret >= 0 { + return ret, types.JsonState{Vt: types.V_DOUBLE, Dv: fv, Ep: pos} + } else { + return ret, types.JsonState{Vt: types.ValueType(ret)} + } + } + + default: + return -int(types.ERR_INVALID_CHAR), types.JsonState{Vt:-types.ValueType(types.ERR_INVALID_CHAR)} + } +} + +//go:nocheckptr +func skipNumber(src string, pos int) (ret int) { + return utils.SkipNumber(src, pos) +} + +//go:nocheckptr +func skipString(src string, pos int) (ret int, ep int) { + if pos+1 >= len(src) { + return -int(types.ERR_EOF), -1 + } + + sp := uintptr(rt.IndexChar(src, pos)) + se := uintptr(rt.IndexChar(src, len(src))) + + // not start with quote + if *(*byte)(unsafe.Pointer(sp)) != '"' { + return -int(types.ERR_INVALID_CHAR), -1 + } + sp += 1 + + ep = -1 + for sp < se { + c := *(*byte)(unsafe.Pointer(sp)) + if c == '\\' { + if ep == -1 { + ep = int(uintptr(sp) - uintptr((*rt.GoString)(unsafe.Pointer(&src)).Ptr)) + } + sp += 2 + continue + } + sp += 1 + if c == '"' { + return int(uintptr(sp) - uintptr((*rt.GoString)(unsafe.Pointer(&src)).Ptr)), ep + } + } + + runtime.KeepAlive(src) + // not found the closed quote until EOF + return -int(types.ERR_EOF), -1 +} + +//go:nocheckptr +func skipPair(src string, pos int, lchar byte, rchar byte) (ret int) { + if pos+1 >= len(src) { + return -int(types.ERR_EOF) + } + + sp := uintptr(rt.IndexChar(src, pos)) + se := uintptr(rt.IndexChar(src, len(src))) + + if *(*byte)(unsafe.Pointer(sp)) != lchar { + return -int(types.ERR_INVALID_CHAR) + } + + sp += 1 + nbrace := 1 + inquote := false + + for sp < se { + c := *(*byte)(unsafe.Pointer(sp)) + if c == '\\' { + sp += 2 + continue + } else if c == '"' { + inquote = !inquote + } else if c == lchar { + if !inquote { + nbrace += 1 + } + } else if c == rchar { + if !inquote { + nbrace -= 1 + if nbrace == 0 { + sp += 1 + break + } + } + } + sp += 1 + } + + if nbrace != 0 { + return -int(types.ERR_INVALID_CHAR) + } + + runtime.KeepAlive(src) + return int(uintptr(sp) - uintptr((*rt.GoString)(unsafe.Pointer(&src)).Ptr)) +} + +func skipValueFast(src string, pos int) (ret int, start int) { + pos = skipBlank(src, pos) + if pos < 0 { + return pos, -1 + } + switch c := src[pos]; c { + case 'n': + ret = decodeNull(src, pos) + case '"': + ret, _ = skipString(src, pos) + case '{': + ret = skipPair(src, pos, '{', '}') + case '[': + ret = skipPair(src, pos, '[', ']') + case 't': + ret = decodeTrue(src, pos) + case 'f': + ret = decodeFalse(src, pos) + case '-', '+', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9': + ret = skipNumber(src, pos) + default: + ret = -int(types.ERR_INVALID_CHAR) + } + return ret, pos +} + +func skipValue(src string, pos int) (ret int, start int) { + pos = skipBlank(src, pos) + if pos < 0 { + return pos, -1 + } + switch c := src[pos]; c { + case 'n': + ret = decodeNull(src, pos) + case '"': + ret, _ = skipString(src, pos) + case '{': + ret, _ = skipObject(src, pos) + case '[': + ret, _ = skipArray(src, pos) + case 't': + ret = decodeTrue(src, pos) + case 'f': + ret = decodeFalse(src, pos) + case '-', '+', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9': + ret = skipNumber(src, pos) + default: + ret = -int(types.ERR_INVALID_CHAR) + } + return ret, pos +} + +func skipObject(src string, pos int) (ret int, start int) { + start = skipBlank(src, pos) + if start < 0 { + return start, -1 + } + + if src[start] != '{' { + return -int(types.ERR_INVALID_CHAR), -1 + } + + pos = start + 1 + pos = skipBlank(src, pos) + if pos < 0 { + return pos, -1 + } + if src[pos] == '}' { + return pos + 1, start + } + + for { + pos, _ = skipString(src, pos) + if pos < 0 { + return pos, -1 + } + + pos = skipBlank(src, pos) + if pos < 0 { + return pos, -1 + } + if src[pos] != ':' { + return -int(types.ERR_INVALID_CHAR), -1 + } + + pos++ + pos, _ = skipValue(src, pos) + if pos < 0 { + return pos, -1 + } + + pos = skipBlank(src, pos) + if pos < 0 { + return pos, -1 + } + if src[pos] == '}' { + return pos + 1, start + } + if src[pos] != ',' { + return -int(types.ERR_INVALID_CHAR), -1 + } + + pos++ + pos = skipBlank(src, pos) + if pos < 0 { + return pos, -1 + } + + } +} + +func skipArray(src string, pos int) (ret int, start int) { + start = skipBlank(src, pos) + if start < 0 { + return start, -1 + } + + if src[start] != '[' { + return -int(types.ERR_INVALID_CHAR), -1 + } + + pos = start + 1 + pos = skipBlank(src, pos) + if pos < 0 { + return pos, -1 + } + if src[pos] == ']' { + return pos + 1, start + } + + for { + pos, _ = skipValue(src, pos) + if pos < 0 { + return pos, -1 + } + + pos = skipBlank(src, pos) + if pos < 0 { + return pos, -1 + } + if src[pos] == ']' { + return pos + 1, start + } + if src[pos] != ',' { + return -int(types.ERR_INVALID_CHAR), -1 + } + pos++ + } +} + +// DecodeString decodes a JSON string from pos and return golang string. +// - needEsc indicates if to unescaped escaping chars +// - hasEsc tells if the returned string has escaping chars +// - validStr enables validating UTF8 charset +// +func _DecodeString(src string, pos int, needEsc bool, validStr bool) (v string, ret int, hasEsc bool) { + p := NewParserObj(src) + p.p = pos + switch val := p.decodeValue(); val.Vt { + case types.V_STRING: + str := p.s[val.Iv : p.p-1] + if validStr && !validate_utf8(str) { + return "", -int(types.ERR_INVALID_UTF8), false + } + /* fast path: no escape sequence */ + if val.Ep == -1 { + return str, p.p, false + } else if !needEsc { + return str, p.p, true + } + /* unquote the string */ + out, err := unquote(str) + /* check for errors */ + if err != 0 { + return "", -int(err), true + } else { + return out, p.p, true + } + default: + return "", -int(_ERR_UNSUPPORT_TYPE), false + } +} diff --git a/vendor/github.com/bytedance/sonic/ast/encode.go b/vendor/github.com/bytedance/sonic/ast/encode.go new file mode 100644 index 00000000000..eae0bd258ba --- /dev/null +++ b/vendor/github.com/bytedance/sonic/ast/encode.go @@ -0,0 +1,274 @@ +/* + * Copyright 2021 ByteDance Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ast + +import ( + "sync" + "unicode/utf8" + + "github.com/bytedance/sonic/internal/rt" + "github.com/bytedance/sonic/option" +) + +func quoteString(e *[]byte, s string) { + *e = append(*e, '"') + start := 0 + for i := 0; i < len(s); { + if b := s[i]; b < utf8.RuneSelf { + if rt.SafeSet[b] { + i++ + continue + } + if start < i { + *e = append(*e, s[start:i]...) + } + *e = append(*e, '\\') + switch b { + case '\\', '"': + *e = append(*e, b) + case '\n': + *e = append(*e, 'n') + case '\r': + *e = append(*e, 'r') + case '\t': + *e = append(*e, 't') + default: + // This encodes bytes < 0x20 except for \t, \n and \r. + // If escapeHTML is set, it also escapes <, >, and & + // because they can lead to security holes when + // user-controlled strings are rendered into JSON + // and served to some browsers. + *e = append(*e, `u00`...) + *e = append(*e, rt.Hex[b>>4]) + *e = append(*e, rt.Hex[b&0xF]) + } + i++ + start = i + continue + } + c, size := utf8.DecodeRuneInString(s[i:]) + // if c == utf8.RuneError && size == 1 { + // if start < i { + // e.Write(s[start:i]) + // } + // e.WriteString(`\ufffd`) + // i += size + // start = i + // continue + // } + if c == '\u2028' || c == '\u2029' { + if start < i { + *e = append(*e, s[start:i]...) + } + *e = append(*e, `\u202`...) + *e = append(*e, rt.Hex[c&0xF]) + i += size + start = i + continue + } + i += size + } + if start < len(s) { + *e = append(*e, s[start:]...) + } + *e = append(*e, '"') +} + +var bytesPool = sync.Pool{} + +func (self *Node) MarshalJSON() ([]byte, error) { + if self == nil { + return bytesNull, nil + } + + buf := newBuffer() + err := self.encode(buf) + if err != nil { + freeBuffer(buf) + return nil, err + } + var ret []byte + if !rt.CanSizeResue(cap(*buf)) { + ret = *buf + } else { + ret = make([]byte, len(*buf)) + copy(ret, *buf) + freeBuffer(buf) + } + return ret, err +} + +func newBuffer() *[]byte { + if ret := bytesPool.Get(); ret != nil { + return ret.(*[]byte) + } else { + buf := make([]byte, 0, option.DefaultAstBufferSize) + return &buf + } +} + +func freeBuffer(buf *[]byte) { + if !rt.CanSizeResue(cap(*buf)) { + return + } + *buf = (*buf)[:0] + bytesPool.Put(buf) +} + +func (self *Node) encode(buf *[]byte) error { + if self.isRaw() { + return self.encodeRaw(buf) + } + switch int(self.itype()) { + case V_NONE : return ErrNotExist + case V_ERROR : return self.Check() + case V_NULL : return self.encodeNull(buf) + case V_TRUE : return self.encodeTrue(buf) + case V_FALSE : return self.encodeFalse(buf) + case V_ARRAY : return self.encodeArray(buf) + case V_OBJECT: return self.encodeObject(buf) + case V_STRING: return self.encodeString(buf) + case V_NUMBER: return self.encodeNumber(buf) + case V_ANY : return self.encodeInterface(buf) + default : return ErrUnsupportType + } +} + +func (self *Node) encodeRaw(buf *[]byte) error { + lock := self.rlock() + if !self.isRaw() { + self.runlock() + return self.encode(buf) + } + raw := self.toString() + if lock { + self.runlock() + } + *buf = append(*buf, raw...) + return nil +} + +func (self *Node) encodeNull(buf *[]byte) error { + *buf = append(*buf, strNull...) + return nil +} + +func (self *Node) encodeTrue(buf *[]byte) error { + *buf = append(*buf, bytesTrue...) + return nil +} + +func (self *Node) encodeFalse(buf *[]byte) error { + *buf = append(*buf, bytesFalse...) + return nil +} + +func (self *Node) encodeNumber(buf *[]byte) error { + str := self.toString() + *buf = append(*buf, str...) + return nil +} + +func (self *Node) encodeString(buf *[]byte) error { + if self.l == 0 { + *buf = append(*buf, '"', '"') + return nil + } + + quote(buf, self.toString()) + return nil +} + +func (self *Node) encodeArray(buf *[]byte) error { + if self.isLazy() { + if err := self.skipAllIndex(); err != nil { + return err + } + } + + nb := self.len() + if nb == 0 { + *buf = append(*buf, bytesArray...) + return nil + } + + *buf = append(*buf, '[') + + var started bool + for i := 0; i < nb; i++ { + n := self.nodeAt(i) + if !n.Exists() { + continue + } + if started { + *buf = append(*buf, ',') + } + started = true + if err := n.encode(buf); err != nil { + return err + } + } + + *buf = append(*buf, ']') + return nil +} + +func (self *Pair) encode(buf *[]byte) error { + if len(*buf) == 0 { + *buf = append(*buf, '"', '"', ':') + return self.Value.encode(buf) + } + + quote(buf, self.Key) + *buf = append(*buf, ':') + + return self.Value.encode(buf) +} + +func (self *Node) encodeObject(buf *[]byte) error { + if self.isLazy() { + if err := self.skipAllKey(); err != nil { + return err + } + } + + nb := self.len() + if nb == 0 { + *buf = append(*buf, bytesObject...) + return nil + } + + *buf = append(*buf, '{') + + var started bool + for i := 0; i < nb; i++ { + n := self.pairAt(i) + if n == nil || !n.Value.Exists() { + continue + } + if started { + *buf = append(*buf, ',') + } + started = true + if err := n.encode(buf); err != nil { + return err + } + } + + *buf = append(*buf, '}') + return nil +} diff --git a/vendor/github.com/bytedance/sonic/ast/error.go b/vendor/github.com/bytedance/sonic/ast/error.go new file mode 100644 index 00000000000..3716e7a91bd --- /dev/null +++ b/vendor/github.com/bytedance/sonic/ast/error.go @@ -0,0 +1,134 @@ +package ast + +import ( + `fmt` + `strings` + `unsafe` + + `github.com/bytedance/sonic/internal/native/types` +) + + +func newError(err types.ParsingError, msg string) *Node { + return &Node{ + t: V_ERROR, + l: uint(err), + p: unsafe.Pointer(&msg), + } +} + +func newErrorPair(err SyntaxError) *Pair { + return &Pair{0, "", *newSyntaxError(err)} +} + +// Error returns error message if the node is invalid +func (self Node) Error() string { + if self.t != V_ERROR { + return "" + } else { + return *(*string)(self.p) + } +} + +func newSyntaxError(err SyntaxError) *Node { + msg := err.Description() + return &Node{ + t: V_ERROR, + l: uint(err.Code), + p: unsafe.Pointer(&msg), + } +} + +func (self *Parser) syntaxError(err types.ParsingError) SyntaxError { + return SyntaxError{ + Pos : self.p, + Src : self.s, + Code: err, + } +} + +func unwrapError(err error) *Node { + if se, ok := err.(*Node); ok { + return se + }else if sse, ok := err.(Node); ok { + return &sse + } else { + msg := err.Error() + return &Node{ + t: V_ERROR, + p: unsafe.Pointer(&msg), + } + } +} + +type SyntaxError struct { + Pos int + Src string + Code types.ParsingError + Msg string +} + +func (self SyntaxError) Error() string { + return fmt.Sprintf("%q", self.Description()) +} + +func (self SyntaxError) Description() string { + return "Syntax error " + self.description() +} + +func (self SyntaxError) description() string { + i := 16 + p := self.Pos - i + q := self.Pos + i + + /* check for empty source */ + if self.Src == "" { + return fmt.Sprintf("no sources available, the input json is empty: %#v", self) + } + + /* prevent slicing before the beginning */ + if p < 0 { + p, q, i = 0, q - p, i + p + } + + /* prevent slicing beyond the end */ + if n := len(self.Src); q > n { + n = q - n + q = len(self.Src) + + /* move the left bound if possible */ + if p > n { + i += n + p -= n + } + } + + /* left and right length */ + x := clamp_zero(i) + y := clamp_zero(q - p - i - 1) + + /* compose the error description */ + return fmt.Sprintf( + "at index %d: %s\n\n\t%s\n\t%s^%s\n", + self.Pos, + self.Message(), + self.Src[p:q], + strings.Repeat(".", x), + strings.Repeat(".", y), + ) +} + +func (self SyntaxError) Message() string { + if self.Msg == "" { + return self.Code.Message() + } + return self.Msg +} + +func clamp_zero(v int) int { + if v < 0 { + return 0 + } else { + return v + } +} diff --git a/vendor/github.com/bytedance/sonic/ast/iterator.go b/vendor/github.com/bytedance/sonic/ast/iterator.go new file mode 100644 index 00000000000..1052dd0a0d9 --- /dev/null +++ b/vendor/github.com/bytedance/sonic/ast/iterator.go @@ -0,0 +1,216 @@ +/* + * Copyright 2021 ByteDance Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ast + +import ( + "fmt" + + "github.com/bytedance/sonic/internal/caching" + "github.com/bytedance/sonic/internal/native/types" +) + +type Pair struct { + hash uint64 + Key string + Value Node +} + +func NewPair(key string, val Node) Pair { + return Pair{ + hash: caching.StrHash(key), + Key: key, + Value: val, + } +} + +// Values returns iterator for array's children traversal +func (self *Node) Values() (ListIterator, error) { + if err := self.should(types.V_ARRAY); err != nil { + return ListIterator{}, err + } + return self.values(), nil +} + +func (self *Node) values() ListIterator { + return ListIterator{Iterator{p: self}} +} + +// Properties returns iterator for object's children traversal +func (self *Node) Properties() (ObjectIterator, error) { + if err := self.should(types.V_OBJECT); err != nil { + return ObjectIterator{}, err + } + return self.properties(), nil +} + +func (self *Node) properties() ObjectIterator { + return ObjectIterator{Iterator{p: self}} +} + +type Iterator struct { + i int + p *Node +} + +func (self *Iterator) Pos() int { + return self.i +} + +func (self *Iterator) Len() int { + return self.p.len() +} + +// HasNext reports if it is the end of iteration or has error. +func (self *Iterator) HasNext() bool { + if !self.p.isLazy() { + return self.p.Valid() && self.i < self.p.len() + } else if self.p.t == _V_ARRAY_LAZY { + return self.p.skipNextNode().Valid() + } else if self.p.t == _V_OBJECT_LAZY { + pair := self.p.skipNextPair() + if pair == nil { + return false + } + return pair.Value.Valid() + } + return false +} + +// ListIterator is specialized iterator for V_ARRAY +type ListIterator struct { + Iterator +} + +// ObjectIterator is specialized iterator for V_ARRAY +type ObjectIterator struct { + Iterator +} + +func (self *ListIterator) next() *Node { +next_start: + if !self.HasNext() { + return nil + } else { + n := self.p.nodeAt(self.i) + self.i++ + if !n.Exists() { + goto next_start + } + return n + } +} + +// Next scans through children of underlying V_ARRAY, +// copies each child to v, and returns .HasNext(). +func (self *ListIterator) Next(v *Node) bool { + n := self.next() + if n == nil { + return false + } + *v = *n + return true +} + +func (self *ObjectIterator) next() *Pair { +next_start: + if !self.HasNext() { + return nil + } else { + n := self.p.pairAt(self.i) + self.i++ + if n == nil || !n.Value.Exists() { + goto next_start + } + return n + } +} + +// Next scans through children of underlying V_OBJECT, +// copies each child to v, and returns .HasNext(). +func (self *ObjectIterator) Next(p *Pair) bool { + n := self.next() + if n == nil { + return false + } + *p = *n + return true +} + +// Sequence represents scanning path of single-layer nodes. +// Index indicates the value's order in both V_ARRAY and V_OBJECT json. +// Key is the value's key (for V_OBJECT json only, otherwise it will be nil). +type Sequence struct { + Index int + Key *string + // Level int +} + +// String is string representation of one Sequence +func (s Sequence) String() string { + k := "" + if s.Key != nil { + k = *s.Key + } + return fmt.Sprintf("Sequence(%d, %q)", s.Index, k) +} + +type Scanner func(path Sequence, node *Node) bool + +// ForEach scans one V_OBJECT node's children from JSON head to tail, +// and pass the Sequence and Node of corresponding JSON value. +// +// Especially, if the node is not V_ARRAY or V_OBJECT, +// the node itself will be returned and Sequence.Index == -1. +// +// NOTICE: A unsetted node WON'T trigger sc, but its index still counts into Path.Index +func (self *Node) ForEach(sc Scanner) error { + if err := self.checkRaw(); err != nil { + return err + } + switch self.itype() { + case types.V_ARRAY: + iter, err := self.Values() + if err != nil { + return err + } + v := iter.next() + for v != nil { + if !sc(Sequence{iter.i-1, nil}, v) { + return nil + } + v = iter.next() + } + case types.V_OBJECT: + iter, err := self.Properties() + if err != nil { + return err + } + v := iter.next() + for v != nil { + if !sc(Sequence{iter.i-1, &v.Key}, &v.Value) { + return nil + } + v = iter.next() + } + default: + if self.Check() != nil { + return self + } + sc(Sequence{-1, nil}, self) + } + return nil +} diff --git a/vendor/github.com/bytedance/sonic/ast/node.go b/vendor/github.com/bytedance/sonic/ast/node.go new file mode 100644 index 00000000000..3b1aee47cda --- /dev/null +++ b/vendor/github.com/bytedance/sonic/ast/node.go @@ -0,0 +1,1843 @@ +/* + * Copyright 2021 ByteDance Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ast + +import ( + "encoding/json" + "fmt" + "strconv" + "sync" + "sync/atomic" + "unsafe" + + "github.com/bytedance/sonic/internal/native/types" + "github.com/bytedance/sonic/internal/rt" +) + +const ( + _V_NONE types.ValueType = 0 + _V_NODE_BASE types.ValueType = 1 << 5 + _V_LAZY types.ValueType = 1 << 7 + _V_RAW types.ValueType = 1 << 8 + _V_NUMBER = _V_NODE_BASE + 1 + _V_ANY = _V_NODE_BASE + 2 + _V_ARRAY_LAZY = _V_LAZY | types.V_ARRAY + _V_OBJECT_LAZY = _V_LAZY | types.V_OBJECT + _MASK_LAZY = _V_LAZY - 1 + _MASK_RAW = _V_RAW - 1 +) + +const ( + V_NONE = 0 + V_ERROR = 1 + V_NULL = int(types.V_NULL) + V_TRUE = int(types.V_TRUE) + V_FALSE = int(types.V_FALSE) + V_ARRAY = int(types.V_ARRAY) + V_OBJECT = int(types.V_OBJECT) + V_STRING = int(types.V_STRING) + V_NUMBER = int(_V_NUMBER) + V_ANY = int(_V_ANY) +) + +type Node struct { + t types.ValueType + l uint + p unsafe.Pointer + m *sync.RWMutex +} + +// UnmarshalJSON is just an adapter to json.Unmarshaler. +// If you want better performance, use Searcher.GetByPath() directly +func (self *Node) UnmarshalJSON(data []byte) (err error) { + *self = NewRaw(string(data)) + return self.Check() +} + +/** Node Type Accessor **/ + +// Type returns json type represented by the node +// It will be one of bellows: +// V_NONE = 0 (empty node, key not exists) +// V_ERROR = 1 (error node) +// V_NULL = 2 (json value `null`, key exists) +// V_TRUE = 3 (json value `true`) +// V_FALSE = 4 (json value `false`) +// V_ARRAY = 5 (json value array) +// V_OBJECT = 6 (json value object) +// V_STRING = 7 (json value string) +// V_NUMBER = 33 (json value number ) +// V_ANY = 34 (golang interface{}) +// +// Deprecated: not concurrent safe. Use TypeSafe instead +func (self Node) Type() int { + return int(self.t & _MASK_LAZY & _MASK_RAW) +} + +// Type concurrently-safe returns json type represented by the node +// It will be one of bellows: +// V_NONE = 0 (empty node, key not exists) +// V_ERROR = 1 (error node) +// V_NULL = 2 (json value `null`, key exists) +// V_TRUE = 3 (json value `true`) +// V_FALSE = 4 (json value `false`) +// V_ARRAY = 5 (json value array) +// V_OBJECT = 6 (json value object) +// V_STRING = 7 (json value string) +// V_NUMBER = 33 (json value number ) +// V_ANY = 34 (golang interface{}) +func (self *Node) TypeSafe() int { + return int(self.loadt() & _MASK_LAZY & _MASK_RAW) +} + +func (self *Node) itype() types.ValueType { + return self.t & _MASK_LAZY & _MASK_RAW +} + +// Exists returns false only if the self is nil or empty node V_NONE +func (self *Node) Exists() bool { + if self == nil { + return false + } + t := self.loadt() + return t != V_ERROR && t != _V_NONE +} + +// Valid reports if self is NOT V_ERROR or nil +func (self *Node) Valid() bool { + if self == nil { + return false + } + return self.loadt() != V_ERROR +} + +// Check checks if the node itself is valid, and return: +// - ErrNotExist If the node is nil +// - Its underlying error If the node is V_ERROR +func (self *Node) Check() error { + if self == nil { + return ErrNotExist + } else if self.loadt() != V_ERROR { + return nil + } else { + return self + } +} + +// isRaw returns true if node's underlying value is raw json +// +// Deprecated: not concurrent safe +func (self Node) IsRaw() bool { + return self.t & _V_RAW != 0 +} + +// IsRaw returns true if node's underlying value is raw json +func (self *Node) isRaw() bool { + return self.loadt() & _V_RAW != 0 +} + +func (self *Node) isLazy() bool { + return self != nil && self.t & _V_LAZY != 0 +} + +func (self *Node) isAny() bool { + return self != nil && self.loadt() == _V_ANY +} + +/** Simple Value Methods **/ + +// Raw returns json representation of the node, +func (self *Node) Raw() (string, error) { + if self == nil { + return "", ErrNotExist + } + lock := self.rlock() + if !self.isRaw() { + if lock { + self.runlock() + } + buf, err := self.MarshalJSON() + return rt.Mem2Str(buf), err + } + ret := self.toString() + if lock { + self.runlock() + } + return ret, nil +} + +func (self *Node) checkRaw() error { + if err := self.Check(); err != nil { + return err + } + if self.isRaw() { + self.parseRaw(false) + } + return self.Check() +} + +// Bool returns bool value represented by this node, +// including types.V_TRUE|V_FALSE|V_NUMBER|V_STRING|V_ANY|V_NULL, +// V_NONE will return error +func (self *Node) Bool() (bool, error) { + if err := self.checkRaw(); err != nil { + return false, err + } + switch self.t { + case types.V_TRUE : return true , nil + case types.V_FALSE : return false, nil + case types.V_NULL : return false, nil + case _V_NUMBER : + if i, err := self.toInt64(); err == nil { + return i != 0, nil + } else if f, err := self.toFloat64(); err == nil { + return f != 0, nil + } else { + return false, err + } + case types.V_STRING: return strconv.ParseBool(self.toString()) + case _V_ANY : + any := self.packAny() + switch v := any.(type) { + case bool : return v, nil + case int : return v != 0, nil + case int8 : return v != 0, nil + case int16 : return v != 0, nil + case int32 : return v != 0, nil + case int64 : return v != 0, nil + case uint : return v != 0, nil + case uint8 : return v != 0, nil + case uint16 : return v != 0, nil + case uint32 : return v != 0, nil + case uint64 : return v != 0, nil + case float32: return v != 0, nil + case float64: return v != 0, nil + case string : return strconv.ParseBool(v) + case json.Number: + if i, err := v.Int64(); err == nil { + return i != 0, nil + } else if f, err := v.Float64(); err == nil { + return f != 0, nil + } else { + return false, err + } + default: return false, ErrUnsupportType + } + default : return false, ErrUnsupportType + } +} + +// Int64 casts the node to int64 value, +// including V_NUMBER|V_TRUE|V_FALSE|V_ANY|V_STRING +// V_NONE it will return error +func (self *Node) Int64() (int64, error) { + if err := self.checkRaw(); err != nil { + return 0, err + } + switch self.t { + case _V_NUMBER, types.V_STRING : + if i, err := self.toInt64(); err == nil { + return i, nil + } else if f, err := self.toFloat64(); err == nil { + return int64(f), nil + } else { + return 0, err + } + case types.V_TRUE : return 1, nil + case types.V_FALSE : return 0, nil + case types.V_NULL : return 0, nil + case _V_ANY : + any := self.packAny() + switch v := any.(type) { + case bool : if v { return 1, nil } else { return 0, nil } + case int : return int64(v), nil + case int8 : return int64(v), nil + case int16 : return int64(v), nil + case int32 : return int64(v), nil + case int64 : return int64(v), nil + case uint : return int64(v), nil + case uint8 : return int64(v), nil + case uint16 : return int64(v), nil + case uint32 : return int64(v), nil + case uint64 : return int64(v), nil + case float32: return int64(v), nil + case float64: return int64(v), nil + case string : + if i, err := strconv.ParseInt(v, 10, 64); err == nil { + return i, nil + } else if f, err := strconv.ParseFloat(v, 64); err == nil { + return int64(f), nil + } else { + return 0, err + } + case json.Number: + if i, err := v.Int64(); err == nil { + return i, nil + } else if f, err := v.Float64(); err == nil { + return int64(f), nil + } else { + return 0, err + } + default: return 0, ErrUnsupportType + } + default : return 0, ErrUnsupportType + } +} + +// StrictInt64 exports underlying int64 value, including V_NUMBER, V_ANY +func (self *Node) StrictInt64() (int64, error) { + if err := self.checkRaw(); err != nil { + return 0, err + } + switch self.t { + case _V_NUMBER : return self.toInt64() + case _V_ANY : + any := self.packAny() + switch v := any.(type) { + case int : return int64(v), nil + case int8 : return int64(v), nil + case int16 : return int64(v), nil + case int32 : return int64(v), nil + case int64 : return int64(v), nil + case uint : return int64(v), nil + case uint8 : return int64(v), nil + case uint16: return int64(v), nil + case uint32: return int64(v), nil + case uint64: return int64(v), nil + case json.Number: + if i, err := v.Int64(); err == nil { + return i, nil + } else { + return 0, err + } + default: return 0, ErrUnsupportType + } + default : return 0, ErrUnsupportType + } +} + +func castNumber(v bool) json.Number { + if v { + return json.Number("1") + } else { + return json.Number("0") + } +} + +// Number casts node to float64, +// including V_NUMBER|V_TRUE|V_FALSE|V_ANY|V_STRING|V_NULL, +// V_NONE it will return error +func (self *Node) Number() (json.Number, error) { + if err := self.checkRaw(); err != nil { + return json.Number(""), err + } + switch self.t { + case _V_NUMBER : return self.toNumber(), nil + case types.V_STRING : + if _, err := self.toInt64(); err == nil { + return self.toNumber(), nil + } else if _, err := self.toFloat64(); err == nil { + return self.toNumber(), nil + } else { + return json.Number(""), err + } + case types.V_TRUE : return json.Number("1"), nil + case types.V_FALSE : return json.Number("0"), nil + case types.V_NULL : return json.Number("0"), nil + case _V_ANY : + any := self.packAny() + switch v := any.(type) { + case bool : return castNumber(v), nil + case int : return castNumber(v != 0), nil + case int8 : return castNumber(v != 0), nil + case int16 : return castNumber(v != 0), nil + case int32 : return castNumber(v != 0), nil + case int64 : return castNumber(v != 0), nil + case uint : return castNumber(v != 0), nil + case uint8 : return castNumber(v != 0), nil + case uint16 : return castNumber(v != 0), nil + case uint32 : return castNumber(v != 0), nil + case uint64 : return castNumber(v != 0), nil + case float32: return castNumber(v != 0), nil + case float64: return castNumber(v != 0), nil + case string : + if _, err := strconv.ParseFloat(v, 64); err == nil { + return json.Number(v), nil + } else { + return json.Number(""), err + } + case json.Number: return v, nil + default: return json.Number(""), ErrUnsupportType + } + default : return json.Number(""), ErrUnsupportType + } +} + +// Number exports underlying float64 value, including V_NUMBER, V_ANY of json.Number +func (self *Node) StrictNumber() (json.Number, error) { + if err := self.checkRaw(); err != nil { + return json.Number(""), err + } + switch self.t { + case _V_NUMBER : return self.toNumber() , nil + case _V_ANY : + if v, ok := self.packAny().(json.Number); ok { + return v, nil + } else { + return json.Number(""), ErrUnsupportType + } + default : return json.Number(""), ErrUnsupportType + } +} + +// String cast node to string, +// including V_NUMBER|V_TRUE|V_FALSE|V_ANY|V_STRING|V_NULL, +// V_NONE it will return error +func (self *Node) String() (string, error) { + if err := self.checkRaw(); err != nil { + return "", err + } + switch self.t { + case types.V_NULL : return "" , nil + case types.V_TRUE : return "true" , nil + case types.V_FALSE : return "false", nil + case types.V_STRING, _V_NUMBER : return self.toString(), nil + case _V_ANY : + any := self.packAny() + switch v := any.(type) { + case bool : return strconv.FormatBool(v), nil + case int : return strconv.Itoa(v), nil + case int8 : return strconv.Itoa(int(v)), nil + case int16 : return strconv.Itoa(int(v)), nil + case int32 : return strconv.Itoa(int(v)), nil + case int64 : return strconv.Itoa(int(v)), nil + case uint : return strconv.Itoa(int(v)), nil + case uint8 : return strconv.Itoa(int(v)), nil + case uint16 : return strconv.Itoa(int(v)), nil + case uint32 : return strconv.Itoa(int(v)), nil + case uint64 : return strconv.Itoa(int(v)), nil + case float32: return strconv.FormatFloat(float64(v), 'g', -1, 64), nil + case float64: return strconv.FormatFloat(float64(v), 'g', -1, 64), nil + case string : return v, nil + case json.Number: return v.String(), nil + default: return "", ErrUnsupportType + } + default : return "" , ErrUnsupportType + } +} + +// StrictString returns string value (unescaped), including V_STRING, V_ANY of string. +// In other cases, it will return empty string. +func (self *Node) StrictString() (string, error) { + if err := self.checkRaw(); err != nil { + return "", err + } + switch self.t { + case types.V_STRING : return self.toString(), nil + case _V_ANY : + if v, ok := self.packAny().(string); ok { + return v, nil + } else { + return "", ErrUnsupportType + } + default : return "", ErrUnsupportType + } +} + +// Float64 cast node to float64, +// including V_NUMBER|V_TRUE|V_FALSE|V_ANY|V_STRING|V_NULL, +// V_NONE it will return error +func (self *Node) Float64() (float64, error) { + if err := self.checkRaw(); err != nil { + return 0.0, err + } + switch self.t { + case _V_NUMBER, types.V_STRING : return self.toFloat64() + case types.V_TRUE : return 1.0, nil + case types.V_FALSE : return 0.0, nil + case types.V_NULL : return 0.0, nil + case _V_ANY : + any := self.packAny() + switch v := any.(type) { + case bool : + if v { + return 1.0, nil + } else { + return 0.0, nil + } + case int : return float64(v), nil + case int8 : return float64(v), nil + case int16 : return float64(v), nil + case int32 : return float64(v), nil + case int64 : return float64(v), nil + case uint : return float64(v), nil + case uint8 : return float64(v), nil + case uint16 : return float64(v), nil + case uint32 : return float64(v), nil + case uint64 : return float64(v), nil + case float32: return float64(v), nil + case float64: return float64(v), nil + case string : + if f, err := strconv.ParseFloat(v, 64); err == nil { + return float64(f), nil + } else { + return 0, err + } + case json.Number: + if f, err := v.Float64(); err == nil { + return float64(f), nil + } else { + return 0, err + } + default : return 0, ErrUnsupportType + } + default : return 0.0, ErrUnsupportType + } +} + +// Float64 exports underlying float64 value, including V_NUMBER, V_ANY +func (self *Node) StrictFloat64() (float64, error) { + if err := self.checkRaw(); err != nil { + return 0.0, err + } + switch self.t { + case _V_NUMBER : return self.toFloat64() + case _V_ANY : + any := self.packAny() + switch v := any.(type) { + case float32 : return float64(v), nil + case float64 : return float64(v), nil + default : return 0, ErrUnsupportType + } + default : return 0.0, ErrUnsupportType + } +} + +/** Sequential Value Methods **/ + +// Len returns children count of a array|object|string node +// WARN: For partially loaded node, it also works but only counts the parsed children +func (self *Node) Len() (int, error) { + if err := self.checkRaw(); err != nil { + return 0, err + } + if self.t == types.V_ARRAY || self.t == types.V_OBJECT || self.t == _V_ARRAY_LAZY || self.t == _V_OBJECT_LAZY || self.t == types.V_STRING { + return int(self.l), nil + } else if self.t == _V_NONE || self.t == types.V_NULL { + return 0, nil + } else { + return 0, ErrUnsupportType + } +} + +func (self *Node) len() int { + return int(self.l) +} + +// Cap returns malloc capacity of a array|object node for children +func (self *Node) Cap() (int, error) { + if err := self.checkRaw(); err != nil { + return 0, err + } + switch self.t { + case types.V_ARRAY: return (*linkedNodes)(self.p).Cap(), nil + case types.V_OBJECT: return (*linkedPairs)(self.p).Cap(), nil + case _V_ARRAY_LAZY: return (*parseArrayStack)(self.p).v.Cap(), nil + case _V_OBJECT_LAZY: return (*parseObjectStack)(self.p).v.Cap(), nil + case _V_NONE, types.V_NULL: return 0, nil + default: return 0, ErrUnsupportType + } +} + +// Set sets the node of given key under self, and reports if the key has existed. +// +// If self is V_NONE or V_NULL, it becomes V_OBJECT and sets the node at the key. +func (self *Node) Set(key string, node Node) (bool, error) { + if err := self.checkRaw(); err != nil { + return false, err + } + if err := node.Check(); err != nil { + return false, err + } + + if self.t == _V_NONE || self.t == types.V_NULL { + *self = NewObject([]Pair{NewPair(key, node)}) + return false, nil + } else if self.itype() != types.V_OBJECT { + return false, ErrUnsupportType + } + + p := self.Get(key) + + if !p.Exists() { + // self must be fully-loaded here + if self.len() == 0 { + *self = newObject(new(linkedPairs)) + } + s := (*linkedPairs)(self.p) + s.Push(NewPair(key, node)) + self.l++ + return false, nil + + } else if err := p.Check(); err != nil { + return false, err + } + + *p = node + return true, nil +} + +// SetAny wraps val with V_ANY node, and Set() the node. +func (self *Node) SetAny(key string, val interface{}) (bool, error) { + return self.Set(key, NewAny(val)) +} + +// Unset REMOVE (soft) the node of given key under object parent, and reports if the key has existed. +func (self *Node) Unset(key string) (bool, error) { + if err := self.should(types.V_OBJECT); err != nil { + return false, err + } + // NOTICE: must get accurate length before deduct + if err := self.skipAllKey(); err != nil { + return false, err + } + p, i := self.skipKey(key) + if !p.Exists() { + return false, nil + } else if err := p.Check(); err != nil { + return false, err + } + self.removePairAt(i) + return true, nil +} + +// SetByIndex sets the node of given index, and reports if the key has existed. +// +// The index must be within self's children. +func (self *Node) SetByIndex(index int, node Node) (bool, error) { + if err := self.checkRaw(); err != nil { + return false, err + } + if err := node.Check(); err != nil { + return false, err + } + + if index == 0 && (self.t == _V_NONE || self.t == types.V_NULL) { + *self = NewArray([]Node{node}) + return false, nil + } + + p := self.Index(index) + if !p.Exists() { + return false, ErrNotExist + } else if err := p.Check(); err != nil { + return false, err + } + + *p = node + return true, nil +} + +// SetAny wraps val with V_ANY node, and SetByIndex() the node. +func (self *Node) SetAnyByIndex(index int, val interface{}) (bool, error) { + return self.SetByIndex(index, NewAny(val)) +} + +// UnsetByIndex REMOVE (softly) the node of given index. +// +// WARN: this will change address of elements, which is a dangerous action. +// Use Unset() for object or Pop() for array instead. +func (self *Node) UnsetByIndex(index int) (bool, error) { + if err := self.checkRaw(); err != nil { + return false, err + } + + var p *Node + it := self.itype() + + if it == types.V_ARRAY { + if err := self.skipAllIndex(); err != nil { + return false, err + } + p = self.nodeAt(index) + } else if it == types.V_OBJECT { + if err := self.skipAllKey(); err != nil { + return false, err + } + pr := self.pairAt(index) + if pr == nil { + return false, ErrNotExist + } + p = &pr.Value + } else { + return false, ErrUnsupportType + } + + if !p.Exists() { + return false, ErrNotExist + } + + // last elem + if index == self.len() - 1 { + return true, self.Pop() + } + + // not last elem, self.len() change but linked-chunk not change + if it == types.V_ARRAY { + self.removeNode(index) + }else if it == types.V_OBJECT { + self.removePair(index) + } + return true, nil +} + +// Add appends the given node under self. +// +// If self is V_NONE or V_NULL, it becomes V_ARRAY and sets the node at index 0. +func (self *Node) Add(node Node) error { + if err := self.checkRaw(); err != nil { + return err + } + + if self != nil && (self.t == _V_NONE || self.t == types.V_NULL) { + *self = NewArray([]Node{node}) + return nil + } + if err := self.should(types.V_ARRAY); err != nil { + return err + } + + s, err := self.unsafeArray() + if err != nil { + return err + } + + // Notice: array won't have unset node in tail + s.Push(node) + self.l++ + return nil +} + +// Pop remove the last child of the V_Array or V_Object node. +func (self *Node) Pop() error { + if err := self.checkRaw(); err != nil { + return err + } + + if it := self.itype(); it == types.V_ARRAY { + s, err := self.unsafeArray() + if err != nil { + return err + } + // remove tail unset nodes + for i := s.Len()-1; i >= 0; i-- { + if s.At(i).Exists() { + s.Pop() + self.l-- + break + } + s.Pop() + } + + } else if it == types.V_OBJECT { + s, err := self.unsafeMap() + if err != nil { + return err + } + // remove tail unset nodes + for i := s.Len()-1; i >= 0; i-- { + if p := s.At(i); p != nil && p.Value.Exists() { + s.Pop() + self.l-- + break + } + s.Pop() + } + + } else { + return ErrUnsupportType + } + + return nil +} + +// Move moves the child at src index to dst index, +// meanwhile slides sliblings from src+1 to dst. +// +// WARN: this will change address of elements, which is a dangerous action. +func (self *Node) Move(dst, src int) error { + if err := self.should(types.V_ARRAY); err != nil { + return err + } + + s, err := self.unsafeArray() + if err != nil { + return err + } + + // check if any unset node exists + if l := s.Len(); self.len() != l { + di, si := dst, src + // find real pos of src and dst + for i := 0; i < l; i++ { + if s.At(i).Exists() { + di-- + si-- + } + if di == -1 { + dst = i + di-- + } + if si == -1 { + src = i + si-- + } + if di == -2 && si == -2 { + break + } + } + } + + s.MoveOne(src, dst) + return nil +} + +// SetAny wraps val with V_ANY node, and Add() the node. +func (self *Node) AddAny(val interface{}) error { + return self.Add(NewAny(val)) +} + +// GetByPath load given path on demands, +// which only ensure nodes before this path got parsed. +// +// Note, the api expects the json is well-formed at least, +// otherwise it may return unexpected result. +func (self *Node) GetByPath(path ...interface{}) *Node { + if !self.Valid() { + return self + } + var s = self + for _, p := range path { + switch p := p.(type) { + case int: + s = s.Index(p) + if !s.Valid() { + return s + } + case string: + s = s.Get(p) + if !s.Valid() { + return s + } + default: + panic("path must be either int or string") + } + } + return s +} + +// Get loads given key of an object node on demands +func (self *Node) Get(key string) *Node { + if err := self.should(types.V_OBJECT); err != nil { + return unwrapError(err) + } + n, _ := self.skipKey(key) + return n +} + +// Index indexies node at given idx, +// node type CAN be either V_OBJECT or V_ARRAY +func (self *Node) Index(idx int) *Node { + if err := self.checkRaw(); err != nil { + return unwrapError(err) + } + + it := self.itype() + if it == types.V_ARRAY { + return self.skipIndex(idx) + + }else if it == types.V_OBJECT { + pr := self.skipIndexPair(idx) + if pr == nil { + return newError(_ERR_NOT_FOUND, "value not exists") + } + return &pr.Value + + } else { + return newError(_ERR_UNSUPPORT_TYPE, fmt.Sprintf("unsupported type: %v", self.itype())) + } +} + +// IndexPair indexies pair at given idx, +// node type MUST be either V_OBJECT +func (self *Node) IndexPair(idx int) *Pair { + if err := self.should(types.V_OBJECT); err != nil { + return nil + } + return self.skipIndexPair(idx) +} + +func (self *Node) indexOrGet(idx int, key string) (*Node, int) { + if err := self.should(types.V_OBJECT); err != nil { + return unwrapError(err), idx + } + + pr := self.skipIndexPair(idx) + if pr != nil && pr.Key == key { + return &pr.Value, idx + } + + return self.skipKey(key) +} + +// IndexOrGet firstly use idx to index a value and check if its key matches +// If not, then use the key to search value +func (self *Node) IndexOrGet(idx int, key string) *Node { + node, _ := self.indexOrGet(idx, key) + return node +} + +// IndexOrGetWithIdx attempts to retrieve a node by index and key, returning the node and its correct index. +// If the key does not match at the given index, it searches by key and returns the node with its updated index. +func (self *Node) IndexOrGetWithIdx(idx int, key string) (*Node, int) { + return self.indexOrGet(idx, key) +} + +/** Generic Value Converters **/ + +// Map loads all keys of an object node +func (self *Node) Map() (map[string]interface{}, error) { + if self.isAny() { + any := self.packAny() + if v, ok := any.(map[string]interface{}); ok { + return v, nil + } else { + return nil, ErrUnsupportType + } + } + if err := self.should(types.V_OBJECT); err != nil { + return nil, err + } + if err := self.loadAllKey(false); err != nil { + return nil, err + } + return self.toGenericObject() +} + +// MapUseNumber loads all keys of an object node, with numeric nodes casted to json.Number +func (self *Node) MapUseNumber() (map[string]interface{}, error) { + if self.isAny() { + any := self.packAny() + if v, ok := any.(map[string]interface{}); ok { + return v, nil + } else { + return nil, ErrUnsupportType + } + } + if err := self.should(types.V_OBJECT); err != nil { + return nil, err + } + if err := self.loadAllKey(false); err != nil { + return nil, err + } + return self.toGenericObjectUseNumber() +} + +// MapUseNode scans both parsed and non-parsed children nodes, +// and map them by their keys +func (self *Node) MapUseNode() (map[string]Node, error) { + if self.isAny() { + any := self.packAny() + if v, ok := any.(map[string]Node); ok { + return v, nil + } else { + return nil, ErrUnsupportType + } + } + if err := self.should(types.V_OBJECT); err != nil { + return nil, err + } + if err := self.skipAllKey(); err != nil { + return nil, err + } + return self.toGenericObjectUseNode() +} + +// MapUnsafe exports the underlying pointer to its children map +// WARN: don't use it unless you know what you are doing +// +// Deprecated: this API now returns copied nodes instead of directly reference, +// func (self *Node) UnsafeMap() ([]Pair, error) { +// if err := self.should(types.V_OBJECT, "an object"); err != nil { +// return nil, err +// } +// if err := self.skipAllKey(); err != nil { +// return nil, err +// } +// return self.toGenericObjectUsePair() +// } + +//go:nocheckptr +func (self *Node) unsafeMap() (*linkedPairs, error) { + if err := self.skipAllKey(); err != nil { + return nil, err + } + if self.p == nil { + *self = newObject(new(linkedPairs)) + } + return (*linkedPairs)(self.p), nil +} + +// SortKeys sorts children of a V_OBJECT node in ascending key-order. +// If recurse is true, it recursively sorts children's children as long as a V_OBJECT node is found. +func (self *Node) SortKeys(recurse bool) error { + // check raw node first + if err := self.checkRaw(); err != nil { + return err + } + if self.itype() == types.V_OBJECT { + return self.sortKeys(recurse) + } else if self.itype() == types.V_ARRAY { + var err error + err2 := self.ForEach(func(path Sequence, node *Node) bool { + it := node.itype() + if it == types.V_ARRAY || it == types.V_OBJECT { + err = node.SortKeys(recurse) + if err != nil { + return false + } + } + return true + }) + if err != nil { + return err + } + return err2 + } else { + return nil + } +} + +func (self *Node) sortKeys(recurse bool) (err error) { + // check raw node first + if err := self.checkRaw(); err != nil { + return err + } + ps, err := self.unsafeMap() + if err != nil { + return err + } + ps.Sort() + if recurse { + var sc Scanner + sc = func(path Sequence, node *Node) bool { + if node.itype() == types.V_OBJECT { + if err := node.sortKeys(recurse); err != nil { + return false + } + } + if node.itype() == types.V_ARRAY { + if err := node.ForEach(sc); err != nil { + return false + } + } + return true + } + if err := self.ForEach(sc); err != nil { + return err + } + } + return nil +} + +// Array loads all indexes of an array node +func (self *Node) Array() ([]interface{}, error) { + if self.isAny() { + any := self.packAny() + if v, ok := any.([]interface{}); ok { + return v, nil + } else { + return nil, ErrUnsupportType + } + } + if err := self.should(types.V_ARRAY); err != nil { + return nil, err + } + if err := self.loadAllIndex(false); err != nil { + return nil, err + } + return self.toGenericArray() +} + +// ArrayUseNumber loads all indexes of an array node, with numeric nodes casted to json.Number +func (self *Node) ArrayUseNumber() ([]interface{}, error) { + if self.isAny() { + any := self.packAny() + if v, ok := any.([]interface{}); ok { + return v, nil + } else { + return nil, ErrUnsupportType + } + } + if err := self.should(types.V_ARRAY); err != nil { + return nil, err + } + if err := self.loadAllIndex(false); err != nil { + return nil, err + } + return self.toGenericArrayUseNumber() +} + +// ArrayUseNode copies both parsed and non-parsed children nodes, +// and indexes them by original order +func (self *Node) ArrayUseNode() ([]Node, error) { + if self.isAny() { + any := self.packAny() + if v, ok := any.([]Node); ok { + return v, nil + } else { + return nil, ErrUnsupportType + } + } + if err := self.should(types.V_ARRAY); err != nil { + return nil, err + } + if err := self.skipAllIndex(); err != nil { + return nil, err + } + return self.toGenericArrayUseNode() +} + +// ArrayUnsafe exports the underlying pointer to its children array +// WARN: don't use it unless you know what you are doing +// +// Deprecated: this API now returns copied nodes instead of directly reference, +// which has no difference with ArrayUseNode +// func (self *Node) UnsafeArray() ([]Node, error) { +// if err := self.should(types.V_ARRAY, "an array"); err != nil { +// return nil, err +// } +// if err := self.skipAllIndex(); err != nil { +// return nil, err +// } +// return self.toGenericArrayUseNode() +// } + +func (self *Node) unsafeArray() (*linkedNodes, error) { + if err := self.skipAllIndex(); err != nil { + return nil, err + } + if self.p == nil { + *self = newArray(new(linkedNodes)) + } + return (*linkedNodes)(self.p), nil +} + +// Interface loads all children under all paths from this node, +// and converts itself as generic type. +// WARN: all numeric nodes are casted to float64 +func (self *Node) Interface() (interface{}, error) { + if err := self.checkRaw(); err != nil { + return nil, err + } + switch self.t { + case V_ERROR : return nil, self.Check() + case types.V_NULL : return nil, nil + case types.V_TRUE : return true, nil + case types.V_FALSE : return false, nil + case types.V_ARRAY : return self.toGenericArray() + case types.V_OBJECT : return self.toGenericObject() + case types.V_STRING : return self.toString(), nil + case _V_NUMBER : + v, err := self.toFloat64() + if err != nil { + return nil, err + } + return v, nil + case _V_ARRAY_LAZY : + if err := self.loadAllIndex(false); err != nil { + return nil, err + } + return self.toGenericArray() + case _V_OBJECT_LAZY : + if err := self.loadAllKey(false); err != nil { + return nil, err + } + return self.toGenericObject() + case _V_ANY: + switch v := self.packAny().(type) { + case Node : return v.Interface() + case *Node: return v.Interface() + default : return v, nil + } + default : return nil, ErrUnsupportType + } +} + +func (self *Node) packAny() interface{} { + return *(*interface{})(self.p) +} + +// InterfaceUseNumber works same with Interface() +// except numeric nodes are casted to json.Number +func (self *Node) InterfaceUseNumber() (interface{}, error) { + if err := self.checkRaw(); err != nil { + return nil, err + } + switch self.t { + case V_ERROR : return nil, self.Check() + case types.V_NULL : return nil, nil + case types.V_TRUE : return true, nil + case types.V_FALSE : return false, nil + case types.V_ARRAY : return self.toGenericArrayUseNumber() + case types.V_OBJECT : return self.toGenericObjectUseNumber() + case types.V_STRING : return self.toString(), nil + case _V_NUMBER : return self.toNumber(), nil + case _V_ARRAY_LAZY : + if err := self.loadAllIndex(false); err != nil { + return nil, err + } + return self.toGenericArrayUseNumber() + case _V_OBJECT_LAZY : + if err := self.loadAllKey(false); err != nil { + return nil, err + } + return self.toGenericObjectUseNumber() + case _V_ANY : return self.packAny(), nil + default : return nil, ErrUnsupportType + } +} + +// InterfaceUseNode clone itself as a new node, +// or its children as map[string]Node (or []Node) +func (self *Node) InterfaceUseNode() (interface{}, error) { + if err := self.checkRaw(); err != nil { + return nil, err + } + switch self.t { + case types.V_ARRAY : return self.toGenericArrayUseNode() + case types.V_OBJECT : return self.toGenericObjectUseNode() + case _V_ARRAY_LAZY : + if err := self.skipAllIndex(); err != nil { + return nil, err + } + return self.toGenericArrayUseNode() + case _V_OBJECT_LAZY : + if err := self.skipAllKey(); err != nil { + return nil, err + } + return self.toGenericObjectUseNode() + default : return *self, self.Check() + } +} + +// LoadAll loads the node's children +// and ensure all its children can be READ concurrently (include its children's children) +func (self *Node) LoadAll() error { + return self.Load() +} + +// Load loads the node's children as parsed. +// and ensure all its children can be READ concurrently (include its children's children) +func (self *Node) Load() error { + switch self.t { + case _V_ARRAY_LAZY: self.loadAllIndex(true) + case _V_OBJECT_LAZY: self.loadAllKey(true) + case V_ERROR: return self + case V_NONE: return nil + } + if self.m == nil { + self.m = new(sync.RWMutex) + } + return self.checkRaw() +} + +/**---------------------------------- Internal Helper Methods ----------------------------------**/ + +func (self *Node) should(t types.ValueType) error { + if err := self.checkRaw(); err != nil { + return err + } + if self.itype() != t { + return ErrUnsupportType + } + return nil +} + +func (self *Node) nodeAt(i int) *Node { + var p *linkedNodes + if self.isLazy() { + _, stack := self.getParserAndArrayStack() + p = &stack.v + } else { + p = (*linkedNodes)(self.p) + if l := p.Len(); l != self.len() { + // some nodes got unset, iterate to skip them + for j:=0; j 0 { + /* linear search */ + var p *Pair + var i int + if lazy { + s := (*parseObjectStack)(self.p) + p, i = s.v.Get(key) + } else { + p, i = (*linkedPairs)(self.p).Get(key) + } + + if p != nil { + return &p.Value, i + } + } + + /* not found */ + if !lazy { + return nil, -1 + } + + // lazy load + for last, i := self.skipNextPair(), nb; last != nil; last, i = self.skipNextPair(), i+1 { + if last.Value.Check() != nil { + return &last.Value, -1 + } + if last.Key == key { + return &last.Value, i + } + } + + return nil, -1 +} + +func (self *Node) skipIndex(index int) *Node { + nb := self.len() + if nb > index { + v := self.nodeAt(index) + return v + } + if !self.isLazy() { + return nil + } + + // lazy load + for last := self.skipNextNode(); last != nil; last = self.skipNextNode(){ + if last.Check() != nil { + return last + } + if self.len() > index { + return last + } + } + + return nil +} + +func (self *Node) skipIndexPair(index int) *Pair { + nb := self.len() + if nb > index { + return self.pairAt(index) + } + if !self.isLazy() { + return nil + } + + // lazy load + for last := self.skipNextPair(); last != nil; last = self.skipNextPair(){ + if last.Value.Check() != nil { + return last + } + if self.len() > index { + return last + } + } + + return nil +} + +func (self *Node) loadAllIndex(loadOnce bool) error { + if !self.isLazy() { + return nil + } + var err types.ParsingError + parser, stack := self.getParserAndArrayStack() + if !loadOnce { + parser.noLazy = true + } else { + parser.loadOnce = true + } + *self, err = parser.decodeArray(&stack.v) + if err != 0 { + return parser.ExportError(err) + } + return nil +} + +func (self *Node) loadAllKey(loadOnce bool) error { + if !self.isLazy() { + return nil + } + var err types.ParsingError + parser, stack := self.getParserAndObjectStack() + if !loadOnce { + parser.noLazy = true + *self, err = parser.decodeObject(&stack.v) + } else { + parser.loadOnce = true + *self, err = parser.decodeObject(&stack.v) + } + if err != 0 { + return parser.ExportError(err) + } + return nil +} + +func (self *Node) removeNode(i int) { + node := self.nodeAt(i) + if node == nil { + return + } + *node = Node{} + // NOTICE: not be consistent with linkedNode.Len() + self.l-- +} + +func (self *Node) removePair(i int) { + last := self.pairAt(i) + if last == nil { + return + } + *last = Pair{} + // NOTICE: should be consistent with linkedPair.Len() + self.l-- +} + +func (self *Node) removePairAt(i int) { + p := (*linkedPairs)(self.p).At(i) + if p == nil { + return + } + *p = Pair{} + // NOTICE: should be consistent with linkedPair.Len() + self.l-- +} + +func (self *Node) toGenericArray() ([]interface{}, error) { + nb := self.len() + if nb == 0 { + return []interface{}{}, nil + } + ret := make([]interface{}, 0, nb) + + /* convert each item */ + it := self.values() + for v := it.next(); v != nil; v = it.next() { + vv, err := v.Interface() + if err != nil { + return nil, err + } + ret = append(ret, vv) + } + + /* all done */ + return ret, nil +} + +func (self *Node) toGenericArrayUseNumber() ([]interface{}, error) { + nb := self.len() + if nb == 0 { + return []interface{}{}, nil + } + ret := make([]interface{}, 0, nb) + + /* convert each item */ + it := self.values() + for v := it.next(); v != nil; v = it.next() { + vv, err := v.InterfaceUseNumber() + if err != nil { + return nil, err + } + ret = append(ret, vv) + } + + /* all done */ + return ret, nil +} + +func (self *Node) toGenericArrayUseNode() ([]Node, error) { + var nb = self.len() + if nb == 0 { + return []Node{}, nil + } + + var s = (*linkedNodes)(self.p) + var out = make([]Node, nb) + s.ToSlice(out) + + return out, nil +} + +func (self *Node) toGenericObject() (map[string]interface{}, error) { + nb := self.len() + if nb == 0 { + return map[string]interface{}{}, nil + } + ret := make(map[string]interface{}, nb) + + /* convert each item */ + it := self.properties() + for v := it.next(); v != nil; v = it.next() { + vv, err := v.Value.Interface() + if err != nil { + return nil, err + } + ret[v.Key] = vv + } + + /* all done */ + return ret, nil +} + + +func (self *Node) toGenericObjectUseNumber() (map[string]interface{}, error) { + nb := self.len() + if nb == 0 { + return map[string]interface{}{}, nil + } + ret := make(map[string]interface{}, nb) + + /* convert each item */ + it := self.properties() + for v := it.next(); v != nil; v = it.next() { + vv, err := v.Value.InterfaceUseNumber() + if err != nil { + return nil, err + } + ret[v.Key] = vv + } + + /* all done */ + return ret, nil +} + +func (self *Node) toGenericObjectUseNode() (map[string]Node, error) { + var nb = self.len() + if nb == 0 { + return map[string]Node{}, nil + } + + var s = (*linkedPairs)(self.p) + var out = make(map[string]Node, nb) + s.ToMap(out) + + /* all done */ + return out, nil +} + +/**------------------------------------ Factory Methods ------------------------------------**/ + +var ( + nullNode = Node{t: types.V_NULL} + trueNode = Node{t: types.V_TRUE} + falseNode = Node{t: types.V_FALSE} +) + +// NewRaw creates a node of raw json. +// If the input json is invalid, NewRaw returns a error Node. +func NewRaw(json string) Node { + parser := NewParserObj(json) + start, err := parser.skip() + if err != 0 { + return *newError(err, err.Message()) + } + it := switchRawType(parser.s[start]) + if it == _V_NONE { + return Node{} + } + return newRawNode(parser.s[start:parser.p], it, false) +} + +// NewRawConcurrentRead creates a node of raw json, which can be READ +// (GetByPath/Get/Index/GetOrIndex/Int64/Bool/Float64/String/Number/Interface/Array/Map/Raw/MarshalJSON) concurrently. +// If the input json is invalid, NewRaw returns a error Node. +func NewRawConcurrentRead(json string) Node { + parser := NewParserObj(json) + start, err := parser.skip() + if err != 0 { + return *newError(err, err.Message()) + } + it := switchRawType(parser.s[start]) + if it == _V_NONE { + return Node{} + } + return newRawNode(parser.s[start:parser.p], it, true) +} + +// NewAny creates a node of type V_ANY if any's type isn't Node or *Node, +// which stores interface{} and can be only used for `.Interface()`\`.MarshalJSON()`. +func NewAny(any interface{}) Node { + switch n := any.(type) { + case Node: + return n + case *Node: + return *n + default: + return Node{ + t: _V_ANY, + p: unsafe.Pointer(&any), + } + } +} + +// NewBytes encodes given src with Base64 (RFC 4648), and creates a node of type V_STRING. +func NewBytes(src []byte) Node { + if len(src) == 0 { + panic("empty src bytes") + } + out := rt.EncodeBase64(src) + return NewString(out) +} + +// NewNull creates a node of type V_NULL +func NewNull() Node { + return Node{ + p: nil, + t: types.V_NULL, + } +} + +// NewBool creates a node of type bool: +// If v is true, returns V_TRUE node +// If v is false, returns V_FALSE node +func NewBool(v bool) Node { + var t = types.V_FALSE + if v { + t = types.V_TRUE + } + return Node{ + p: nil, + t: t, + } +} + +// NewNumber creates a json.Number node +// v must be a decimal string complying with RFC8259 +func NewNumber(v string) Node { + return Node{ + l: uint(len(v)), + p: rt.StrPtr(v), + t: _V_NUMBER, + } +} + +func (node *Node) toNumber() json.Number { + return json.Number(rt.StrFrom(node.p, int64(node.l))) +} + +func (self *Node) toString() string { + return rt.StrFrom(self.p, int64(self.l)) +} + +func (node *Node) toFloat64() (float64, error) { + ret, err := node.toNumber().Float64() + if err != nil { + return 0, err + } + return ret, nil +} + +func (node *Node) toInt64() (int64, error) { + ret,err := node.toNumber().Int64() + if err != nil { + return 0, err + } + return ret, nil +} + +func newBytes(v []byte) Node { + return Node{ + t: types.V_STRING, + p: mem2ptr(v), + l: uint(len(v)), + } +} + +// NewString creates a node of type V_STRING. +// v is considered to be a valid UTF-8 string, +// which means it won't be validated and unescaped. +// when the node is encoded to json, v will be escaped. +func NewString(v string) Node { + return Node{ + t: types.V_STRING, + p: rt.StrPtr(v), + l: uint(len(v)), + } +} + +// NewArray creates a node of type V_ARRAY, +// using v as its underlying children +func NewArray(v []Node) Node { + s := new(linkedNodes) + s.FromSlice(v) + return newArray(s) +} + +const _Threshold_Index = 16 + +func newArray(v *linkedNodes) Node { + return Node{ + t: types.V_ARRAY, + l: uint(v.Len()), + p: unsafe.Pointer(v), + } +} + +func (self *Node) setArray(v *linkedNodes) { + self.t = types.V_ARRAY + self.l = uint(v.Len()) + self.p = unsafe.Pointer(v) +} + +// NewObject creates a node of type V_OBJECT, +// using v as its underlying children +func NewObject(v []Pair) Node { + s := new(linkedPairs) + s.FromSlice(v) + return newObject(s) +} + +func newObject(v *linkedPairs) Node { + if v.size > _Threshold_Index { + v.BuildIndex() + } + return Node{ + t: types.V_OBJECT, + l: uint(v.Len()), + p: unsafe.Pointer(v), + } +} + +func (self *Node) setObject(v *linkedPairs) { + if v.size > _Threshold_Index { + v.BuildIndex() + } + self.t = types.V_OBJECT + self.l = uint(v.Len()) + self.p = unsafe.Pointer(v) +} + +func (self *Node) parseRaw(full bool) { + lock := self.lock() + defer self.unlock() + if !self.isRaw() { + return + } + raw := self.toString() + parser := NewParserObj(raw) + var e types.ParsingError + if full { + parser.noLazy = true + *self, e = parser.Parse() + } else if lock { + var n Node + parser.noLazy = true + parser.loadOnce = true + n, e = parser.Parse() + self.assign(n) + } else { + *self, e = parser.Parse() + } + if e != 0 { + *self = *newSyntaxError(parser.syntaxError(e)) + } +} + +func (self *Node) assign(n Node) { + self.l = n.l + self.p = n.p + atomic.StoreInt64(&self.t, n.t) +} diff --git a/vendor/github.com/bytedance/sonic/ast/parser.go b/vendor/github.com/bytedance/sonic/ast/parser.go new file mode 100644 index 00000000000..30bd1f4519b --- /dev/null +++ b/vendor/github.com/bytedance/sonic/ast/parser.go @@ -0,0 +1,766 @@ +/* + * Copyright 2021 ByteDance Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ast + +import ( + "fmt" + "sync" + "sync/atomic" + + "github.com/bytedance/sonic/internal/native/types" + "github.com/bytedance/sonic/internal/rt" +) + +const ( + _DEFAULT_NODE_CAP int = 16 + _APPEND_GROW_SHIFT = 1 +) + +const ( + _ERR_NOT_FOUND types.ParsingError = 33 + _ERR_UNSUPPORT_TYPE types.ParsingError = 34 +) + +var ( + // ErrNotExist means both key and value doesn't exist + ErrNotExist error = newError(_ERR_NOT_FOUND, "value not exists") + + // ErrUnsupportType means API on the node is unsupported + ErrUnsupportType error = newError(_ERR_UNSUPPORT_TYPE, "unsupported type") +) + +type Parser struct { + p int + s string + noLazy bool + loadOnce bool + skipValue bool + dbuf *byte +} + +/** Parser Private Methods **/ + +func (self *Parser) delim() types.ParsingError { + n := len(self.s) + p := self.lspace(self.p) + + /* check for EOF */ + if p >= n { + return types.ERR_EOF + } + + /* check for the delimtier */ + if self.s[p] != ':' { + return types.ERR_INVALID_CHAR + } + + /* update the read pointer */ + self.p = p + 1 + return 0 +} + +func (self *Parser) object() types.ParsingError { + n := len(self.s) + p := self.lspace(self.p) + + /* check for EOF */ + if p >= n { + return types.ERR_EOF + } + + /* check for the delimtier */ + if self.s[p] != '{' { + return types.ERR_INVALID_CHAR + } + + /* update the read pointer */ + self.p = p + 1 + return 0 +} + +func (self *Parser) array() types.ParsingError { + n := len(self.s) + p := self.lspace(self.p) + + /* check for EOF */ + if p >= n { + return types.ERR_EOF + } + + /* check for the delimtier */ + if self.s[p] != '[' { + return types.ERR_INVALID_CHAR + } + + /* update the read pointer */ + self.p = p + 1 + return 0 +} + +func (self *Parser) lspace(sp int) int { + ns := len(self.s) + for ; sp= 0 && isSpace(self.s[self.p]); self.p-=1 {} +} + +func (self *Parser) decodeArray(ret *linkedNodes) (Node, types.ParsingError) { + sp := self.p + ns := len(self.s) + + /* check for EOF */ + if self.p = self.lspace(sp); self.p >= ns { + return Node{}, types.ERR_EOF + } + + /* check for empty array */ + if self.s[self.p] == ']' { + self.p++ + return Node{t: types.V_ARRAY}, 0 + } + + /* allocate array space and parse every element */ + for { + var val Node + var err types.ParsingError + + if self.skipValue { + /* skip the value */ + var start int + if start, err = self.skipFast(); err != 0 { + return Node{}, err + } + if self.p > ns { + return Node{}, types.ERR_EOF + } + t := switchRawType(self.s[start]) + if t == _V_NONE { + return Node{}, types.ERR_INVALID_CHAR + } + val = newRawNode(self.s[start:self.p], t, false) + }else{ + /* decode the value */ + if val, err = self.Parse(); err != 0 { + return Node{}, err + } + } + + /* add the value to result */ + ret.Push(val) + self.p = self.lspace(self.p) + + /* check for EOF */ + if self.p >= ns { + return Node{}, types.ERR_EOF + } + + /* check for the next character */ + switch self.s[self.p] { + case ',' : self.p++ + case ']' : self.p++; return newArray(ret), 0 + default: + // if val.isLazy() { + // return newLazyArray(self, ret), 0 + // } + return Node{}, types.ERR_INVALID_CHAR + } + } +} + +func (self *Parser) decodeObject(ret *linkedPairs) (Node, types.ParsingError) { + sp := self.p + ns := len(self.s) + + /* check for EOF */ + if self.p = self.lspace(sp); self.p >= ns { + return Node{}, types.ERR_EOF + } + + /* check for empty object */ + if self.s[self.p] == '}' { + self.p++ + return Node{t: types.V_OBJECT}, 0 + } + + /* decode each pair */ + for { + var val Node + var njs types.JsonState + var err types.ParsingError + + /* decode the key */ + if njs = self.decodeValue(); njs.Vt != types.V_STRING { + return Node{}, types.ERR_INVALID_CHAR + } + + /* extract the key */ + idx := self.p - 1 + key := self.s[njs.Iv:idx] + + /* check for escape sequence */ + if njs.Ep != -1 { + if key, err = unquote(key); err != 0 { + return Node{}, err + } + } + + /* expect a ':' delimiter */ + if err = self.delim(); err != 0 { + return Node{}, err + } + + + if self.skipValue { + /* skip the value */ + var start int + if start, err = self.skipFast(); err != 0 { + return Node{}, err + } + if self.p > ns { + return Node{}, types.ERR_EOF + } + t := switchRawType(self.s[start]) + if t == _V_NONE { + return Node{}, types.ERR_INVALID_CHAR + } + val = newRawNode(self.s[start:self.p], t, false) + } else { + /* decode the value */ + if val, err = self.Parse(); err != 0 { + return Node{}, err + } + } + + /* add the value to result */ + // FIXME: ret's address may change here, thus previous referred node in ret may be invalid !! + ret.Push(NewPair(key, val)) + self.p = self.lspace(self.p) + + /* check for EOF */ + if self.p >= ns { + return Node{}, types.ERR_EOF + } + + /* check for the next character */ + switch self.s[self.p] { + case ',' : self.p++ + case '}' : self.p++; return newObject(ret), 0 + default: + // if val.isLazy() { + // return newLazyObject(self, ret), 0 + // } + return Node{}, types.ERR_INVALID_CHAR + } + } +} + +func (self *Parser) decodeString(iv int64, ep int) (Node, types.ParsingError) { + p := self.p - 1 + s := self.s[iv:p] + + /* fast path: no escape sequence */ + if ep == -1 { + return NewString(s), 0 + } + + /* unquote the string */ + out, err := unquote(s) + + /* check for errors */ + if err != 0 { + return Node{}, err + } else { + return newBytes(rt.Str2Mem(out)), 0 + } +} + +/** Parser Interface **/ + +func (self *Parser) Pos() int { + return self.p +} + + +// Parse returns a ast.Node representing the parser's JSON. +// NOTICE: the specific parsing lazy dependens parser's option +// It only parse first layer and first child for Object or Array be default +func (self *Parser) Parse() (Node, types.ParsingError) { + switch val := self.decodeValue(); val.Vt { + case types.V_EOF : return Node{}, types.ERR_EOF + case types.V_NULL : return nullNode, 0 + case types.V_TRUE : return trueNode, 0 + case types.V_FALSE : return falseNode, 0 + case types.V_STRING : return self.decodeString(val.Iv, val.Ep) + case types.V_ARRAY: + s := self.p - 1; + if p := skipBlank(self.s, self.p); p >= self.p && self.s[p] == ']' { + self.p = p + 1 + return Node{t: types.V_ARRAY}, 0 + } + if self.noLazy { + if self.loadOnce { + self.noLazy = false + } + return self.decodeArray(new(linkedNodes)) + } + // NOTICE: loadOnce always keep raw json for object or array + if self.loadOnce { + self.p = s + s, e := self.skipFast() + if e != 0 { + return Node{}, e + } + return newRawNode(self.s[s:self.p], types.V_ARRAY, true), 0 + } + return newLazyArray(self), 0 + case types.V_OBJECT: + s := self.p - 1; + if p := skipBlank(self.s, self.p); p >= self.p && self.s[p] == '}' { + self.p = p + 1 + return Node{t: types.V_OBJECT}, 0 + } + // NOTICE: loadOnce always keep raw json for object or array + if self.noLazy { + if self.loadOnce { + self.noLazy = false + } + return self.decodeObject(new(linkedPairs)) + } + if self.loadOnce { + self.p = s + s, e := self.skipFast() + if e != 0 { + return Node{}, e + } + return newRawNode(self.s[s:self.p], types.V_OBJECT, true), 0 + } + return newLazyObject(self), 0 + case types.V_DOUBLE : return NewNumber(self.s[val.Ep:self.p]), 0 + case types.V_INTEGER : return NewNumber(self.s[val.Ep:self.p]), 0 + default : return Node{}, types.ParsingError(-val.Vt) + } +} + +func (self *Parser) searchKey(match string) types.ParsingError { + ns := len(self.s) + if err := self.object(); err != 0 { + return err + } + + /* check for EOF */ + if self.p = self.lspace(self.p); self.p >= ns { + return types.ERR_EOF + } + + /* check for empty object */ + if self.s[self.p] == '}' { + self.p++ + return _ERR_NOT_FOUND + } + + var njs types.JsonState + var err types.ParsingError + /* decode each pair */ + for { + + /* decode the key */ + if njs = self.decodeValue(); njs.Vt != types.V_STRING { + return types.ERR_INVALID_CHAR + } + + /* extract the key */ + idx := self.p - 1 + key := self.s[njs.Iv:idx] + + /* check for escape sequence */ + if njs.Ep != -1 { + if key, err = unquote(key); err != 0 { + return err + } + } + + /* expect a ':' delimiter */ + if err = self.delim(); err != 0 { + return err + } + + /* skip value */ + if key != match { + if _, err = self.skipFast(); err != 0 { + return err + } + } else { + return 0 + } + + /* check for EOF */ + self.p = self.lspace(self.p) + if self.p >= ns { + return types.ERR_EOF + } + + /* check for the next character */ + switch self.s[self.p] { + case ',': + self.p++ + case '}': + self.p++ + return _ERR_NOT_FOUND + default: + return types.ERR_INVALID_CHAR + } + } +} + +func (self *Parser) searchIndex(idx int) types.ParsingError { + ns := len(self.s) + if err := self.array(); err != 0 { + return err + } + + /* check for EOF */ + if self.p = self.lspace(self.p); self.p >= ns { + return types.ERR_EOF + } + + /* check for empty array */ + if self.s[self.p] == ']' { + self.p++ + return _ERR_NOT_FOUND + } + + var err types.ParsingError + /* allocate array space and parse every element */ + for i := 0; i < idx; i++ { + + /* decode the value */ + if _, err = self.skipFast(); err != 0 { + return err + } + + /* check for EOF */ + self.p = self.lspace(self.p) + if self.p >= ns { + return types.ERR_EOF + } + + /* check for the next character */ + switch self.s[self.p] { + case ',': + self.p++ + case ']': + self.p++ + return _ERR_NOT_FOUND + default: + return types.ERR_INVALID_CHAR + } + } + + return 0 +} + +func (self *Node) skipNextNode() *Node { + if !self.isLazy() { + return nil + } + + parser, stack := self.getParserAndArrayStack() + ret := &stack.v + sp := parser.p + ns := len(parser.s) + + /* check for EOF */ + if parser.p = parser.lspace(sp); parser.p >= ns { + return newSyntaxError(parser.syntaxError(types.ERR_EOF)) + } + + /* check for empty array */ + if parser.s[parser.p] == ']' { + parser.p++ + self.setArray(ret) + return nil + } + + var val Node + /* skip the value */ + if start, err := parser.skipFast(); err != 0 { + return newSyntaxError(parser.syntaxError(err)) + } else { + t := switchRawType(parser.s[start]) + if t == _V_NONE { + return newSyntaxError(parser.syntaxError(types.ERR_INVALID_CHAR)) + } + val = newRawNode(parser.s[start:parser.p], t, false) + } + + /* add the value to result */ + ret.Push(val) + self.l++ + parser.p = parser.lspace(parser.p) + + /* check for EOF */ + if parser.p >= ns { + return newSyntaxError(parser.syntaxError(types.ERR_EOF)) + } + + /* check for the next character */ + switch parser.s[parser.p] { + case ',': + parser.p++ + return ret.At(ret.Len()-1) + case ']': + parser.p++ + self.setArray(ret) + return ret.At(ret.Len()-1) + default: + return newSyntaxError(parser.syntaxError(types.ERR_INVALID_CHAR)) + } +} + +func (self *Node) skipNextPair() (*Pair) { + if !self.isLazy() { + return nil + } + + parser, stack := self.getParserAndObjectStack() + ret := &stack.v + sp := parser.p + ns := len(parser.s) + + /* check for EOF */ + if parser.p = parser.lspace(sp); parser.p >= ns { + return newErrorPair(parser.syntaxError(types.ERR_EOF)) + } + + /* check for empty object */ + if parser.s[parser.p] == '}' { + parser.p++ + self.setObject(ret) + return nil + } + + /* decode one pair */ + var val Node + var njs types.JsonState + var err types.ParsingError + + /* decode the key */ + if njs = parser.decodeValue(); njs.Vt != types.V_STRING { + return newErrorPair(parser.syntaxError(types.ERR_INVALID_CHAR)) + } + + /* extract the key */ + idx := parser.p - 1 + key := parser.s[njs.Iv:idx] + + /* check for escape sequence */ + if njs.Ep != -1 { + if key, err = unquote(key); err != 0 { + return newErrorPair(parser.syntaxError(err)) + } + } + + /* expect a ':' delimiter */ + if err = parser.delim(); err != 0 { + return newErrorPair(parser.syntaxError(err)) + } + + /* skip the value */ + if start, err := parser.skipFast(); err != 0 { + return newErrorPair(parser.syntaxError(err)) + } else { + t := switchRawType(parser.s[start]) + if t == _V_NONE { + return newErrorPair(parser.syntaxError(types.ERR_INVALID_CHAR)) + } + val = newRawNode(parser.s[start:parser.p], t, false) + } + + /* add the value to result */ + ret.Push(NewPair(key, val)) + self.l++ + parser.p = parser.lspace(parser.p) + + /* check for EOF */ + if parser.p >= ns { + return newErrorPair(parser.syntaxError(types.ERR_EOF)) + } + + /* check for the next character */ + switch parser.s[parser.p] { + case ',': + parser.p++ + return ret.At(ret.Len()-1) + case '}': + parser.p++ + self.setObject(ret) + return ret.At(ret.Len()-1) + default: + return newErrorPair(parser.syntaxError(types.ERR_INVALID_CHAR)) + } +} + + +/** Parser Factory **/ + +// Loads parse all json into interface{} +func Loads(src string) (int, interface{}, error) { + ps := &Parser{s: src} + np, err := ps.Parse() + + /* check for errors */ + if err != 0 { + return 0, nil, ps.ExportError(err) + } else { + x, err := np.Interface() + if err != nil { + return 0, nil, err + } + return ps.Pos(), x, nil + } +} + +// LoadsUseNumber parse all json into interface{}, with numeric nodes casted to json.Number +func LoadsUseNumber(src string) (int, interface{}, error) { + ps := &Parser{s: src} + np, err := ps.Parse() + + /* check for errors */ + if err != 0 { + return 0, nil, err + } else { + x, err := np.InterfaceUseNumber() + if err != nil { + return 0, nil, err + } + return ps.Pos(), x, nil + } +} + +// NewParser returns pointer of new allocated parser +func NewParser(src string) *Parser { + return &Parser{s: src} +} + +// NewParser returns new allocated parser +func NewParserObj(src string) Parser { + return Parser{s: src} +} + +// decodeNumber controls if parser decodes the number values instead of skip them +// WARN: once you set decodeNumber(true), please set decodeNumber(false) before you drop the parser +// otherwise the memory CANNOT be reused +func (self *Parser) decodeNumber(decode bool) { + if !decode && self.dbuf != nil { + types.FreeDbuf(self.dbuf) + self.dbuf = nil + return + } + if decode && self.dbuf == nil { + self.dbuf = types.NewDbuf() + } +} + +// ExportError converts types.ParsingError to std Error +func (self *Parser) ExportError(err types.ParsingError) error { + if err == _ERR_NOT_FOUND { + return ErrNotExist + } + return fmt.Errorf("%q", SyntaxError{ + Pos : self.p, + Src : self.s, + Code: err, + }.Description()) +} + +func backward(src string, i int) int { + for ; i>=0 && isSpace(src[i]); i-- {} + return i +} + + +func newRawNode(str string, typ types.ValueType, lock bool) Node { + ret := Node{ + t: typ | _V_RAW, + p: rt.StrPtr(str), + l: uint(len(str)), + } + if lock { + ret.m = new(sync.RWMutex) + } + return ret +} + +var typeJumpTable = [256]types.ValueType{ + '"' : types.V_STRING, + '-' : _V_NUMBER, + '0' : _V_NUMBER, + '1' : _V_NUMBER, + '2' : _V_NUMBER, + '3' : _V_NUMBER, + '4' : _V_NUMBER, + '5' : _V_NUMBER, + '6' : _V_NUMBER, + '7' : _V_NUMBER, + '8' : _V_NUMBER, + '9' : _V_NUMBER, + '[' : types.V_ARRAY, + 'f' : types.V_FALSE, + 'n' : types.V_NULL, + 't' : types.V_TRUE, + '{' : types.V_OBJECT, +} + +func switchRawType(c byte) types.ValueType { + return typeJumpTable[c] +} + +func (self *Node) loadt() types.ValueType { + return (types.ValueType)(atomic.LoadInt64(&self.t)) +} + +func (self *Node) lock() bool { + if m := self.m; m != nil { + m.Lock() + return true + } + return false +} + +func (self *Node) unlock() { + if m := self.m; m != nil { + m.Unlock() + } +} + +func (self *Node) rlock() bool { + if m := self.m; m != nil { + m.RLock() + return true + } + return false +} + +func (self *Node) runlock() { + if m := self.m; m != nil { + m.RUnlock() + } +} diff --git a/vendor/github.com/bytedance/sonic/ast/search.go b/vendor/github.com/bytedance/sonic/ast/search.go new file mode 100644 index 00000000000..9a5fb942036 --- /dev/null +++ b/vendor/github.com/bytedance/sonic/ast/search.go @@ -0,0 +1,157 @@ +/* + * Copyright 2021 ByteDance Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ast + +import ( + `github.com/bytedance/sonic/internal/rt` + `github.com/bytedance/sonic/internal/native/types` +) + +// SearchOptions controls Searcher's behavior +type SearchOptions struct { + // ValidateJSON indicates the searcher to validate the entire JSON + ValidateJSON bool + + // CopyReturn indicates the searcher to copy the result JSON instead of refer from the input + // This can help to reduce memory usage if you cache the results + CopyReturn bool + + // ConcurrentRead indicates the searcher to return a concurrently-READ-safe node, + // including: GetByPath/Get/Index/GetOrIndex/Int64/Bool/Float64/String/Number/Interface/Array/Map/Raw/MarshalJSON + ConcurrentRead bool +} + +type Searcher struct { + parser Parser + SearchOptions +} + +func NewSearcher(str string) *Searcher { + return &Searcher{ + parser: Parser{ + s: str, + noLazy: false, + }, + SearchOptions: SearchOptions{ + ValidateJSON: true, + }, + } +} + +// GetByPathCopy search in depth from top json and returns a **Copied** json node at the path location +func (self *Searcher) GetByPathCopy(path ...interface{}) (Node, error) { + self.CopyReturn = true + return self.getByPath(path...) +} + +// GetByPathNoCopy search in depth from top json and returns a **Referenced** json node at the path location +// +// WARN: this search directly refer partial json from top json, which has faster speed, +// may consumes more memory. +func (self *Searcher) GetByPath(path ...interface{}) (Node, error) { + return self.getByPath(path...) +} + +func (self *Searcher) getByPath(path ...interface{}) (Node, error) { + var err types.ParsingError + var start int + + self.parser.p = 0 + start, err = self.parser.getByPath(self.ValidateJSON, path...) + if err != 0 { + // for compatibility with old version + if err == types.ERR_NOT_FOUND { + return Node{}, ErrNotExist + } + if err == types.ERR_UNSUPPORT_TYPE { + panic("path must be either int(>=0) or string") + } + return Node{}, self.parser.syntaxError(err) + } + + t := switchRawType(self.parser.s[start]) + if t == _V_NONE { + return Node{}, self.parser.ExportError(err) + } + + // copy string to reducing memory usage + var raw string + if self.CopyReturn { + raw = rt.Mem2Str([]byte(self.parser.s[start:self.parser.p])) + } else { + raw = self.parser.s[start:self.parser.p] + } + return newRawNode(raw, t, self.ConcurrentRead), nil +} + +// GetByPath searches a path and returns relaction and types of target +func _GetByPath(src string, path ...interface{}) (start int, end int, typ int, err error) { + p := NewParserObj(src) + s, e := p.getByPath(false, path...) + if e != 0 { + // for compatibility with old version + if e == types.ERR_NOT_FOUND { + return -1, -1, 0, ErrNotExist + } + if e == types.ERR_UNSUPPORT_TYPE { + panic("path must be either int(>=0) or string") + } + return -1, -1, 0, p.syntaxError(e) + } + + t := switchRawType(p.s[s]) + if t == _V_NONE { + return -1, -1, 0, ErrNotExist + } + if t == _V_NUMBER { + p.p = 1 + backward(p.s, p.p-1) + } + return s, p.p, int(t), nil +} + +// ValidSyntax check if a json has a valid JSON syntax, +// while not validate UTF-8 charset +func _ValidSyntax(json string) bool { + p := NewParserObj(json) + _, e := p.skip() + if e != 0 { + return false + } + if skipBlank(p.s, p.p) != -int(types.ERR_EOF) { + return false + } + return true +} + +// SkipFast skip a json value in fast-skip algs, +// while not strictly validate JSON syntax and UTF-8 charset. +func _SkipFast(src string, i int) (int, int, error) { + p := NewParserObj(src) + p.p = i + s, e := p.skipFast() + if e != 0 { + return -1, -1, p.ExportError(e) + } + t := switchRawType(p.s[s]) + if t == _V_NONE { + return -1, -1, ErrNotExist + } + if t == _V_NUMBER { + p.p = 1 + backward(p.s, p.p-1) + } + return s, p.p, nil +} diff --git a/vendor/github.com/bytedance/sonic/ast/stubs.go b/vendor/github.com/bytedance/sonic/ast/stubs.go new file mode 100644 index 00000000000..53bf3b8aa06 --- /dev/null +++ b/vendor/github.com/bytedance/sonic/ast/stubs.go @@ -0,0 +1,142 @@ +/* + * Copyright 2021 ByteDance Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ast + +import ( + "unicode/utf8" + "unsafe" + + "github.com/bytedance/sonic/internal/rt" +) + +//go:noescape +//go:linkname memmove runtime.memmove +//goland:noinspection GoUnusedParameter +func memmove(to unsafe.Pointer, from unsafe.Pointer, n uintptr) + +//go:linkname unsafe_NewArray reflect.unsafe_NewArray +//goland:noinspection GoUnusedParameter +func unsafe_NewArray(typ *rt.GoType, n int) unsafe.Pointer + +//go:nosplit +func mem2ptr(s []byte) unsafe.Pointer { + return (*rt.GoSlice)(unsafe.Pointer(&s)).Ptr +} + +var safeSet = [utf8.RuneSelf]bool{ + ' ': true, + '!': true, + '"': false, + '#': true, + '$': true, + '%': true, + '&': true, + '\'': true, + '(': true, + ')': true, + '*': true, + '+': true, + ',': true, + '-': true, + '.': true, + '/': true, + '0': true, + '1': true, + '2': true, + '3': true, + '4': true, + '5': true, + '6': true, + '7': true, + '8': true, + '9': true, + ':': true, + ';': true, + '<': true, + '=': true, + '>': true, + '?': true, + '@': true, + 'A': true, + 'B': true, + 'C': true, + 'D': true, + 'E': true, + 'F': true, + 'G': true, + 'H': true, + 'I': true, + 'J': true, + 'K': true, + 'L': true, + 'M': true, + 'N': true, + 'O': true, + 'P': true, + 'Q': true, + 'R': true, + 'S': true, + 'T': true, + 'U': true, + 'V': true, + 'W': true, + 'X': true, + 'Y': true, + 'Z': true, + '[': true, + '\\': false, + ']': true, + '^': true, + '_': true, + '`': true, + 'a': true, + 'b': true, + 'c': true, + 'd': true, + 'e': true, + 'f': true, + 'g': true, + 'h': true, + 'i': true, + 'j': true, + 'k': true, + 'l': true, + 'm': true, + 'n': true, + 'o': true, + 'p': true, + 'q': true, + 'r': true, + 's': true, + 't': true, + 'u': true, + 'v': true, + 'w': true, + 'x': true, + 'y': true, + 'z': true, + '{': true, + '|': true, + '}': true, + '~': true, + '\u007f': true, +} + +var hex = "0123456789abcdef" + +//go:linkname unquoteBytes encoding/json.unquoteBytes +func unquoteBytes(s []byte) (t []byte, ok bool) diff --git a/vendor/github.com/bytedance/sonic/ast/visitor.go b/vendor/github.com/bytedance/sonic/ast/visitor.go new file mode 100644 index 00000000000..dc04785133b --- /dev/null +++ b/vendor/github.com/bytedance/sonic/ast/visitor.go @@ -0,0 +1,332 @@ +/* + * Copyright 2021 ByteDance Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ast + +import ( + `encoding/json` + `errors` + + `github.com/bytedance/sonic/internal/native/types` +) + +// Visitor handles the callbacks during preorder traversal of a JSON AST. +// +// According to the JSON RFC8259, a JSON AST can be defined by +// the following rules without separator / whitespace tokens. +// +// JSON-AST = value +// value = false / null / true / object / array / number / string +// object = begin-object [ member *( member ) ] end-object +// member = string value +// array = begin-array [ value *( value ) ] end-array +// +type Visitor interface { + + // OnNull handles a JSON null value. + OnNull() error + + // OnBool handles a JSON true / false value. + OnBool(v bool) error + + // OnString handles a JSON string value. + OnString(v string) error + + // OnInt64 handles a JSON number value with int64 type. + OnInt64(v int64, n json.Number) error + + // OnFloat64 handles a JSON number value with float64 type. + OnFloat64(v float64, n json.Number) error + + // OnObjectBegin handles the beginning of a JSON object value with a + // suggested capacity that can be used to make your custom object container. + // + // After this point the visitor will receive a sequence of callbacks like + // [string, value, string, value, ......, ObjectEnd]. + // + // Note: + // 1. This is a recursive definition which means the value can + // also be a JSON object / array described by a sequence of callbacks. + // 2. The suggested capacity will be 0 if current object is empty. + // 3. Currently sonic use a fixed capacity for non-empty object (keep in + // sync with ast.Node) which might not be very suitable. This may be + // improved in future version. + OnObjectBegin(capacity int) error + + // OnObjectKey handles a JSON object key string in member. + OnObjectKey(key string) error + + // OnObjectEnd handles the ending of a JSON object value. + OnObjectEnd() error + + // OnArrayBegin handles the beginning of a JSON array value with a + // suggested capacity that can be used to make your custom array container. + // + // After this point the visitor will receive a sequence of callbacks like + // [value, value, value, ......, ArrayEnd]. + // + // Note: + // 1. This is a recursive definition which means the value can + // also be a JSON object / array described by a sequence of callbacks. + // 2. The suggested capacity will be 0 if current array is empty. + // 3. Currently sonic use a fixed capacity for non-empty array (keep in + // sync with ast.Node) which might not be very suitable. This may be + // improved in future version. + OnArrayBegin(capacity int) error + + // OnArrayEnd handles the ending of a JSON array value. + OnArrayEnd() error +} + +// VisitorOptions contains all Visitor's options. The default value is an +// empty VisitorOptions{}. +type VisitorOptions struct { + // OnlyNumber indicates parser to directly return number value without + // conversion, then the first argument of OnInt64 / OnFloat64 will always + // be zero. + OnlyNumber bool +} + +var defaultVisitorOptions = &VisitorOptions{} + +// Preorder decodes the whole JSON string and callbacks each AST node to visitor +// during preorder traversal. Any visitor method with an error returned will +// break the traversal and the given error will be directly returned. The opts +// argument can be reused after every call. +func Preorder(str string, visitor Visitor, opts *VisitorOptions) error { + if opts == nil { + opts = defaultVisitorOptions + } + // process VisitorOptions first to guarantee that all options will be + // constant during decoding and make options more readable. + var ( + optDecodeNumber = !opts.OnlyNumber + ) + + tv := &traverser{ + parser: Parser{ + s: str, + noLazy: true, + skipValue: false, + }, + visitor: visitor, + } + + if optDecodeNumber { + tv.parser.decodeNumber(true) + } + + err := tv.decodeValue() + + if optDecodeNumber { + tv.parser.decodeNumber(false) + } + return err +} + +type traverser struct { + parser Parser + visitor Visitor +} + +// NOTE: keep in sync with (*Parser).Parse method. +func (self *traverser) decodeValue() error { + switch val := self.parser.decodeValue(); val.Vt { + case types.V_EOF: + return types.ERR_EOF + case types.V_NULL: + return self.visitor.OnNull() + case types.V_TRUE: + return self.visitor.OnBool(true) + case types.V_FALSE: + return self.visitor.OnBool(false) + case types.V_STRING: + return self.decodeString(val.Iv, val.Ep) + case types.V_DOUBLE: + return self.visitor.OnFloat64(val.Dv, + json.Number(self.parser.s[val.Ep:self.parser.p])) + case types.V_INTEGER: + return self.visitor.OnInt64(val.Iv, + json.Number(self.parser.s[val.Ep:self.parser.p])) + case types.V_ARRAY: + return self.decodeArray() + case types.V_OBJECT: + return self.decodeObject() + default: + return types.ParsingError(-val.Vt) + } +} + +// NOTE: keep in sync with (*Parser).decodeArray method. +func (self *traverser) decodeArray() error { + sp := self.parser.p + ns := len(self.parser.s) + + /* allocate array space and parse every element */ + if err := self.visitor.OnArrayBegin(_DEFAULT_NODE_CAP); err != nil { + if err == VisitOPSkip { + // NOTICE: for user needs to skip entiry object + self.parser.p -= 1 + if _, e := self.parser.skipFast(); e != 0 { + return e + } + return self.visitor.OnArrayEnd() + } + return err + } + + /* check for EOF */ + self.parser.p = self.parser.lspace(sp) + if self.parser.p >= ns { + return types.ERR_EOF + } + + /* check for empty array */ + if self.parser.s[self.parser.p] == ']' { + self.parser.p++ + return self.visitor.OnArrayEnd() + } + + for { + /* decode the value */ + if err := self.decodeValue(); err != nil { + return err + } + self.parser.p = self.parser.lspace(self.parser.p) + + /* check for EOF */ + if self.parser.p >= ns { + return types.ERR_EOF + } + + /* check for the next character */ + switch self.parser.s[self.parser.p] { + case ',': + self.parser.p++ + case ']': + self.parser.p++ + return self.visitor.OnArrayEnd() + default: + return types.ERR_INVALID_CHAR + } + } +} + +// NOTE: keep in sync with (*Parser).decodeObject method. +func (self *traverser) decodeObject() error { + sp := self.parser.p + ns := len(self.parser.s) + + /* allocate object space and decode each pair */ + if err := self.visitor.OnObjectBegin(_DEFAULT_NODE_CAP); err != nil { + if err == VisitOPSkip { + // NOTICE: for user needs to skip entiry object + self.parser.p -= 1 + if _, e := self.parser.skipFast(); e != 0 { + return e + } + return self.visitor.OnObjectEnd() + } + return err + } + + /* check for EOF */ + self.parser.p = self.parser.lspace(sp) + if self.parser.p >= ns { + return types.ERR_EOF + } + + /* check for empty object */ + if self.parser.s[self.parser.p] == '}' { + self.parser.p++ + return self.visitor.OnObjectEnd() + } + + for { + var njs types.JsonState + var err types.ParsingError + + /* decode the key */ + if njs = self.parser.decodeValue(); njs.Vt != types.V_STRING { + return types.ERR_INVALID_CHAR + } + + /* extract the key */ + idx := self.parser.p - 1 + key := self.parser.s[njs.Iv:idx] + + /* check for escape sequence */ + if njs.Ep != -1 { + if key, err = unquote(key); err != 0 { + return err + } + } + + if err := self.visitor.OnObjectKey(key); err != nil { + return err + } + + /* expect a ':' delimiter */ + if err = self.parser.delim(); err != 0 { + return err + } + + /* decode the value */ + if err := self.decodeValue(); err != nil { + return err + } + + self.parser.p = self.parser.lspace(self.parser.p) + + /* check for EOF */ + if self.parser.p >= ns { + return types.ERR_EOF + } + + /* check for the next character */ + switch self.parser.s[self.parser.p] { + case ',': + self.parser.p++ + case '}': + self.parser.p++ + return self.visitor.OnObjectEnd() + default: + return types.ERR_INVALID_CHAR + } + } +} + +// NOTE: keep in sync with (*Parser).decodeString method. +func (self *traverser) decodeString(iv int64, ep int) error { + p := self.parser.p - 1 + s := self.parser.s[iv:p] + + /* fast path: no escape sequence */ + if ep == -1 { + return self.visitor.OnString(s) + } + + /* unquote the string */ + out, err := unquote(s) + if err != 0 { + return err + } + return self.visitor.OnString(out) +} + +// If visitor return this error on `OnObjectBegin()` or `OnArrayBegin()`, +// the transverer will skip entiry object or array +var VisitOPSkip = errors.New("") diff --git a/vendor/github.com/bytedance/sonic/compat.go b/vendor/github.com/bytedance/sonic/compat.go new file mode 100644 index 00000000000..b32342a84aa --- /dev/null +++ b/vendor/github.com/bytedance/sonic/compat.go @@ -0,0 +1,133 @@ +// +build !amd64,!arm64 go1.24 !go1.17 arm64,!go1.20 + +/* + * Copyright 2021 ByteDance Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package sonic + +import ( + `bytes` + `encoding/json` + `io` + `reflect` + + `github.com/bytedance/sonic/option` +) + +const apiKind = UseStdJSON + +type frozenConfig struct { + Config +} + +// Froze convert the Config to API +func (cfg Config) Froze() API { + api := &frozenConfig{Config: cfg} + return api +} + +func (cfg frozenConfig) marshalOptions(val interface{}, prefix, indent string) ([]byte, error) { + w := bytes.NewBuffer([]byte{}) + enc := json.NewEncoder(w) + enc.SetEscapeHTML(cfg.EscapeHTML) + enc.SetIndent(prefix, indent) + err := enc.Encode(val) + out := w.Bytes() + + // json.Encoder always appends '\n' after encoding, + // which is not same with json.Marshal() + if len(out) > 0 && out[len(out)-1] == '\n' { + out = out[:len(out)-1] + } + return out, err +} + +// Marshal is implemented by sonic +func (cfg frozenConfig) Marshal(val interface{}) ([]byte, error) { + if !cfg.EscapeHTML { + return cfg.marshalOptions(val, "", "") + } + return json.Marshal(val) +} + +// MarshalToString is implemented by sonic +func (cfg frozenConfig) MarshalToString(val interface{}) (string, error) { + out, err := cfg.Marshal(val) + return string(out), err +} + +// MarshalIndent is implemented by sonic +func (cfg frozenConfig) MarshalIndent(val interface{}, prefix, indent string) ([]byte, error) { + if !cfg.EscapeHTML { + return cfg.marshalOptions(val, prefix, indent) + } + return json.MarshalIndent(val, prefix, indent) +} + +// UnmarshalFromString is implemented by sonic +func (cfg frozenConfig) UnmarshalFromString(buf string, val interface{}) error { + r := bytes.NewBufferString(buf) + dec := json.NewDecoder(r) + if cfg.UseNumber { + dec.UseNumber() + } + if cfg.DisallowUnknownFields { + dec.DisallowUnknownFields() + } + return dec.Decode(val) +} + +// Unmarshal is implemented by sonic +func (cfg frozenConfig) Unmarshal(buf []byte, val interface{}) error { + return cfg.UnmarshalFromString(string(buf), val) +} + +// NewEncoder is implemented by sonic +func (cfg frozenConfig) NewEncoder(writer io.Writer) Encoder { + enc := json.NewEncoder(writer) + if !cfg.EscapeHTML { + enc.SetEscapeHTML(cfg.EscapeHTML) + } + return enc +} + +// NewDecoder is implemented by sonic +func (cfg frozenConfig) NewDecoder(reader io.Reader) Decoder { + dec := json.NewDecoder(reader) + if cfg.UseNumber { + dec.UseNumber() + } + if cfg.DisallowUnknownFields { + dec.DisallowUnknownFields() + } + return dec +} + +// Valid is implemented by sonic +func (cfg frozenConfig) Valid(data []byte) bool { + return json.Valid(data) +} + +// Pretouch compiles vt ahead-of-time to avoid JIT compilation on-the-fly, in +// order to reduce the first-hit latency at **amd64** Arch. +// Opts are the compile options, for example, "option.WithCompileRecursiveDepth" is +// a compile option to set the depth of recursive compile for the nested struct type. +// * This is the none implement for !amd64. +// It will be useful for someone who develop with !amd64 arch,like Mac M1. +func Pretouch(vt reflect.Type, opts ...option.CompileOption) error { + return nil +} + diff --git a/vendor/github.com/bytedance/sonic/decoder/decoder_compat.go b/vendor/github.com/bytedance/sonic/decoder/decoder_compat.go new file mode 100644 index 00000000000..408d593946a --- /dev/null +++ b/vendor/github.com/bytedance/sonic/decoder/decoder_compat.go @@ -0,0 +1,200 @@ +//go:build (!amd64 && !arm64) || go1.24 || !go1.17 || (arm64 && !go1.20) +// +build !amd64,!arm64 go1.24 !go1.17 arm64,!go1.20 + +/* +* Copyright 2023 ByteDance Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. + */ + +package decoder + +import ( + "bytes" + "encoding/json" + "io" + "reflect" + "unsafe" + + "github.com/bytedance/sonic/internal/decoder/consts" + "github.com/bytedance/sonic/internal/native/types" + "github.com/bytedance/sonic/option" +) + +func init() { + println("WARNING: sonic/decoder only supports (Go1.17~1.23 && CPU amd64) or (go1.20~1.23 && CPU arm64), but your environment is not suitable") +} + +const ( + _F_use_int64 = consts.F_use_int64 + _F_disable_urc = consts.F_disable_unknown + _F_disable_unknown = consts.F_disable_unknown + _F_copy_string = consts.F_copy_string + + _F_use_number = consts.F_use_number + _F_validate_string = consts.F_validate_string + _F_allow_control = consts.F_allow_control + _F_no_validate_json = consts.F_no_validate_json + _F_case_sensitive = consts.F_case_sensitive +) + +type Options uint64 + +const ( + OptionUseInt64 Options = 1 << _F_use_int64 + OptionUseNumber Options = 1 << _F_use_number + OptionUseUnicodeErrors Options = 1 << _F_disable_urc + OptionDisableUnknown Options = 1 << _F_disable_unknown + OptionCopyString Options = 1 << _F_copy_string + OptionValidateString Options = 1 << _F_validate_string + OptionNoValidateJSON Options = 1 << _F_no_validate_json + OptionCaseSensitive Options = 1 << _F_case_sensitive +) + +func (self *Decoder) SetOptions(opts Options) { + if (opts & OptionUseNumber != 0) && (opts & OptionUseInt64 != 0) { + panic("can't set OptionUseInt64 and OptionUseNumber both!") + } + self.f = uint64(opts) +} + + +// Decoder is the decoder context object +type Decoder struct { + i int + f uint64 + s string +} + +// NewDecoder creates a new decoder instance. +func NewDecoder(s string) *Decoder { + return &Decoder{s: s} +} + +// Pos returns the current decoding position. +func (self *Decoder) Pos() int { + return self.i +} + +func (self *Decoder) Reset(s string) { + self.s = s + self.i = 0 + // self.f = 0 +} + +// NOTE: api fallback do nothing +func (self *Decoder) CheckTrailings() error { + pos := self.i + buf := self.s + /* skip all the trailing spaces */ + if pos != len(buf) { + for pos < len(buf) && (types.SPACE_MASK & (1 << buf[pos])) != 0 { + pos++ + } + } + + /* then it must be at EOF */ + if pos == len(buf) { + return nil + } + + /* junk after JSON value */ + return nil +} + + +// Decode parses the JSON-encoded data from current position and stores the result +// in the value pointed to by val. +func (self *Decoder) Decode(val interface{}) error { + r := bytes.NewBufferString(self.s) + dec := json.NewDecoder(r) + if (self.f & uint64(OptionUseNumber)) != 0 { + dec.UseNumber() + } + if (self.f & uint64(OptionDisableUnknown)) != 0 { + dec.DisallowUnknownFields() + } + return dec.Decode(val) +} + +// UseInt64 indicates the Decoder to unmarshal an integer into an interface{} as an +// int64 instead of as a float64. +func (self *Decoder) UseInt64() { + self.f |= 1 << _F_use_int64 + self.f &^= 1 << _F_use_number +} + +// UseNumber indicates the Decoder to unmarshal a number into an interface{} as a +// json.Number instead of as a float64. +func (self *Decoder) UseNumber() { + self.f &^= 1 << _F_use_int64 + self.f |= 1 << _F_use_number +} + +// UseUnicodeErrors indicates the Decoder to return an error when encounter invalid +// UTF-8 escape sequences. +func (self *Decoder) UseUnicodeErrors() { + self.f |= 1 << _F_disable_urc +} + +// DisallowUnknownFields indicates the Decoder to return an error when the destination +// is a struct and the input contains object keys which do not match any +// non-ignored, exported fields in the destination. +func (self *Decoder) DisallowUnknownFields() { + self.f |= 1 << _F_disable_unknown +} + +// CopyString indicates the Decoder to decode string values by copying instead of referring. +func (self *Decoder) CopyString() { + self.f |= 1 << _F_copy_string +} + +// ValidateString causes the Decoder to validate string values when decoding string value +// in JSON. Validation is that, returning error when unescaped control chars(0x00-0x1f) or +// invalid UTF-8 chars in the string value of JSON. +func (self *Decoder) ValidateString() { + self.f |= 1 << _F_validate_string +} + +// Pretouch compiles vt ahead-of-time to avoid JIT compilation on-the-fly, in +// order to reduce the first-hit latency. +// +// Opts are the compile options, for example, "option.WithCompileRecursiveDepth" is +// a compile option to set the depth of recursive compile for the nested struct type. +func Pretouch(vt reflect.Type, opts ...option.CompileOption) error { + return nil +} + +type StreamDecoder = json.Decoder + +// NewStreamDecoder adapts to encoding/json.NewDecoder API. +// +// NewStreamDecoder returns a new decoder that reads from r. +func NewStreamDecoder(r io.Reader) *StreamDecoder { + return json.NewDecoder(r) +} + +// SyntaxError represents json syntax error +type SyntaxError json.SyntaxError + +// Description +func (s SyntaxError) Description() string { + return (*json.SyntaxError)(unsafe.Pointer(&s)).Error() +} +// Error +func (s SyntaxError) Error() string { + return (*json.SyntaxError)(unsafe.Pointer(&s)).Error() +} + +// MismatchTypeError represents mismatching between json and object +type MismatchTypeError json.UnmarshalTypeError diff --git a/vendor/github.com/bytedance/sonic/decoder/decoder_native.go b/vendor/github.com/bytedance/sonic/decoder/decoder_native.go new file mode 100644 index 00000000000..bf71e1bd417 --- /dev/null +++ b/vendor/github.com/bytedance/sonic/decoder/decoder_native.go @@ -0,0 +1,72 @@ +//go:build (amd64 && go1.17 && !go1.24) || (arm64 && go1.20 && !go1.24) +// +build amd64,go1.17,!go1.24 arm64,go1.20,!go1.24 + + +/* +* Copyright 2023 ByteDance Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +package decoder + +import ( + `github.com/bytedance/sonic/internal/decoder/api` +) + +// Decoder is the decoder context object +type Decoder = api.Decoder + +// SyntaxError represents json syntax error +type SyntaxError = api.SyntaxError + +// MismatchTypeError represents mismatching between json and object +type MismatchTypeError = api.MismatchTypeError + +// Options for decode. +type Options = api.Options + +const ( + OptionUseInt64 Options = api.OptionUseInt64 + OptionUseNumber Options = api.OptionUseNumber + OptionUseUnicodeErrors Options = api.OptionUseUnicodeErrors + OptionDisableUnknown Options = api.OptionDisableUnknown + OptionCopyString Options = api.OptionCopyString + OptionValidateString Options = api.OptionValidateString + OptionNoValidateJSON Options = api.OptionNoValidateJSON + OptionCaseSensitive Options = api.OptionCaseSensitive +) + +// StreamDecoder is the decoder context object for streaming input. +type StreamDecoder = api.StreamDecoder + +var ( + // NewDecoder creates a new decoder instance. + NewDecoder = api.NewDecoder + + // NewStreamDecoder adapts to encoding/json.NewDecoder API. + // + // NewStreamDecoder returns a new decoder that reads from r. + NewStreamDecoder = api.NewStreamDecoder + + // Pretouch compiles vt ahead-of-time to avoid JIT compilation on-the-fly, in + // order to reduce the first-hit latency. + // + // Opts are the compile options, for example, "option.WithCompileRecursiveDepth" is + // a compile option to set the depth of recursive compile for the nested struct type. + Pretouch = api.Pretouch + + // Skip skips only one json value, and returns first non-blank character position and its ending position if it is valid. + // Otherwise, returns negative error code using start and invalid character position using end + Skip = api.Skip +) diff --git a/vendor/github.com/bytedance/sonic/encoder/encoder_compat.go b/vendor/github.com/bytedance/sonic/encoder/encoder_compat.go new file mode 100644 index 00000000000..254defa2052 --- /dev/null +++ b/vendor/github.com/bytedance/sonic/encoder/encoder_compat.go @@ -0,0 +1,261 @@ +// +build !amd64,!arm64 go1.24 !go1.17 arm64,!go1.20 + +/* +* Copyright 2023 ByteDance Inc. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +package encoder + +import ( + `io` + `bytes` + `encoding/json` + `reflect` + + `github.com/bytedance/sonic/option` +) + +func init() { + println("WARNING:(encoder) sonic only supports (Go1.17~1.23 && CPU amd64) or (G01.20~1.23 && CPU arm64) , but your environment is not suitable") +} + +// EnableFallback indicates if encoder use fallback +const EnableFallback = true + +// Options is a set of encoding options. +type Options uint64 + +const ( + bitSortMapKeys = iota + bitEscapeHTML + bitCompactMarshaler + bitNoQuoteTextMarshaler + bitNoNullSliceOrMap + bitValidateString + bitNoValidateJSONMarshaler + bitNoEncoderNewline + + // used for recursive compile + bitPointerValue = 63 +) + +const ( + // SortMapKeys indicates that the keys of a map needs to be sorted + // before serializing into JSON. + // WARNING: This hurts performance A LOT, USE WITH CARE. + SortMapKeys Options = 1 << bitSortMapKeys + + // EscapeHTML indicates encoder to escape all HTML characters + // after serializing into JSON (see https://pkg.go.dev/encoding/json#HTMLEscape). + // WARNING: This hurts performance A LOT, USE WITH CARE. + EscapeHTML Options = 1 << bitEscapeHTML + + // CompactMarshaler indicates that the output JSON from json.Marshaler + // is always compact and needs no validation + CompactMarshaler Options = 1 << bitCompactMarshaler + + // NoQuoteTextMarshaler indicates that the output text from encoding.TextMarshaler + // is always escaped string and needs no quoting + NoQuoteTextMarshaler Options = 1 << bitNoQuoteTextMarshaler + + // NoNullSliceOrMap indicates all empty Array or Object are encoded as '[]' or '{}', + // instead of 'null' + NoNullSliceOrMap Options = 1 << bitNoNullSliceOrMap + + // ValidateString indicates that encoder should validate the input string + // before encoding it into JSON. + ValidateString Options = 1 << bitValidateString + + // NoValidateJSONMarshaler indicates that the encoder should not validate the output string + // after encoding the JSONMarshaler to JSON. + NoValidateJSONMarshaler Options = 1 << bitNoValidateJSONMarshaler + + // NoEncoderNewline indicates that the encoder should not add a newline after every message + NoEncoderNewline Options = 1 << bitNoEncoderNewline + + // CompatibleWithStd is used to be compatible with std encoder. + CompatibleWithStd Options = SortMapKeys | EscapeHTML | CompactMarshaler +) + +// Encoder represents a specific set of encoder configurations. +type Encoder struct { + Opts Options + prefix string + indent string +} + +// Encode returns the JSON encoding of v. +func (self *Encoder) Encode(v interface{}) ([]byte, error) { + if self.indent != "" || self.prefix != "" { + return EncodeIndented(v, self.prefix, self.indent, self.Opts) + } + return Encode(v, self.Opts) +} + +// SortKeys enables the SortMapKeys option. +func (self *Encoder) SortKeys() *Encoder { + self.Opts |= SortMapKeys + return self +} + +// SetEscapeHTML specifies if option EscapeHTML opens +func (self *Encoder) SetEscapeHTML(f bool) { + if f { + self.Opts |= EscapeHTML + } else { + self.Opts &= ^EscapeHTML + } +} + +// SetValidateString specifies if option ValidateString opens +func (self *Encoder) SetValidateString(f bool) { + if f { + self.Opts |= ValidateString + } else { + self.Opts &= ^ValidateString + } +} + +// SetNoValidateJSONMarshaler specifies if option NoValidateJSONMarshaler opens +func (self *Encoder) SetNoValidateJSONMarshaler(f bool) { + if f { + self.Opts |= NoValidateJSONMarshaler + } else { + self.Opts &= ^NoValidateJSONMarshaler + } +} + +// SetNoEncoderNewline specifies if option NoEncoderNewline opens +func (self *Encoder) SetNoEncoderNewline(f bool) { + if f { + self.Opts |= NoEncoderNewline + } else { + self.Opts &= ^NoEncoderNewline + } +} + +// SetCompactMarshaler specifies if option CompactMarshaler opens +func (self *Encoder) SetCompactMarshaler(f bool) { + if f { + self.Opts |= CompactMarshaler + } else { + self.Opts &= ^CompactMarshaler + } +} + +// SetNoQuoteTextMarshaler specifies if option NoQuoteTextMarshaler opens +func (self *Encoder) SetNoQuoteTextMarshaler(f bool) { + if f { + self.Opts |= NoQuoteTextMarshaler + } else { + self.Opts &= ^NoQuoteTextMarshaler + } +} + +// SetIndent instructs the encoder to format each subsequent encoded +// value as if indented by the package-level function EncodeIndent(). +// Calling SetIndent("", "") disables indentation. +func (enc *Encoder) SetIndent(prefix, indent string) { + enc.prefix = prefix + enc.indent = indent +} + +// Quote returns the JSON-quoted version of s. +func Quote(s string) string { + /* check for empty string */ + if s == "" { + return `""` + } + + out, _ := json.Marshal(s) + return string(out) +} + +// Encode returns the JSON encoding of val, encoded with opts. +func Encode(val interface{}, opts Options) ([]byte, error) { + return json.Marshal(val) +} + +// EncodeInto is like Encode but uses a user-supplied buffer instead of allocating +// a new one. +func EncodeInto(buf *[]byte, val interface{}, opts Options) error { + if buf == nil { + panic("user-supplied buffer buf is nil") + } + w := bytes.NewBuffer(*buf) + enc := json.NewEncoder(w) + enc.SetEscapeHTML((opts & EscapeHTML) != 0) + err := enc.Encode(val) + *buf = w.Bytes() + l := len(*buf) + if l > 0 && (opts & NoEncoderNewline != 0) && (*buf)[l-1] == '\n' { + *buf = (*buf)[:l-1] + } + return err +} + +// HTMLEscape appends to dst the JSON-encoded src with <, >, &, U+2028 and U+2029 +// characters inside string literals changed to \u003c, \u003e, \u0026, \u2028, \u2029 +// so that the JSON will be safe to embed inside HTML