github.com/cloudwego/frugal@v0.1.15/internal/binary/decoder/compiler_test.go (about) 1 /* 2 * Copyright 2022 ByteDance Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package decoder 18 19 import ( 20 `reflect` 21 `testing` 22 23 `github.com/stretchr/testify/require` 24 ) 25 26 type CompilerTest struct { 27 A bool `frugal:"0,default,bool"` 28 B int8 `frugal:"1,default,i8"` 29 C float64 `frugal:"2,default,double"` 30 D int16 `frugal:"3,default,i16"` 31 E int32 `frugal:"4,default,i32"` 32 F int64 `frugal:"5,default,i64"` 33 G string `frugal:"6,default,string"` 34 H *CompilerTest `frugal:"7,default,CompilerTest"` 35 I *CompilerTestSubStruct `frugal:"8,default,CompilerTestSubStruct"` 36 J map[string]int `frugal:"9,default,map<string:int>"` 37 K []string `frugal:"10,default,set<string>"` 38 L []string `frugal:"11,default,list<string>"` 39 M []byte `frugal:"12,default,binary"` 40 N []int8 `frugal:"13,default,set<i8>"` 41 O []int8 `frugal:"14,default,list<i8>"` 42 P int64 `frugal:"16,required,i64"` 43 } 44 45 func (self *CompilerTest) InitDefault() { 46 *self = CompilerTest{} 47 } 48 49 type CompilerTestSubStruct struct { 50 X int `frugal:"0,default,i64"` 51 Y *CompilerTestSubStruct `frugal:"1,default,CompilerTestSubStruct"` 52 Z map[*CompilerTest]*CompilerTestSubStruct `frugal:"2,default,map<CompilerTest:CompilerTestSubStruct>"` 53 } 54 55 func (self *CompilerTestSubStruct) InitDefault() { 56 *self = CompilerTestSubStruct{} 57 } 58 59 func TestCompiler_Compile(t *testing.T) { 60 p, err := CreateCompiler().Compile(reflect.TypeOf(CompilerTest{})) 61 require.NoError(t, err) 62 println(p.Disassemble()) 63 } 64 65 type NoCopyStringTestStruct struct { 66 A string `frugal:"1,default,string"` 67 B string `frugal:"2,default,string,nocopy"` 68 C *string `frugal:"3,optional,string,nocopy"` 69 D []byte `frugal:"4,default,binary"` 70 E []byte `frugal:"5,default,binary,nocopy"` 71 F *[]byte `frugal:"6,optional,binary,nocopy"` 72 } 73 74 func TestCompiler_NoCopyString(t *testing.T) { 75 p, err := CreateCompiler().Compile(reflect.TypeOf(NoCopyStringTestStruct{})) 76 require.NoError(t, err) 77 println(p.Disassemble()) 78 }