github.com/erda-project/erda-infra@v1.0.9/providers/component-protocol/utils/cputil/obj_test.go (about)

     1  // Copyright (c) 2021 Terminus, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package cputil
    16  
    17  import (
    18  	"fmt"
    19  	"testing"
    20  
    21  	"github.com/stretchr/testify/assert"
    22  	"google.golang.org/protobuf/types/known/structpb"
    23  
    24  	"github.com/erda-project/erda-infra/providers/component-protocol/cptype"
    25  	"github.com/erda-project/erda-infra/providers/component-protocol/protobuf/proto-go/cp/pb"
    26  )
    27  
    28  func TestObjJSONTransfer(t *testing.T) {
    29  	src := pb.ComponentProtocol{
    30  		Options: &pb.ProtocolOptions{
    31  			SyncIntervalSecond: 0.01,
    32  		},
    33  		Hierarchy: &pb.Hierarchy{
    34  			Parallel: map[string]*structpb.Value{
    35  				"page": func() *structpb.Value {
    36  					result, err := structpb.NewValue([]interface{}{"filter", "grid"})
    37  					if err != nil {
    38  						panic(err)
    39  					}
    40  					return result
    41  				}(),
    42  			},
    43  		},
    44  	}
    45  	var dest cptype.ComponentProtocol
    46  
    47  	err := ObjJSONTransfer(&src, &dest)
    48  	assert.NoError(t, err)
    49  	assert.Equal(t, src.Options.SyncIntervalSecond, dest.Options.SyncIntervalSecond)
    50  	fmt.Printf("%#v\n", dest.Hierarchy.Parallel)
    51  }
    52  
    53  func TestMustFlatMapMeta(t *testing.T) {
    54  	input := map[string]interface{}{
    55  		"a": "b",
    56  	}
    57  	MustFlatMapMeta(input, false)
    58  	assert.True(t, len(input) == 1)
    59  
    60  	input = map[string]interface{}{
    61  		"c": "d",
    62  		"meta": map[string]interface{}{
    63  			"a": "b",
    64  		},
    65  	}
    66  	MustFlatMapMeta(input, false)
    67  	assert.True(t, len(input) == 3)
    68  
    69  	// map
    70  	input = map[string]interface{}{
    71  		"c": "d",
    72  		"meta": map[string]interface{}{
    73  			"a": "b",
    74  		},
    75  		"flatMeta": true,
    76  	}
    77  	MustFlatMapMeta(input, false)
    78  	assert.True(t, len(input) == 4)
    79  	assert.Equal(t, "b", input["a"])
    80  
    81  	// ref map
    82  	input = map[string]interface{}{
    83  		"c": "d",
    84  		"meta": map[string]interface{}{
    85  			"a": "b",
    86  			"e": "f",
    87  		},
    88  		"flatMeta": true,
    89  	}
    90  	MustFlatMapMeta(&input, false)
    91  	assert.True(t, len(input) == 5)
    92  	assert.Equal(t, "b", input["a"])
    93  	assert.Equal(t, "f", input["e"])
    94  
    95  	// nested flat meta
    96  	input = map[string]interface{}{
    97  		"a": "b",
    98  		"meta": map[string]interface{}{
    99  			"sub": map[string]interface{}{
   100  				"meta": map[string]interface{}{
   101  					"e": "f",
   102  					"g": "h",
   103  				},
   104  			},
   105  		},
   106  	}
   107  	MustFlatMapMeta(&input, true)
   108  	b, _ := json.MarshalIndent(input, "", "  ")
   109  	fmt.Println(string(b))
   110  }