github.com/cloudwego/dynamicgo@v0.2.6-0.20240519101509-707f41b6b834/thrift/test_util.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 thrift
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  
    23  	"github.com/cloudwego/dynamicgo/internal/util_test"
    24  )
    25  
    26  // GetFnDescFromFile get a fucntion descriptor from idl path (relative to your git root) and
    27  // the function name
    28  func GetFnDescFromFile(filePath, fnName string, opts Options) *FunctionDescriptor {
    29  	svc, err := opts.NewDescritorFromPath(context.Background(), util_test.MustGitPath(filePath))
    30  	if err != nil {
    31  		panic(fmt.Errorf("%s:%s", util_test.MustGitPath(filePath), err))
    32  	}
    33  	fn, err := svc.LookupFunctionByMethod(fnName)
    34  	if err != nil {
    35  		panic(err)
    36  	}
    37  	return fn
    38  }
    39  
    40  // FnResponse get the normal response type
    41  func FnResponse(fn *FunctionDescriptor) *TypeDescriptor {
    42  	// let-it-fail: it panic when something is nil
    43  	return fn.Response().Struct().FieldById(0).Type()
    44  }
    45  
    46  // FnWholeResponse get the normal response type
    47  func FnWholeResponse(fn *FunctionDescriptor) *TypeDescriptor {
    48  	// let-it-fail: it panic when something is nil
    49  	return fn.Response()
    50  }
    51  
    52  // FnRequest
    53  // We assume the request only have one argument and the only argument it the type we want.
    54  func FnRequest(fn *FunctionDescriptor) *TypeDescriptor {
    55  	// let-it-fail: it panic when something is nil
    56  	return fn.Request().Struct().Fields()[0].Type()
    57  }