Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

109 linhas
2.4 KiB

  1. // Copyright 2016 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 optional provides versions of primitive types that can
  15. // be nil. These are useful in methods that update some of an API object's
  16. // fields.
  17. package optional
  18. import (
  19. "fmt"
  20. "strings"
  21. "time"
  22. )
  23. type (
  24. // Bool is either a bool or nil.
  25. Bool interface{}
  26. // String is either a string or nil.
  27. String interface{}
  28. // Int is either an int or nil.
  29. Int interface{}
  30. // Uint is either a uint or nil.
  31. Uint interface{}
  32. // Float64 is either a float64 or nil.
  33. Float64 interface{}
  34. // Duration is either a time.Duration or nil.
  35. Duration interface{}
  36. )
  37. // ToBool returns its argument as a bool.
  38. // It panics if its argument is nil or not a bool.
  39. func ToBool(v Bool) bool {
  40. x, ok := v.(bool)
  41. if !ok {
  42. doPanic("Bool", v)
  43. }
  44. return x
  45. }
  46. // ToString returns its argument as a string.
  47. // It panics if its argument is nil or not a string.
  48. func ToString(v String) string {
  49. x, ok := v.(string)
  50. if !ok {
  51. doPanic("String", v)
  52. }
  53. return x
  54. }
  55. // ToInt returns its argument as an int.
  56. // It panics if its argument is nil or not an int.
  57. func ToInt(v Int) int {
  58. x, ok := v.(int)
  59. if !ok {
  60. doPanic("Int", v)
  61. }
  62. return x
  63. }
  64. // ToUint returns its argument as a uint.
  65. // It panics if its argument is nil or not a uint.
  66. func ToUint(v Uint) uint {
  67. x, ok := v.(uint)
  68. if !ok {
  69. doPanic("Uint", v)
  70. }
  71. return x
  72. }
  73. // ToFloat64 returns its argument as a float64.
  74. // It panics if its argument is nil or not a float64.
  75. func ToFloat64(v Float64) float64 {
  76. x, ok := v.(float64)
  77. if !ok {
  78. doPanic("Float64", v)
  79. }
  80. return x
  81. }
  82. // ToDuration returns its argument as a time.Duration.
  83. // It panics if its argument is nil or not a time.Duration.
  84. func ToDuration(v Duration) time.Duration {
  85. x, ok := v.(time.Duration)
  86. if !ok {
  87. doPanic("Duration", v)
  88. }
  89. return x
  90. }
  91. func doPanic(capType string, v interface{}) {
  92. panic(fmt.Sprintf("optional.%s value should be %s, got %T", capType, strings.ToLower(capType), v))
  93. }