github.com/goshafaq/sonic@v0.0.0-20231026082336-871835fb94c6/issue_test/issue45_test.go (about)

     1  /*
     2   * Copyright 2021 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 issue_test
    18  
    19  import (
    20  	"fmt"
    21  	. "github.com/goshafaq/sonic"
    22  	"sync"
    23  	"testing"
    24  )
    25  
    26  func ExtractJson(idx int, body interface{}) {
    27  	j := []byte(exampleJson[idx])
    28  
    29  	// REPLACE sonic WITH json
    30  	err := Unmarshal(j, &body)
    31  	if err != nil {
    32  		fmt.Println(err.Error())
    33  		return
    34  	}
    35  }
    36  
    37  var exampleJson1 = `{}`
    38  
    39  var exampleJson2 = `{}`
    40  
    41  var exampleJson = []string{exampleJson1, exampleJson2}
    42  
    43  type raceTestStruct struct {
    44  	f1 *[]raceTestStruct2 `json:"f1"`
    45  	f2 *int               `json:"f2"`
    46  	f3 *string            `json:"f3"`
    47  	f4 *string            `json:"f4"`
    48  }
    49  type raceTestStruct2 struct {
    50  	g1 *string           `json:"g1"`
    51  	g2 *string           `json:"g2"`
    52  	g3 *string           `json:"g3"`
    53  	g4 []raceTestStruct3 `json:"g4"`
    54  }
    55  type raceTestStruct3 struct {
    56  	e1 *string  `json:"e1"`
    57  	e2 *string  `json:"e2"`
    58  	e3 *float64 `json:"e3"`
    59  	e4 *float64 `json:"e4"`
    60  }
    61  
    62  func TestExtracJson(t *testing.T) {
    63  
    64  	wg := sync.WaitGroup{}
    65  
    66  	resultChan := make(chan raceTestStruct, 2)
    67  
    68  	wg.Add(1)
    69  	go func() {
    70  		defer wg.Done()
    71  		var model raceTestStruct
    72  		ExtractJson(0, &model)
    73  		resultChan <- model
    74  	}()
    75  
    76  	wg.Add(1)
    77  	go func() {
    78  		defer wg.Done()
    79  		var model raceTestStruct
    80  		ExtractJson(1, &model)
    81  		resultChan <- model
    82  	}()
    83  
    84  	var results []raceTestStruct
    85  	for i := 0; i < 2; i++ {
    86  		results = append(results, <-resultChan)
    87  	}
    88  
    89  	wg.Wait()
    90  	close(resultChan)
    91  }