| 1 | /* |
| 2 | Package to provides helpers to ease working with pointer values of marshalled structures. |
| 3 | */ |
| 4 | package to |
| 5 | |
| 6 | // Copyright 2017 Microsoft Corporation |
| 7 | // |
| 8 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | // you may not use this file except in compliance with the License. |
| 10 | // You may obtain a copy of the License at |
| 11 | // |
| 12 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | // |
| 14 | // Unless required by applicable law or agreed to in writing, software |
| 15 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | // See the License for the specific language governing permissions and |
| 18 | // limitations under the License. |
| 19 | |
| 20 | // String returns a string value for the passed string pointer. It returns the empty string if the |
| 21 | // pointer is nil. |
| 22 | func String(s *string) string { |
| 23 | if s != nil { |
| 24 | return *s |
| 25 | } |
| 26 | return "" |
| 27 | } |
| 28 | |
| 29 | // StringPtr returns a pointer to the passed string. |
| 30 | func StringPtr(s string) *string { |
| 31 | return &s |
| 32 | } |
| 33 | |
| 34 | // StringSlice returns a string slice value for the passed string slice pointer. It returns a nil |
| 35 | // slice if the pointer is nil. |
| 36 | func StringSlice(s *[]string) []string { |
| 37 | if s != nil { |
| 38 | return *s |
| 39 | } |
| 40 | return nil |
| 41 | } |
| 42 | |
| 43 | // StringSlicePtr returns a pointer to the passed string slice. |
| 44 | func StringSlicePtr(s []string) *[]string { |
| 45 | return &s |
| 46 | } |
| 47 | |
| 48 | // StringMap returns a map of strings built from the map of string pointers. The empty string is |
| 49 | // used for nil pointers. |
| 50 | func StringMap(msp map[string]*string) map[string]string { |
| 51 | ms := make(map[string]string, len(msp)) |
| 52 | for k, sp := range msp { |
| 53 | if sp != nil { |
| 54 | ms[k] = *sp |
| 55 | } else { |
| 56 | ms[k] = "" |
| 57 | } |
| 58 | } |
| 59 | return ms |
| 60 | } |
| 61 | |
| 62 | // StringMapPtr returns a pointer to a map of string pointers built from the passed map of strings. |
| 63 | func StringMapPtr(ms map[string]string) *map[string]*string { |
| 64 | msp := make(map[string]*string, len(ms)) |
| 65 | for k, s := range ms { |
| 66 | msp[k] = StringPtr(s) |
| 67 | } |
| 68 | return &msp |
| 69 | } |
| 70 | |
| 71 | // Bool returns a bool value for the passed bool pointer. It returns false if the pointer is nil. |
| 72 | func Bool(b *bool) bool { |
| 73 | if b != nil { |
| 74 | return *b |
| 75 | } |
| 76 | return false |
| 77 | } |
| 78 | |
| 79 | // BoolPtr returns a pointer to the passed bool. |
| 80 | func BoolPtr(b bool) *bool { |
| 81 | return &b |
| 82 | } |
| 83 | |
| 84 | // Int returns an int value for the passed int pointer. It returns 0 if the pointer is nil. |
| 85 | func Int(i *int) int { |
| 86 | if i != nil { |
| 87 | return *i |
| 88 | } |
| 89 | return 0 |
| 90 | } |
| 91 | |
| 92 | // IntPtr returns a pointer to the passed int. |
| 93 | func IntPtr(i int) *int { |
| 94 | return &i |
| 95 | } |
| 96 | |
| 97 | // Int32 returns an int value for the passed int pointer. It returns 0 if the pointer is nil. |
| 98 | func Int32(i *int32) int32 { |
| 99 | if i != nil { |
| 100 | return *i |
| 101 | } |
| 102 | return 0 |
| 103 | } |
| 104 | |
| 105 | // Int32Ptr returns a pointer to the passed int32. |
| 106 | func Int32Ptr(i int32) *int32 { |
| 107 | return &i |
| 108 | } |
| 109 | |
| 110 | // Int64 returns an int value for the passed int pointer. It returns 0 if the pointer is nil. |
| 111 | func Int64(i *int64) int64 { |
| 112 | if i != nil { |
| 113 | return *i |
| 114 | } |
| 115 | return 0 |
| 116 | } |
| 117 | |
| 118 | // Int64Ptr returns a pointer to the passed int64. |
| 119 | func Int64Ptr(i int64) *int64 { |
| 120 | return &i |
| 121 | } |
| 122 | |
| 123 | // Float32 returns an int value for the passed int pointer. It returns 0.0 if the pointer is nil. |
| 124 | func Float32(i *float32) float32 { |
| 125 | if i != nil { |
| 126 | return *i |
| 127 | } |
| 128 | return 0.0 |
| 129 | } |
| 130 | |
| 131 | // Float32Ptr returns a pointer to the passed float32. |
| 132 | func Float32Ptr(i float32) *float32 { |
| 133 | return &i |
| 134 | } |
| 135 | |
| 136 | // Float64 returns an int value for the passed int pointer. It returns 0.0 if the pointer is nil. |
| 137 | func Float64(i *float64) float64 { |
| 138 | if i != nil { |
| 139 | return *i |
| 140 | } |
| 141 | return 0.0 |
| 142 | } |
| 143 | |
| 144 | // Float64Ptr returns a pointer to the passed float64. |
| 145 | func Float64Ptr(i float64) *float64 { |
| 146 | return &i |
| 147 | } |
| 148 | |
| 149 | // ByteSlicePtr returns a pointer to the passed byte slice. |
| 150 | func ByteSlicePtr(b []byte) *[]byte { |
| 151 | return &b |
| 152 | } |