properties_gogo.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright (c) 2013, Vastech SA (PTY) LTD. All rights reserved.
  2. // http://code.google.com/p/gogoprotobuf/gogoproto
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. //
  15. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  18. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  19. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. package proto
  27. import (
  28. "fmt"
  29. "os"
  30. "reflect"
  31. )
  32. func (p *Properties) setCustomEncAndDec(typ reflect.Type) {
  33. p.ctype = typ
  34. if p.Repeated {
  35. p.enc = (*Buffer).enc_custom_slice_bytes
  36. p.dec = (*Buffer).dec_custom_slice_bytes
  37. p.size = size_custom_slice_bytes
  38. } else if typ.Kind() == reflect.Ptr {
  39. p.enc = (*Buffer).enc_custom_bytes
  40. p.dec = (*Buffer).dec_custom_bytes
  41. p.size = size_custom_bytes
  42. } else {
  43. p.enc = (*Buffer).enc_custom_ref_bytes
  44. p.dec = (*Buffer).dec_custom_ref_bytes
  45. p.size = size_custom_ref_bytes
  46. }
  47. }
  48. func (p *Properties) setNonNullableEncAndDec(typ reflect.Type) bool {
  49. switch typ.Kind() {
  50. case reflect.Bool:
  51. p.enc = (*Buffer).enc_ref_bool
  52. p.dec = (*Buffer).dec_ref_bool
  53. p.size = size_ref_bool
  54. case reflect.Int32, reflect.Uint32:
  55. p.enc = (*Buffer).enc_ref_int32
  56. p.dec = (*Buffer).dec_ref_int32
  57. p.size = size_ref_int32
  58. case reflect.Int64, reflect.Uint64:
  59. p.enc = (*Buffer).enc_ref_int64
  60. p.dec = (*Buffer).dec_ref_int64
  61. p.size = size_ref_int64
  62. case reflect.Float32:
  63. p.enc = (*Buffer).enc_ref_int32 // can just treat them as bits
  64. p.dec = (*Buffer).dec_ref_int32
  65. p.size = size_ref_int32
  66. case reflect.Float64:
  67. p.enc = (*Buffer).enc_ref_int64 // can just treat them as bits
  68. p.dec = (*Buffer).dec_ref_int64
  69. p.size = size_ref_int64
  70. case reflect.String:
  71. p.dec = (*Buffer).dec_ref_string
  72. p.enc = (*Buffer).enc_ref_string
  73. p.size = size_ref_string
  74. case reflect.Struct:
  75. p.stype = typ
  76. p.isMarshaler = isMarshaler(typ)
  77. p.isUnmarshaler = isUnmarshaler(typ)
  78. if p.Wire == "bytes" {
  79. p.enc = (*Buffer).enc_ref_struct_message
  80. p.dec = (*Buffer).dec_ref_struct_message
  81. p.size = size_ref_struct_message
  82. } else {
  83. fmt.Fprintf(os.Stderr, "proto: no coders for struct %T\n", typ)
  84. }
  85. default:
  86. return false
  87. }
  88. return true
  89. }
  90. func (p *Properties) setSliceOfNonPointerStructs(typ reflect.Type) {
  91. t2 := typ.Elem()
  92. p.sstype = typ
  93. p.stype = t2
  94. p.isMarshaler = isMarshaler(t2)
  95. p.isUnmarshaler = isUnmarshaler(t2)
  96. p.enc = (*Buffer).enc_slice_ref_struct_message
  97. p.dec = (*Buffer).dec_slice_ref_struct_message
  98. p.size = size_slice_ref_struct_message
  99. if p.Wire != "bytes" {
  100. fmt.Fprintf(os.Stderr, "proto: no ptr oenc for %T -> %T \n", typ, t2)
  101. }
  102. }