|
| 1 | +// Licensed to Elasticsearch B.V. under one or more contributor |
| 2 | +// license agreements. See the NOTICE file distributed with |
| 3 | +// this work for additional information regarding copyright |
| 4 | +// ownership. Elasticsearch B.V. licenses this file to you under |
| 5 | +// the Apache License, Version 2.0 (the "License"); you may |
| 6 | +// not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package opt |
| 19 | + |
| 20 | +import "github.com/elastic/go-structform" |
| 21 | + |
| 22 | +// ZeroInterface is a type interface for cases where we need to cast from a void pointer |
| 23 | +type ZeroInterface interface { |
| 24 | + IsZero() bool |
| 25 | +} |
| 26 | + |
| 27 | +// Int |
| 28 | + |
| 29 | +// Int is a wrapper for "optional" types, with the bool value indicating |
| 30 | +// if the stored int is a legitimate value. |
| 31 | +type Int struct { |
| 32 | + exists bool |
| 33 | + value int |
| 34 | +} |
| 35 | + |
| 36 | +// NewUintNone returns a new OptUint wrapper |
| 37 | +func NewIntNone() Int { |
| 38 | + return Int{ |
| 39 | + exists: false, |
| 40 | + value: 0, |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +// UintWith returns a new OptUint wrapper with a given int |
| 45 | +func IntWith(i int) Int { |
| 46 | + return Int{ |
| 47 | + exists: true, |
| 48 | + value: i, |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +// IsZero returns true if the underlying value nil |
| 53 | +func (opt Int) IsZero() bool { |
| 54 | + return !opt.exists |
| 55 | +} |
| 56 | + |
| 57 | +// Exists returns true if the underlying value exists |
| 58 | +func (opt Int) Exists() bool { |
| 59 | + return opt.exists |
| 60 | +} |
| 61 | + |
| 62 | +// ValueOr returns the stored value, or a given int |
| 63 | +// Please do not use this for populating reported data, |
| 64 | +// as we actually want to avoid sending zeros where values are functionally null |
| 65 | +func (opt Int) ValueOr(i int) int { |
| 66 | + if opt.exists { |
| 67 | + return opt.value |
| 68 | + } |
| 69 | + return i |
| 70 | +} |
| 71 | + |
| 72 | +// Fold implements the folder interface for OptUint |
| 73 | +func (opt *Int) Fold(v structform.ExtVisitor) error { |
| 74 | + if opt.exists { |
| 75 | + value := opt.value |
| 76 | + _ = v.OnInt(value) |
| 77 | + } else { |
| 78 | + _ = v.OnNil() |
| 79 | + } |
| 80 | + return nil |
| 81 | +} |
| 82 | + |
| 83 | +// Uint |
| 84 | + |
| 85 | +// Uint is a wrapper for "optional" types, with the bool value indicating |
| 86 | +// if the stored int is a legitimate value. |
| 87 | +type Uint struct { |
| 88 | + exists bool |
| 89 | + value uint64 |
| 90 | +} |
| 91 | + |
| 92 | +// NewUintNone returns a new OptUint wrapper |
| 93 | +func NewUintNone() Uint { |
| 94 | + return Uint{ |
| 95 | + exists: false, |
| 96 | + value: 0, |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +// UintWith returns a new OptUint wrapper with a given int |
| 101 | +func UintWith(i uint64) Uint { |
| 102 | + return Uint{ |
| 103 | + exists: true, |
| 104 | + value: i, |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +// IsZero returns true if the underlying value nil |
| 109 | +func (opt Uint) IsZero() bool { |
| 110 | + return !opt.exists |
| 111 | +} |
| 112 | + |
| 113 | +// Exists returns true if the underlying value exists |
| 114 | +func (opt Uint) Exists() bool { |
| 115 | + return opt.exists |
| 116 | +} |
| 117 | + |
| 118 | +// ValueOr returns the stored value, or a given int |
| 119 | +// Please do not use this for populating reported data, |
| 120 | +// as we actually want to avoid sending zeros where values are functionally null |
| 121 | +func (opt Uint) ValueOr(i uint64) uint64 { |
| 122 | + if opt.exists { |
| 123 | + return opt.value |
| 124 | + } |
| 125 | + return i |
| 126 | +} |
| 127 | + |
| 128 | +// SumOptUint sums a list of OptUint values |
| 129 | +func SumOptUint(opts ...Uint) uint64 { |
| 130 | + var sum uint64 |
| 131 | + for _, opt := range opts { |
| 132 | + sum += opt.ValueOr(0) |
| 133 | + } |
| 134 | + return sum |
| 135 | +} |
| 136 | + |
| 137 | +// Fold implements the folder interface for OptUint |
| 138 | +func (opt *Uint) Fold(v structform.ExtVisitor) error { |
| 139 | + if opt.exists { |
| 140 | + value := opt.value |
| 141 | + _ = v.OnUint64(value) |
| 142 | + } else { |
| 143 | + _ = v.OnNil() |
| 144 | + } |
| 145 | + return nil |
| 146 | +} |
| 147 | + |
| 148 | +// Float |
| 149 | + |
| 150 | +// Float is a wrapper for "optional" types, with the bool value indicating |
| 151 | +// if the stored int is a legitimate value. |
| 152 | +type Float struct { |
| 153 | + exists bool |
| 154 | + value float64 |
| 155 | +} |
| 156 | + |
| 157 | +// NewFloatNone returns a new uint wrapper |
| 158 | +func NewFloatNone() Float { |
| 159 | + return Float{ |
| 160 | + exists: false, |
| 161 | + value: 0, |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +// FloatWith returns a new uint wrapper for the specified value |
| 166 | +func FloatWith(f float64) Float { |
| 167 | + return Float{ |
| 168 | + exists: true, |
| 169 | + value: f, |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +// IsZero returns true if the underlying value nil |
| 174 | +func (opt Float) IsZero() bool { |
| 175 | + return !opt.exists |
| 176 | +} |
| 177 | + |
| 178 | +// Exists returns true if the underlying value exists |
| 179 | +func (opt Float) Exists() bool { |
| 180 | + return opt.exists |
| 181 | +} |
| 182 | + |
| 183 | +// ValueOr returns the stored value, or zero |
| 184 | +// Please do not use this for populating reported data, |
| 185 | +// as we actually want to avoid sending zeros where values are functionally null |
| 186 | +func (opt Float) ValueOr(f float64) float64 { |
| 187 | + if opt.exists { |
| 188 | + return opt.value |
| 189 | + } |
| 190 | + return f |
| 191 | +} |
| 192 | + |
| 193 | +// Fold implements the folder interface for OptUint |
| 194 | +func (opt *Float) Fold(v structform.ExtVisitor) error { |
| 195 | + if opt.exists { |
| 196 | + value := opt.value |
| 197 | + _ = v.OnFloat64(value) |
| 198 | + } else { |
| 199 | + _ = v.OnNil() |
| 200 | + } |
| 201 | + return nil |
| 202 | +} |
0 commit comments