pointerize.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. package thrift
  20. ///////////////////////////////////////////////////////////////////////////////
  21. // This file is home to helpers that convert from various base types to
  22. // respective pointer types. This is necessary because Go does not permit
  23. // references to constants, nor can a pointer type to base type be allocated
  24. // and initialized in a single expression.
  25. //
  26. // E.g., this is not allowed:
  27. //
  28. // var ip *int = &5
  29. //
  30. // But this *is* allowed:
  31. //
  32. // func IntPtr(i int) *int { return &i }
  33. // var ip *int = IntPtr(5)
  34. //
  35. // Since pointers to base types are commonplace as [optional] fields in
  36. // exported thrift structs, we factor such helpers here.
  37. ///////////////////////////////////////////////////////////////////////////////
  38. func Float32Ptr(v float32) *float32 { return &v }
  39. func Float64Ptr(v float64) *float64 { return &v }
  40. func IntPtr(v int) *int { return &v }
  41. func Int32Ptr(v int32) *int32 { return &v }
  42. func Int64Ptr(v int64) *int64 { return &v }
  43. func StringPtr(v string) *string { return &v }
  44. func Uint32Ptr(v uint32) *uint32 { return &v }
  45. func Uint64Ptr(v uint64) *uint64 { return &v }
  46. func BoolPtr(v bool) *bool { return &v }
  47. func ByteSlicePtr(v []byte) *[]byte { return &v }