Skip to content

Commit

Permalink
feat: remove unnecessary json api
Browse files Browse the repository at this point in the history
  • Loading branch information
AsterDY committed Oct 25, 2023
1 parent 74959d3 commit 428f360
Show file tree
Hide file tree
Showing 26 changed files with 157 additions and 7,675 deletions.
2 changes: 1 addition & 1 deletion conv/j2t/conv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import (

"github.com/cloudwego/dynamicgo/conv"
"github.com/cloudwego/dynamicgo/http"
sjson "github.com/cloudwego/dynamicgo/internal/json"
sjson "github.com/bytedance/sonic/ast"
"github.com/cloudwego/dynamicgo/internal/native"
"github.com/cloudwego/dynamicgo/internal/native/types"
"github.com/cloudwego/dynamicgo/internal/rt"
Expand Down
4 changes: 3 additions & 1 deletion conv/t2j/conv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,16 @@ import (
"time"
"unsafe"

sjson "github.com/bytedance/sonic/ast"
"github.com/cloudwego/dynamicgo/conv"
"github.com/cloudwego/dynamicgo/http"
sjson "github.com/cloudwego/dynamicgo/internal/json"
"github.com/cloudwego/dynamicgo/internal/util_test"
"github.com/cloudwego/dynamicgo/meta"
kbase "github.com/cloudwego/dynamicgo/testdata/kitex_gen/base"
"github.com/cloudwego/dynamicgo/testdata/kitex_gen/example3"
"github.com/cloudwego/dynamicgo/thrift"
"github.com/cloudwego/dynamicgo/thrift/base"
"github.com/davecgh/go-spew/spew"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -642,6 +643,7 @@ func TestOptionalDefaultValue(t *testing.T) {
ctx := context.WithValue(context.Background(), conv.CtxKeyHTTPResponse, resp)
out, err := cv.Do(ctx, desc, in)
require.NoError(t, err)
spew.Dump(string(out))
act := &example3.ExampleOptionalDefaultValue{}
err = json.Unmarshal(out, act)
require.NoError(t, err)
Expand Down
83 changes: 20 additions & 63 deletions internal/json/api_amd64.go
Original file line number Diff line number Diff line change
@@ -1,47 +1,34 @@
//go:build amd64 && go1.16
// +build amd64,go1.16

/**
* Copyright 2023 CloudWeGo Authors.
*
* 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.
*/
// Copyright 2023 CloudWeGo Authors.
//
// 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 json

import (
"runtime"
"unsafe"

"github.com/bytedance/sonic/encoder"
"github.com/chenzhuoyu/base64x"
"github.com/cloudwego/dynamicgo/internal/native"
"github.com/cloudwego/dynamicgo/internal/native/types"
"github.com/cloudwego/dynamicgo/internal/rt"
uq "github.com/cloudwego/dynamicgo/internal/unquote"
)

var typeByte = rt.UnpackEface(byte(0)).Type

func quote(buf *[]byte, val string) {
*buf = append(*buf, '"')
if len(val) == 0 {
*buf = append(*buf, '"')
}
NoQuote(buf, val)

*buf = append(*buf, '"')
}
var typeByte = rt.UnpackEface(byte(0)).Type

//go:nocheckptr
// NoQuote only escape inner '\' and '"' of one string, but it does add quotes besides string.
Expand Down Expand Up @@ -78,44 +65,10 @@ func NoQuote(buf *[]byte, val string) {
runtime.KeepAlive(sp)
}

func unquote(src string) (string, types.ParsingError) {
return uq.String(src)
}

func decodeBase64(src string) ([]byte, error) {
return base64x.StdEncoding.DecodeString(src)
}

func encodeBase64(src []byte) string {
return base64x.StdEncoding.EncodeToString(src)
}

func (self *Parser) decodeValue() (val types.JsonState) {
sv := (*rt.GoString)(unsafe.Pointer(&self.s))
self.p = native.Value(sv.Ptr, sv.Len, self.p, &val, 0)
return
}

func (self *Parser) skip() (int, types.ParsingError) {
fsm := types.NewStateMachine()
start := native.SkipOne(&self.s, &self.p, fsm)
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(), 0)
}

func i64toa(buf *[]byte, val int64) int {
rt.GuardSlice(buf, types.MaxInt64StringLen)
s := len(*buf)
ret := native.I64toa((*byte)(rt.IndexPtr((*rt.GoSlice)(unsafe.Pointer(buf)).Ptr, byteType.Size, s)), val)
ret := native.I64toa((*byte)(rt.IndexPtr((*rt.GoSlice)(unsafe.Pointer(buf)).Ptr, typeByte.Size, s)), val)
if ret < 0 {
*buf = append((*buf)[s:], '0')
return 1
Expand All @@ -127,11 +80,15 @@ func i64toa(buf *[]byte, val int64) int {
func f64toa(buf *[]byte, val float64) int {
rt.GuardSlice(buf, types.MaxFloat64StringLen)
s := len(*buf)
ret := native.F64toa((*byte)(rt.IndexPtr((*rt.GoSlice)(unsafe.Pointer(buf)).Ptr, byteType.Size, s)), val)
ret := native.F64toa((*byte)(rt.IndexPtr((*rt.GoSlice)(unsafe.Pointer(buf)).Ptr, typeByte.Size, s)), val)
if ret < 0 {
*buf = append((*buf)[s:], '0')
return 1
}
*buf = (*buf)[:s+ret]
return ret
}

func encodeBase64(src []byte) string {
return base64x.StdEncoding.EncodeToString(src)
}
71 changes: 0 additions & 71 deletions internal/json/api_amd64_test.go

This file was deleted.

Loading

0 comments on commit 428f360

Please sign in to comment.