git.delta.rocks / jrsonnet / refs/commits / 2cd889c5aabc

difftreelog

feat object extend_with_field method

Yaroslav Bolyukin2021-02-20parent: #2051500.patch.diff
in: master

2 files changed

modifiedbindings/jsonnet/src/val_modify.rsdiffbeforeafterboth
3//! In jrsonnet every value is immutable, and this code is probally broken3//! In jrsonnet every value is immutable, and this code is probally broken
44
5use jrsonnet_evaluator::{5use jrsonnet_evaluator::{ArrValue, EvaluationState, LazyBinding, LazyVal, ObjMember, Val};
6 ArrValue, EvaluationState, LazyBinding, LazyVal, ObjMember, ObjValue, Val,
7};
8use jrsonnet_parser::Visibility;6use jrsonnet_parser::Visibility;
9use std::{collections::HashMap, ffi::CStr, os::raw::c_char, rc::Rc};7use std::{ffi::CStr, os::raw::c_char, rc::Rc};
108
11/// # Safety9/// # Safety
12///10///
42) {40) {
43 match obj {41 match obj {
44 Val::Obj(old) => {42 Val::Obj(old) => {
45 let mut new = HashMap::new();43 let new_obj = old.clone().extend_with_field(
46 new.insert(
47 CStr::from_ptr(name).to_str().unwrap().into(),44 CStr::from_ptr(name).to_str().unwrap().into(),
48 ObjMember {45 ObjMember {
49 add: false,46 add: false,
52 location: None,49 location: None,
53 },50 },
54 );51 );
55 let new_obj = ObjValue::new(Some(old.clone()), Rc::new(new));52
56 *obj = Val::Obj(new_obj);53 *obj = Val::Obj(new_obj);
57 }54 }
58 _ => panic!("should receive object"),55 _ => panic!("should receive object"),
modifiedcrates/jrsonnet-evaluator/src/obj.rsdiffbeforeafterboth
3use jrsonnet_parser::{ExprLocation, Visibility};3use jrsonnet_parser::{ExprLocation, Visibility};
4use rustc_hash::FxHashMap;4use rustc_hash::FxHashMap;
5use std::hash::{Hash, Hasher};5use std::hash::{Hash, Hasher};
6use std::{cell::RefCell, fmt::Debug, rc::Rc};6use std::{cell::RefCell, fmt::Debug, hash::BuildHasherDefault, rc::Rc};
77
8#[derive(Debug)]8#[derive(Debug)]
9pub struct ObjMember {9pub struct ObjMember {
170 self.get_raw(key, self.0.this_obj.as_ref())170 self.get_raw(key, self.0.this_obj.as_ref())
171 }171 }
172
173 pub fn extend_with_field(self, key: IStr, value: ObjMember) -> Self {
174 let mut new = FxHashMap::with_capacity_and_hasher(1, BuildHasherDefault::default());
175 new.insert(key, value);
176 ObjValue::new(Some(self), Rc::new(new))
177 }
178
172 pub(crate) fn get_raw(&self, key: IStr, real_this: Option<&Self>) -> Result<Option<Val>> {179 pub(crate) fn get_raw(&self, key: IStr, real_this: Option<&Self>) -> Result<Option<Val>> {
173 let real_this = real_this.unwrap_or(self);180 let real_this = real_this.unwrap_or(self);