MCPcopy
hub / github.com/uber/aresdb / ValueFromString

Function ValueFromString

memstore/common/data_value.go:332–485  ·  view source on GitHub ↗

ValueFromString converts raw string value to actual value given input data type.

(str string, dataType DataType)

Source from the content-addressed store, hash-verified

330
331// ValueFromString converts raw string value to actual value given input data type.
332func ValueFromString(str string, dataType DataType) (val DataValue, err error) {
333 val.DataType = dataType
334
335 if len(str) == 0 || str == "null" {
336 return
337 }
338
339 var b bool
340 var i int64
341 var f float64
342 var ui uint64
343
344 switch dataType {
345 case Bool:
346 val.IsBool = true
347 b, err = strconv.ParseBool(str)
348 if err != nil {
349 err = utils.StackError(err, "")
350 return
351 }
352 val.Valid = true
353 val.BoolVal = b
354 return
355 case Int8:
356 i, err = strconv.ParseInt(str, 10, 8)
357 if err != nil {
358 err = utils.StackError(err, "")
359 return
360 }
361
362 // We need to convert it from i64 to i8 since strconv.ParseXXX
363 // always returns the largest bit size value.
364 i8 := int8(i)
365 val.Valid = true
366 val.OtherVal = unsafe.Pointer(&i8)
367 return
368 case Uint8, SmallEnum:
369 ui, err = strconv.ParseUint(str, 10, 8)
370 if err != nil {
371 err = utils.StackError(err, "")
372 return
373 }
374 ui8 := uint8(ui)
375 val.Valid = true
376 val.OtherVal = unsafe.Pointer(&ui8)
377 return
378 case Int16:
379 i, err = strconv.ParseInt(str, 10, 16)
380 if err != nil {
381 err = utils.StackError(err, "")
382 return
383 }
384 i16 := int16(i)
385 val.Valid = true
386 val.OtherVal = unsafe.Pointer(&i16)
387 return
388 case Uint16, BigEnum:
389 ui, err = strconv.ParseUint(str, 10, 16)

Callers 7

LookupKeyMethod · 0.92
toVectorMethod · 0.92
toUpsertBatchMethod · 0.92
GetDataValueFunction · 0.85
data_value_test.goFile · 0.85
SetDefaultValueMethod · 0.85

Calls 8

StackErrorFunction · 0.92
NewBufferWriterFunction · 0.92
GeoPointFromStringFunction · 0.85
IsArrayTypeFunction · 0.85
ArrayValueFromStringFunction · 0.85
GetElementDataTypeFunction · 0.85
GetSerBytesMethod · 0.65
WriteMethod · 0.65

Tested by

no test coverage detected