github.com/aacfactory/fns@v1.2.86-0.20240310083819-80d667fc0a17/transports/params_test.go (about)

     1  /*
     2   * Copyright 2023 Wang Min Xiang
     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  
    18  package transports_test
    19  
    20  import (
    21  	"fmt"
    22  	"github.com/aacfactory/fns/transports"
    23  	"strings"
    24  	"testing"
    25  	"time"
    26  )
    27  
    28  type Id int64
    29  
    30  type Date time.Time
    31  
    32  type Range struct {
    33  	Offset int `json:"offset"`
    34  	Length int `json:"length"`
    35  }
    36  
    37  type Param struct {
    38  	Range
    39  	Id     Id          `json:"id"`
    40  	UserId Id          `json:"userId"`
    41  	Name   string      `json:"name"`
    42  	Score  float64     `json:"score"`
    43  	Age    uint        `json:"age"`
    44  	Date   Date        `json:"date"`
    45  	Dates  []time.Time `json:"dates"`
    46  }
    47  
    48  func TestDecodeParams(t *testing.T) {
    49  	params := transports.NewParams()
    50  	params.Set([]byte("id"), []byte("1"))
    51  	params.Set([]byte("userId"), []byte("1"))
    52  	params.Set([]byte("name"), []byte("name"))
    53  	params.Set([]byte("score"), []byte("99.99"))
    54  	params.Set([]byte("age"), []byte("13"))
    55  	params.Set([]byte("date"), []byte(time.Now().Format(time.RFC3339)))
    56  	params.Set([]byte("dates"), []byte(strings.Join([]string{time.Now().Format(time.RFC3339), time.Now().Format(time.RFC3339)}, ",")))
    57  	//params.Add([]byte("dates"), []byte(time.Now().Format(time.RFC3339)))
    58  	//params.Add([]byte("dates"), []byte(time.Now().Format(time.RFC3339)))
    59  
    60  	params.Set([]byte("offset"), []byte("10"))
    61  	params.Set([]byte("length"), []byte("50"))
    62  
    63  	fmt.Println(string(params.Encode()))
    64  
    65  	param := Param{}
    66  	decodeErr := transports.DecodeParams(params, &param)
    67  	if decodeErr != nil {
    68  		fmt.Println(fmt.Sprintf("%+v", decodeErr))
    69  		return
    70  	}
    71  	fmt.Println(fmt.Sprintf("%+v", param))
    72  }