github.com/cloudwego/dynamicgo@v0.2.6-0.20240519101509-707f41b6b834/thrift/annotation/anno_mapping_test.go (about)

     1  /**
     2   * Copyright 2023 CloudWeGo Authors.
     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 annotation
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/cloudwego/dynamicgo/thrift"
    23  	"github.com/stretchr/testify/assert"
    24  	"github.com/stretchr/testify/require"
    25  )
    26  
    27  func TestMain(m *testing.M) {
    28  	InitAGWAnnos()
    29  	m.Run()
    30  }
    31  
    32  func TestGoTagJSON(t *testing.T) {
    33  	p, err := GetDescFromContent(`
    34  	namespace go kitex.test.server
    35  	struct Base {
    36  		1: required string required_field
    37  		999: required string double_quote (go.tag="json:\"test2,string\" protobuf:\"bytes,1,opt,name=test2\"")
    38  		888: string no_quote (go.tag='protobuf:"bytes,1,opt,name=test3" json:"test3,string"')
    39  		3: string pass 
    40  	}
    41  
    42  	service InboxService {
    43  		string ExampleMethod(1: Base req)
    44  	}
    45  	`, "ExampleMethod")
    46  	if err != nil {
    47  		t.Fatal(err)
    48  	}
    49  	req := p.Request().Struct().Fields()[0].Type()
    50  	assert.Equal(t, "test2", req.Struct().FieldById(999).Alias())
    51  	assert.Equal(t, "test3", req.Struct().FieldById(888).Alias())
    52  
    53  	_, err = GetDescFromContent(`
    54  	namespace go kitex.test.server
    55  	struct Base {
    56  		1: required string required_field
    57  		999: required string double_quote (go.tag="json:"test2,string" protobuf:\"bytes,1,opt,name=test2\"")
    58  		3: string pass 
    59  	}
    60  
    61  	service InboxService {
    62  		string ExampleMethod(1: Base req)
    63  	}
    64  	`, "ExampleMethod")
    65  	require.Error(t, err)
    66  	println(err.Error())
    67  
    68  	_, err = GetDescFromContent(`
    69  	namespace go kitex.test.server
    70  	struct Base {
    71  		1: required string required_field
    72  		999: required string double_quote (go.tag='json:\"test2,string\" protobuf:\"bytes,1,opt,name=test2\"')
    73  		3: string pass 
    74  	}
    75  
    76  	service InboxService {
    77  		string ExampleMethod(1: Base req)
    78  	}
    79  	`, "ExampleMethod")
    80  	require.NoError(t, err)
    81  }
    82  
    83  func TestApiKey(t *testing.T) {
    84  	p, err := GetDescFromContent(`
    85  	namespace go kitex.test.server
    86  
    87  	struct Base {
    88  		1: string FieldName1 (agw.source="query", api.key="test1")
    89  		2: string FieldName2 (agw.target="header")
    90  		3: string FieldName3 (agw.source="query", go.tag="json:\"test3\"")
    91  	} (agw.to_snake="true")
    92  
    93  	service InboxService {
    94  		string ExampleMethod(1: Base req)
    95  	}
    96  	`, "ExampleMethod")
    97  	require.NoError(t, err)
    98  	req := p.Request().Struct().Fields()[0].Type()
    99  	require.Equal(t, "test1", req.Struct().FieldById(1).Alias())
   100  	require.Equal(t, "test1", req.Struct().FieldById(1).HTTPMappings()[0].(apiQuery).value)
   101  	require.Equal(t, "field_name_2", req.Struct().FieldById(2).Alias())
   102  	require.Equal(t, "field_name_2", req.Struct().FieldById(2).HTTPMappings()[0].(apiHeader).value)
   103  	require.Equal(t, "test3", req.Struct().FieldById(3).Alias())
   104  	require.Equal(t, "field_name_3", req.Struct().FieldById(3).HTTPMappings()[0].(apiQuery).value)
   105  }
   106  
   107  func TestIgnoreField(t *testing.T) {
   108  	p, err := GetDescFromContent(`
   109  	namespace go kitex.test.server
   110  
   111  	struct Base {
   112  		0: string DefaultField
   113  		1: string FieldName1 (agw.target="ignore")
   114  		2: string FieldName2 (agw.source="ignore")
   115  	}
   116  
   117  	service InboxService {
   118  		string ExampleMethod(1: Base req)
   119  	}
   120  	`, "ExampleMethod")
   121  	require.NoError(t, err)
   122  	req := p.Request().Struct().Fields()[0].Type()
   123  	require.Equal(t, (*thrift.FieldDescriptor)(nil), req.Struct().FieldById(1))
   124  	require.Equal(t, (*thrift.FieldDescriptor)(nil), req.Struct().FieldByKey("FieldName1"))
   125  	require.NotNil(t, req.Struct().FieldById(2))
   126  	require.NotNil(t, req.Struct().FieldByKey("FieldName2"))
   127  }
   128  
   129  func TestApiNoneField(t *testing.T) {
   130  	p, err := GetDescFromContent(`
   131  	namespace go kitex.test.server
   132  
   133  	struct ExampleStruct {
   134  		0: string FieldName1
   135  		1: string FieldName2 (api.none="")
   136  	}
   137  
   138  	struct Base {
   139  		0: string DefaultField
   140  		1: string FieldName1 (api.none="")
   141  		2: ExampleStruct ExampleStruct
   142  	}
   143  
   144  	service InboxService {
   145  		Base ExampleMethod(1: Base req)
   146  	}
   147  	`, "ExampleMethod")
   148  	require.NoError(t, err)
   149  	req := p.Request().Struct().Fields()[0].Type()
   150  	resp := p.Response().Struct().Fields()[0].Type()
   151  	require.NotNil(t, req.Struct().FieldById(1))
   152  	require.NotNil(t, req.Struct().FieldByKey("FieldName1"))
   153  	require.NotNil(t, req.Struct().FieldById(2).Type().Struct().FieldById(1))
   154  	require.NotNil(t, req.Struct().FieldByKey("ExampleStruct").Type().Struct().FieldByKey("FieldName2"))
   155  	require.Equal(t, (*thrift.FieldDescriptor)(nil), resp.Struct().FieldById(1))
   156  	require.Equal(t, (*thrift.FieldDescriptor)(nil), resp.Struct().FieldByKey("FieldName1"))
   157  	require.Equal(t, (*thrift.FieldDescriptor)(nil), resp.Struct().FieldById(2).Type().Struct().FieldById(1))
   158  	require.Equal(t, (*thrift.FieldDescriptor)(nil), resp.Struct().FieldByKey("ExampleStruct").Type().Struct().FieldByKey("FieldName2"))
   159  }