25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 

59 satır
1.8 KiB

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