Skip to content

Commit 67b1ae9

Browse files
authored
[chore] [pdata] Update comments for Value methods (#9038)
Resolves #9025
1 parent 6e2fdc7 commit 67b1ae9

File tree

2 files changed

+33
-16
lines changed

2 files changed

+33
-16
lines changed

pdata/pcommon/value.go

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ func (v Value) getState() *internal.State {
129129
return internal.GetValueState(internal.Value(v))
130130
}
131131

132+
// FromRaw sets the value from the given raw value.
133+
// Calling this function on zero-initialized Value will cause a panic.
132134
func (v Value) FromRaw(iv any) error {
133135
switch tv := iv.(type) {
134136
case nil:
@@ -198,37 +200,31 @@ func (v Value) Type() ValueType {
198200
// Str returns the string value associated with this Value.
199201
// The shorter name is used instead of String to avoid implementing fmt.Stringer interface.
200202
// If the Type() is not ValueTypeStr then returns empty string.
201-
// Calling this function on zero-initialized Value will cause a panic.
202203
func (v Value) Str() string {
203204
return v.getOrig().GetStringValue()
204205
}
205206

206207
// Int returns the int64 value associated with this Value.
207208
// If the Type() is not ValueTypeInt then returns int64(0).
208-
// Calling this function on zero-initialized Value will cause a panic.
209209
func (v Value) Int() int64 {
210210
return v.getOrig().GetIntValue()
211211
}
212212

213213
// Double returns the float64 value associated with this Value.
214214
// If the Type() is not ValueTypeDouble then returns float64(0).
215-
// Calling this function on zero-initialized Value will cause a panic.
216215
func (v Value) Double() float64 {
217216
return v.getOrig().GetDoubleValue()
218217
}
219218

220219
// Bool returns the bool value associated with this Value.
221220
// If the Type() is not ValueTypeBool then returns false.
222-
// Calling this function on zero-initialized Value will cause a panic.
223221
func (v Value) Bool() bool {
224222
return v.getOrig().GetBoolValue()
225223
}
226224

227225
// Map returns the map value associated with this Value.
228-
// If the Type() is not ValueTypeMap then returns an invalid map. Note that using
229-
// such map can cause panic.
230-
//
231-
// Calling this function on zero-initialized Value will cause a panic.
226+
// If the function is called on zero-initialized Value or if the Type() is not ValueTypeMap
227+
// then it returns an invalid map. Note that using such map can cause panic.
232228
func (v Value) Map() Map {
233229
kvlist := v.getOrig().GetKvlistValue()
234230
if kvlist == nil {
@@ -238,10 +234,8 @@ func (v Value) Map() Map {
238234
}
239235

240236
// Slice returns the slice value associated with this Value.
241-
// If the Type() is not ValueTypeSlice then returns an invalid slice. Note that using
242-
// such slice can cause panic.
243-
//
244-
// Calling this function on zero-initialized Value will cause a panic.
237+
// If the function is called on zero-initialized Value or if the Type() is not ValueTypeSlice
238+
// then returns an invalid slice. Note that using such slice can cause panic.
245239
func (v Value) Slice() Slice {
246240
arr := v.getOrig().GetArrayValue()
247241
if arr == nil {
@@ -251,10 +245,8 @@ func (v Value) Slice() Slice {
251245
}
252246

253247
// Bytes returns the ByteSlice value associated with this Value.
254-
// If the Type() is not ValueTypeBytes then returns an invalid ByteSlice object. Note that using
255-
// such slice can cause panic.
256-
//
257-
// Calling this function on zero-initialized Value will cause a panic.
248+
// If the function is called on zero-initialized Value or if the Type() is not ValueTypeBytes
249+
// then returns an invalid ByteSlice object. Note that using such slice can cause panic.
258250
func (v Value) Bytes() ByteSlice {
259251
bv, ok := v.getOrig().GetValue().(*otlpcommon.AnyValue_BytesValue)
260252
if !ok {
@@ -325,6 +317,7 @@ func (v Value) SetEmptySlice() Slice {
325317
}
326318

327319
// CopyTo copies the Value instance overriding the destination.
320+
// Calling this function on zero-initialized Value will cause a panic.
328321
func (v Value) CopyTo(dest Value) {
329322
dest.getState().AssertMutable()
330323
destOrig := dest.getOrig()
@@ -370,6 +363,7 @@ func (v Value) CopyTo(dest Value) {
370363
// AsString converts an OTLP Value object of any type to its equivalent string
371364
// representation. This differs from Str which only returns a non-empty value
372365
// if the ValueType is ValueTypeStr.
366+
// Calling this function on zero-initialized Value will cause a panic.
373367
func (v Value) AsString() string {
374368
switch v.Type() {
375369
case ValueTypeEmpty:

pdata/pcommon/value_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,29 @@ func TestNewValueFromRawInvalid(t *testing.T) {
556556
assert.EqualError(t, actual.FromRaw(ValueTypeDouble), "<Invalid value type pcommon.ValueType>")
557557
}
558558

559+
func TestInvalidValue(t *testing.T) {
560+
v := Value{}
561+
assert.Equal(t, false, v.Bool())
562+
assert.Equal(t, int64(0), v.Int())
563+
assert.Equal(t, float64(0), v.Double())
564+
assert.Equal(t, "", v.Str())
565+
assert.Equal(t, ByteSlice{}, v.Bytes())
566+
assert.Equal(t, Map{}, v.Map())
567+
assert.Equal(t, Slice{}, v.Slice())
568+
assert.Panics(t, func() { v.AsString() })
569+
assert.Panics(t, func() { v.AsRaw() })
570+
assert.Panics(t, func() { _ = v.FromRaw(1) })
571+
assert.Panics(t, func() { v.Type() })
572+
assert.Panics(t, func() { v.SetStr("") })
573+
assert.Panics(t, func() { v.SetInt(0) })
574+
assert.Panics(t, func() { v.SetDouble(0) })
575+
assert.Panics(t, func() { v.SetBool(false) })
576+
assert.Panics(t, func() { v.SetEmptyBytes() })
577+
assert.Panics(t, func() { v.SetEmptyMap() })
578+
assert.Panics(t, func() { v.SetEmptySlice() })
579+
assert.Panics(t, func() { v.CopyTo(NewValueEmpty()) })
580+
}
581+
559582
func generateTestValueMap() Value {
560583
ret := NewValueMap()
561584
attrMap := ret.Map()

0 commit comments

Comments
 (0)