MCPcopy Create free account
hub / github.com/hashintel/hash / decode_unknown

Method decode_unknown

libs/@local/hashql/eval/src/orchestrator/codec/decode/mod.rs:63–140  ·  view source on GitHub ↗
(&self, value: JsonValueRef<'_>)

Source from the content-addressed store, hash-verified

61 }
62
63 fn decode_unknown(&self, value: JsonValueRef<'_>) -> Result<Value<'heap, A>, DecodeError<'heap>>
64 where
65 A: Clone,
66 {
67 match value {
68 JsonValueRef::Null => Ok(Value::Unit),
69 JsonValueRef::Bool(value) => Ok(Value::Integer(value::Int::from(value))),
70 JsonValueRef::Number(number) => {
71 if let Some(value) = number.as_i128() {
72 Ok(Value::Integer(value::Int::from(value)))
73 } else {
74 let value = number
75 .as_f64()
76 .ok_or(DecodeError::NumberOutOfRange { expected: None })?;
77
78 Ok(Value::Number(value::Num::from(value)))
79 }
80 }
81 JsonValueRef::String(string) => {
82 let value = value::Str::from(Rc::from_in(string, self.alloc.clone()));
83 Ok(Value::String(value))
84 }
85 JsonValueRef::Array(values) => {
86 // We default in the output to **lists** not tuples. Very important distinction
87 let mut output = value::List::new();
88
89 for element in values {
90 output.push_back(self.decode_unknown(element.into())?);
91 }
92
93 Ok(Value::List(output))
94 }
95 JsonValueRef::Object(map) => {
96 if !map.keys().all(|key| {
97 // Mirrors the implementation of `BaseUrl` parse validation.
98 if key.len() < 2048
99 && let Ok(url) = url::Url::parse(key)
100 && matches!(url.scheme(), "http" | "https")
101 && !url.cannot_be_a_base()
102 && key.ends_with('/')
103 {
104 true
105 } else {
106 false
107 }
108 }) {
109 let mut dict = value::Dict::new();
110
111 for (key, value) in map {
112 let key = self.decode_unknown(JsonValueRef::String(key))?;
113 let value = self.decode_unknown(value.into())?;
114
115 dict.insert(key, value);
116 }
117
118 return Ok(Value::Dict(dict));
119 }
120

Callers 1

decodeMethod · 0.80

Calls 15

OkInterface · 0.85
IntegerClass · 0.85
StringClass · 0.85
co_sortFunction · 0.85
as_i128Method · 0.80
push_backMethod · 0.80
ends_withMethod · 0.80
intern_sliceMethod · 0.80
pushMethod · 0.65
fromFunction · 0.50
NumberClass · 0.50
ListClass · 0.50

Tested by

no test coverage detected