Skip to content

Commit

Permalink
feat(thrift): add base pkg (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaost authored Jul 25, 2024
1 parent 969ae87 commit 34d5327
Show file tree
Hide file tree
Showing 6 changed files with 698 additions and 0 deletions.
11 changes: 11 additions & 0 deletions protocol/thrift/base/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# base

`base` contains optimised generated code for kitex generic.
`base` is used widely in internal systems, so we expect to have a unified place for `base`.
It deprecates `Base` and `BaseResp` structs in `kitex` and `dynamicgo`

Users should not use this package, it's only for cloudwego internals.

The code under this package is generated by `fastgo` version of `thriftgo` which we haven't released yet and is only used in a few repos. You can find the code [here](https://github.com/cloudwego/thriftgo/tree/feat-fastcodec).

Use `thriftgo` `fastgo` at your own risks. it's NOT fully tested and that's why we manually add test code in this package.
127 changes: 127 additions & 0 deletions protocol/thrift/base/base.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions protocol/thrift/base/base.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#! /bin/bash

set -e

thriftgo -g fastgo:no_default_serdes=true -o=.. ./base.thrift

mv base.go base_tmp.go # fix sed base.go > base.go
# rm unused funcs and vars, keep the file smaller:
# func GetXXX
# func IsSet
# multiline DEFAULT vars
# singleline DEFAULT vars
sed '/func.* Get.* {/,/^}/d' base_tmp.go |\
sed '/func.* IsSet.* {/,/^}/d' |\
sed '/DEFAULT.*{/,/^}/d' |\
sed '/DEFAULT/d' > base.go

gofmt -w base.go
rm base_tmp.go
23 changes: 23 additions & 0 deletions protocol/thrift/base/base.thrift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace py base
namespace go base
namespace java com.bytedance.thrift.base

struct TrafficEnv {
1: bool Open = false,
2: string Env = "",
}

struct Base {
1: string LogID = "",
2: string Caller = "",
3: string Addr = "",
4: string Client = "",
5: optional TrafficEnv TrafficEnv,
6: optional map<string, string> Extra,
}

struct BaseResp {
1: string StatusMessage = "",
2: i32 StatusCode = 0,
3: optional map<string, string> Extra,
}
86 changes: 86 additions & 0 deletions protocol/thrift/base/base_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright 2024 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 base

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestBase(t *testing.T) {
var err error

p := NewBase()
p.InitDefault() // for code coverage ...
p.LogID = "1"
p.Caller = "2"
p.Addr = "3"
p.Client = "4"
t.Log(p.String())

sz := p.BLength()
b := make([]byte, sz)
n := p.FastWrite(b)
require.Equal(t, sz, n)

p2 := NewBase()
n, err = p2.FastRead(b)
require.Equal(t, len(b), n)
require.NoError(t, err)
require.Equal(t, p, p2)

// optional fields

p.TrafficEnv = NewTrafficEnv()
p.TrafficEnv.InitDefault() // for code coverage ...
p.Extra = map[string]string{"5": "6"}
t.Log(p.String())

sz = p.BLength()
b = make([]byte, sz)
n = p.FastWrite(b)
require.Equal(t, sz, n)

p2 = NewBase()
n, err = p2.FastRead(b)
require.Equal(t, len(b), n)
require.NoError(t, err)
require.Equal(t, p, p2)
}

func TestBaseResp(t *testing.T) {
var err error

p := NewBaseResp()
p.InitDefault() // for code coverage ...
p.StatusMessage = "msg"
p.StatusCode = 200
p.Extra = map[string]string{"k": "v"}
t.Log(p.String())

sz := p.BLength()
b := make([]byte, sz)
n := p.FastWrite(b)
require.Equal(t, sz, n)

p2 := NewBaseResp()
n, err = p2.FastRead(b)
require.Equal(t, len(b), n)
require.NoError(t, err)
require.Equal(t, p, p2)
}
Loading

0 comments on commit 34d5327

Please sign in to comment.