MCPcopy Create free account
hub / github.com/MaterializeInc/materialize / decode_text

Method decode_text

src/pgrepr/src/value.rs:668–741  ·  view source on GitHub ↗

Deserializes a value of type `ty` from `raw` using the [text encoding format](Format::Text).

(
        ty: &'a Type,
        raw: &'a [u8],
    )

Source from the content-addressed store, hash-verified

666 /// Deserializes a value of type `ty` from `raw` using the [text encoding
667 /// format](Format::Text).
668 pub fn decode_text<'a>(
669 ty: &'a Type,
670 raw: &'a [u8],
671 ) -> Result<Value, Box<dyn Error + Sync + Send>> {
672 let s = str::from_utf8(raw)?;
673 Ok(match ty {
674 Type::Array(elem_type) => {
675 let (elements, dims) = strconv::parse_array(
676 s,
677 || None,
678 |elem_text| Value::decode_text(elem_type, elem_text.as_bytes()).map(Some),
679 )?;
680 Value::Array { dims, elements }
681 }
682 Type::Int2Vector { .. } => {
683 return Err("input of Int2Vector types is not implemented".into());
684 }
685 Type::Bool => Value::Bool(strconv::parse_bool(s)?),
686 Type::Bytea => Value::Bytea(strconv::parse_bytes(s)?),
687 Type::Char => Value::Char(raw.get(0).copied().unwrap_or(0)),
688 Type::Date => Value::Date(strconv::parse_date(s)?),
689 Type::Float4 => Value::Float4(strconv::parse_float32(s)?),
690 Type::Float8 => Value::Float8(strconv::parse_float64(s)?),
691 Type::Int2 => Value::Int2(strconv::parse_int16(s)?),
692 Type::Int4 => Value::Int4(strconv::parse_int32(s)?),
693 Type::Int8 => Value::Int8(strconv::parse_int64(s)?),
694 Type::UInt2 => Value::UInt2(UInt2(strconv::parse_uint16(s)?)),
695 Type::UInt4 => Value::UInt4(UInt4(strconv::parse_uint32(s)?)),
696 Type::UInt8 => Value::UInt8(UInt8(strconv::parse_uint64(s)?)),
697 Type::Interval { .. } => Value::Interval(Interval(strconv::parse_interval(s)?)),
698 Type::Json => return Err("input of json types is not implemented".into()),
699 Type::Jsonb => Value::Jsonb(Jsonb(strconv::parse_jsonb(s)?)),
700 Type::List(elem_type) => Value::List(strconv::parse_list(
701 s,
702 matches!(**elem_type, Type::List(..)),
703 || None,
704 |elem_text| Value::decode_text(elem_type, elem_text.as_bytes()).map(Some),
705 )?),
706 Type::Map { value_type } => Value::Map(strconv::parse_map(
707 s,
708 matches!(**value_type, Type::Map { .. }),
709 |elem_text| {
710 elem_text
711 .map(|t| Value::decode_text(value_type, t.as_bytes()))
712 .transpose()
713 },
714 )?),
715 Type::Name => Value::Name(strconv::parse_pg_legacy_name(s)),
716 Type::Numeric { constraints } => Value::Numeric(Numeric(rescale_numeric(
717 strconv::parse_numeric(s)?,
718 constraints.as_ref(),
719 )?)),
720 Type::Oid | Type::RegClass | Type::RegProc | Type::RegType => {
721 Value::Oid(strconv::parse_oid(s)?)
722 }
723 Type::Record(_) => {
724 return Err("input of anonymous composite types is not implemented".into());
725 }

Callers

nothing calls this directly

Calls 15

parse_boolFunction · 0.85
ByteaClass · 0.85
parse_dateFunction · 0.85
parse_float32Function · 0.85
parse_float64Function · 0.85
parse_int16Function · 0.85
parse_int32Function · 0.85
parse_int64Function · 0.85
parse_uint16Function · 0.85
parse_uint32Function · 0.85
parse_uint64Function · 0.85
parse_intervalFunction · 0.85

Tested by

no test coverage detected