Skip to content
Snippets Groups Projects
Select Git revision
  • 78129222005b30f10cd248ad7d916f15bdcd7ed5
  • main default protected
  • v1.302.0
  • v1.301.0
  • v1.300.0
  • v1.299.0
  • v1.298.0
  • v1.297.0
  • v1.296.0
  • v1.295.0
  • v1.294.0
  • v1.293.0
  • v1.292.0
  • v1.291.0
  • v1.290.0
  • v1.289.0
  • v1.288.0
  • v1.287.0
  • v1.286.0
  • v1.285.0
  • v1.284.0
  • v1.283.0
22 results

params_test.go

Blame
  • params_test.go 3.30 KiB
    package api_test
    
    import (
    	"context"
    	"reflect"
    	"testing"
    
    	"github.com/aws/aws-lambda-go/events"
    	"gitlab.com/uafrica/go-utils/api"
    	"gitlab.com/uafrica/go-utils/logger"
    )
    
    type P1 struct {
    	A int `json:"a"`
    }
    
    type P2 struct {
    	P1       //nested struct must be filled
    	B  int   `json:"b"`
    	F  []int `json:"f"`
    }
    
    type P3 struct {
    	P2       //nessted struct must be filled
    	C  int   `json:"c"`
    	E  []int `json:"e"`
    }
    
    func TestNested(t *testing.T) {
    	logger.SetGlobalLevel(logger.LevelDebug)
    	logger.SetGlobalFormat(logger.NewConsole())
    	var ctx api.Context
    	var err error
    	ctx, err = api.New("request-id", nil).NewContext(
    		context.Background(),
    		"123",
    		events.APIGatewayProxyRequest{
    			QueryStringParameters: map[string]string{
    				"a": "1", //must be written into P3.P2.P1.A
    				"b": "2", //must be written into P3.P2.B
    				"c": "3", //must be written into P3.C
    				"d": "4", //ignored because no field tagged "d"
    			},
    			MultiValueQueryStringParameters: map[string][]string{
    				"e": {"5", "6", "7"}, //filled into P3.E as []string {"5", "6", "7"}
    				"f": {"6", "7", "8"}, //filled into P2 as []string {"6", "7", "8"}
    			},
    		})
    	if err != nil {
    		t.Fatal(err)
    	}
    
    	if p3d, err := ctx.GetRequestParams(reflect.TypeOf(P3{})); err != nil {
    		t.Fatal(err)
    	} else {
    		p3 := p3d.(P3)
    		t.Logf("p3: %+v", p3)
    		if p3.C != 3 || p3.B != 2 || p3.A != 1 {
    			t.Fatalf("wrong values")
    		}
    		if len(p3.E) != 3 || p3.E[0] != 5 || p3.E[1] != 6 || p3.E[2] != 7 {
    			t.Fatalf("wrong values")
    		}
    		if len(p3.F) != 3 || p3.F[0] != 6 || p3.F[1] != 7 || p3.F[2] != 8 {
    			t.Fatalf("wrong values")
    		}
    	}
    }
    
    type PageParams struct {