github.com/cloudwego/frugal@v0.1.15/internal/binary/decoder/translator_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 TranslatorTestStruct struct { 27 A bool `frugal:"0,default,bool"` 28 B int8 `frugal:"1,required,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 []byte `frugal:"7,default,binary"` 35 I []int32 `frugal:"8,required,list<i32>"` 36 J map[string]string `frugal:"9,default,map<string:string>"` 37 K map[string]*TranslatorTestStruct `frugal:"65,required,map<string:TranslatorTestStruct>"` 38 } 39 40 func TestTranslator_Translate(t *testing.T) { 41 var v TranslatorTestStruct 42 p, err := CreateCompiler().Compile(reflect.TypeOf(v)) 43 require.NoError(t, err) 44 tr := Translate(p) 45 println(tr.Disassemble()) 46 } 47 48 func TestTranslator_NoCopyString(t *testing.T) { 49 var v NoCopyStringTestStruct 50 p, err := CreateCompiler().Compile(reflect.TypeOf(v)) 51 require.NoError(t, err) 52 tr := Translate(p) 53 println(tr.Disassemble()) 54 }