MCPcopy Create free account
hub / github.com/NVIDIA/NeMo-Relay / merge_plugin_components

Function merge_plugin_components

crates/core/src/plugin.rs:927–962  ·  view source on GitHub ↗

Merges `right` components into `left` by `kind`, pairing repeated kinds positionally.

(left: &mut Json, right: Json)

Source from the content-addressed store, hash-verified

925
926/// Merges `right` components into `left` by `kind`, pairing repeated kinds positionally.
927fn merge_plugin_components(left: &mut Json, right: Json) {
928 let Json::Array(left_components) = left else {
929 *left = right;
930 return;
931 };
932 let Json::Array(right_components) = right else {
933 *left = right;
934 return;
935 };
936 let mut base_slots: HashMap<String, Vec<usize>> = HashMap::new();
937 for (index, component) in left_components.iter().enumerate() {
938 if let Some(kind) = component_kind(component) {
939 base_slots.entry(kind.to_string()).or_default().push(index);
940 }
941 }
942 let mut consumed: HashMap<String, usize> = HashMap::new();
943 for component in right_components {
944 let Some(kind) = component_kind(&component).map(str::to_owned) else {
945 left_components.push(component);
946 continue;
947 };
948 let nth = consumed.entry(kind.clone()).or_insert(0);
949 let slot = base_slots
950 .get(&kind)
951 .and_then(|slots| slots.get(*nth))
952 .copied();
953 *nth += 1;
954 match slot {
955 Some(index) if kind == "pricing" => {
956 merge_pricing_component(&mut left_components[index], component)
957 }
958 Some(index) => merge_json_value(&mut left_components[index], component),
959 None => left_components.push(component),
960 }
961 }
962}
963
964/// Recursively merges `right` into a `left` JSON object; arrays and scalars are replaced.
965fn merge_json_value(left: &mut Json, right: Json) {

Callers 1

layer_configFunction · 0.85

Calls 5

component_kindFunction · 0.85
merge_pricing_componentFunction · 0.85
merge_json_valueFunction · 0.85
getMethod · 0.80
pushMethod · 0.45

Tested by

no test coverage detected