...

Source file src/cloud.google.com/go/internal/protostruct/protostruct_test.go

Documentation: cloud.google.com/go/internal/protostruct

     1  // Copyright 2017 Google LLC
     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 protostruct supports operations on the protocol buffer Struct message.
    16  package protostruct
    17  
    18  import (
    19  	"testing"
    20  
    21  	"cloud.google.com/go/internal/testutil"
    22  	pb "google.golang.org/protobuf/types/known/structpb"
    23  )
    24  
    25  func TestDecodeToMap(t *testing.T) {
    26  	if got := DecodeToMap(nil); !testutil.Equal(got, map[string]interface{}(nil)) {
    27  		t.Errorf("DecodeToMap(nil) = %v, want nil", got)
    28  	}
    29  	nullv := &pb.Value{Kind: &pb.Value_NullValue{}}
    30  	stringv := &pb.Value{Kind: &pb.Value_StringValue{"x"}}
    31  	boolv := &pb.Value{Kind: &pb.Value_BoolValue{true}}
    32  	numberv := &pb.Value{Kind: &pb.Value_NumberValue{2.7}}
    33  	in := &pb.Struct{Fields: map[string]*pb.Value{
    34  		"n": nullv,
    35  		"s": stringv,
    36  		"b": boolv,
    37  		"f": numberv,
    38  		"l": {Kind: &pb.Value_ListValue{&pb.ListValue{
    39  			Values: []*pb.Value{nullv, stringv, boolv, numberv},
    40  		}}},
    41  		"S": {Kind: &pb.Value_StructValue{&pb.Struct{Fields: map[string]*pb.Value{
    42  			"n1": nullv,
    43  			"b1": boolv,
    44  		}}}},
    45  	}}
    46  	want := map[string]interface{}{
    47  		"n": nil,
    48  		"s": "x",
    49  		"b": true,
    50  		"f": 2.7,
    51  		"l": []interface{}{nil, "x", true, 2.7},
    52  		"S": map[string]interface{}{"n1": nil, "b1": true},
    53  	}
    54  	got := DecodeToMap(in)
    55  	if diff := testutil.Diff(got, want); diff != "" {
    56  		t.Error(diff)
    57  	}
    58  }
    59  

View as plain text