Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 

57 Zeilen
1.6 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. pb "github.com/golang/protobuf/ptypes/struct"
  18. )
  19. // DecodeToMap converts a pb.Struct to a map from strings to Go types.
  20. // DecodeToMap panics if s is invalid.
  21. func DecodeToMap(s *pb.Struct) map[string]interface{} {
  22. if s == nil {
  23. return nil
  24. }
  25. m := map[string]interface{}{}
  26. for k, v := range s.Fields {
  27. m[k] = decodeValue(v)
  28. }
  29. return m
  30. }
  31. func decodeValue(v *pb.Value) interface{} {
  32. switch k := v.Kind.(type) {
  33. case *pb.Value_NullValue:
  34. return nil
  35. case *pb.Value_NumberValue:
  36. return k.NumberValue
  37. case *pb.Value_StringValue:
  38. return k.StringValue
  39. case *pb.Value_BoolValue:
  40. return k.BoolValue
  41. case *pb.Value_StructValue:
  42. return DecodeToMap(k.StructValue)
  43. case *pb.Value_ListValue:
  44. s := make([]interface{}, len(k.ListValue.Values))
  45. for i, e := range k.ListValue.Values {
  46. s[i] = decodeValue(e)
  47. }
  48. return s
  49. default:
  50. panic("protostruct: unknown kind")
  51. }
  52. }