Skip to content

Commit

Permalink
add inital changes as per add apollo specfication docs
Browse files Browse the repository at this point in the history
  • Loading branch information
asr2003 authored Sep 15, 2024
1 parent d02b6e6 commit 589a0b6
Showing 1 changed file with 190 additions and 40 deletions.
230 changes: 190 additions & 40 deletions src/core/merge_right.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,36 @@
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};

Check warning on line 1 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Formatter and Lint Check

Diff in /home/runner/work/tailcall/tailcall/src/core/merge_right.rs
use std::sync::Arc;

use crate::core::valid::{Valid, ValidationError, Validator};
use serde_yaml::Value;

pub trait MergeRight {
fn merge_right(self, other: Self) -> Self;
fn merge_right(self, other: Self) -> Valid<Self, String>;
}

impl<A: MergeRight> MergeRight for Option<A> {
fn merge_right(self, other: Self) -> Self {
fn merge_right(self, other: Self) -> Valid<Self, String> {
match (self, other) {
(Some(this), Some(that)) => Some(this.merge_right(that)),
(None, Some(that)) => Some(that),
(Some(this), None) => Some(this),
(None, None) => None,
(Some(this), Some(that)) => this.merge_right(that).map(Some),
(None, Some(that)) => Valid::succeed(Some(that)),
(Some(this), None) => Valid::succeed(Some(this)),
(None, None) => Valid::succeed(None),
}
}
}

impl<A: MergeRight + Default> MergeRight for Arc<A> {
fn merge_right(self, other: Self) -> Self {
let l = Arc::into_inner(self);
let r = Arc::into_inner(other);
Arc::new(l.merge_right(r).unwrap_or_default())
fn merge_right(self, other: Self) -> Valid<Self, String> {
let l = Arc::try_unwrap(self).unwrap_or_else(|arc| (*arc).clone());
let r = Arc::try_unwrap(other).unwrap_or_else(|arc| (*arc).clone());
l.merge_right(r).map(Arc::new)
}
}

impl<A> MergeRight for Vec<A> {
fn merge_right(mut self, other: Self) -> Self {
fn merge_right(mut self, other: Self) -> Valid<Self, String> {
self.extend(other);
self
Valid::succeed(self)
}
}

Expand All @@ -38,89 +39,238 @@ where
K: Ord,
V: Clone + MergeRight,
{
fn merge_right(mut self, other: Self) -> Self {
for (other_name, mut other_value) in other {
if let Some(self_value) = self.remove(&other_name) {
other_value = self_value.merge_right(other_value);
fn merge_right(mut self, other: Self) -> Valid<Self, String> {
let mut errors = ValidationError::empty();

for (other_key, other_value) in other {
if let Some(self_value) = self.remove(&other_key) {
match self_value.merge_right(other_value).to_result() {
Ok(merged_value) => {
self.insert(other_key, merged_value);
}
Err(err) => {
errors = errors.combine(err);
}
}
} else {
self.insert(other_key, other_value);
}
}

self.insert(other_name, other_value);
if errors.is_empty() {
Valid::succeed(self)
} else {
Valid::from_validation_err(errors)
}
self
}
}

impl<V> MergeRight for BTreeSet<V>
where
V: Ord,
{
fn merge_right(mut self, other: Self) -> Self {
fn merge_right(mut self, other: Self) -> Valid<Self, String> {
self.extend(other);
self
Valid::succeed(self)
}
}

impl<V> MergeRight for HashSet<V>
where
V: Eq + std::hash::Hash,
{
fn merge_right(mut self, other: Self) -> Self {
fn merge_right(mut self, other: Self) -> Valid<Self, String> {
self.extend(other);
self
Valid::succeed(self)
}
}

impl<K, V> MergeRight for HashMap<K, V>
where
K: Eq + std::hash::Hash,
K: Eq + std::hash::Hash + Clone + std::fmt::Display,
V: MergeRight,
{
fn merge_right(mut self, other: Self) -> Self {
self.extend(other);
self
fn merge_right(mut self, other: Self) -> Valid<Self, String> {
let mut errors = ValidationError::empty();

for (key, other_value) in other {
if let Some(self_value) = self.remove(&key) {
match self_value.merge_right(other_value).to_result() {
Ok(merged_value) => {
self.insert(key, merged_value);
}
Err(err) => {
errors = errors.combine(err);
}
}
} else {
self.insert(key, other_value);
}
}

if errors.is_empty() {
Valid::succeed(self)
} else {
Valid::from_validation_err(errors)
}
}
}

impl MergeRight for Value {
fn merge_right(self, other: Self) -> Self {
fn merge_right(self, other: Self) -> Valid<Self, String> {
match (self, other) {
(Value::Null | Value::Bool(_) | Value::Number(_) | Value::String(_), other) => other,
(Value::Null | Value::Bool(_) | Value::Number(_) | Value::String(_), other) => {
Valid::succeed(other)
}
(Value::Sequence(mut lhs), other) => match other {
Value::Sequence(rhs) => {
lhs.extend(rhs);
Value::Sequence(lhs)
Valid::succeed(Value::Sequence(lhs))
}
other => {
lhs.push(other);
Value::Sequence(lhs)
Valid::succeed(Value::Sequence(lhs))
}
},
(Value::Mapping(mut lhs), other) => match other {
Value::Mapping(rhs) => {
for (key, mut value) in rhs {
let mut errors = ValidationError::empty();
for (key, other_value) in rhs {
if let Some(lhs_value) = lhs.remove(&key) {
value = lhs_value.merge_right(value);
match lhs_value.merge_right(other_value).to_result() {
Ok(merged_value) => {
lhs.insert(key, merged_value);
}
Err(err) => {
errors = errors.combine(err);
}
}
} else {
lhs.insert(key, other_value);
}
lhs.insert(key, value);
}
Value::Mapping(lhs)

if errors.is_empty() {
Valid::succeed(Value::Mapping(lhs))
} else {
Valid::from_validation_err(errors)
}
}
Value::Sequence(mut rhs) => {
rhs.push(Value::Mapping(lhs));
Value::Sequence(rhs)
Valid::succeed(Value::Sequence(rhs))
}
other => other,
other => Valid::succeed(other),
},
(Value::Tagged(mut lhs), other) => match other {
Value::Tagged(rhs) => {
if lhs.tag == rhs.tag {
lhs.value = lhs.value.merge_right(rhs.value);
Value::Tagged(lhs)
lhs.value = lhs.value.merge_right(rhs.value)?;
Valid::succeed(Value::Tagged(lhs))
} else {
Value::Tagged(rhs)
Valid::succeed(Value::Tagged(rhs))
}
}
other => other,
other => Valid::succeed(other),
},
}
}
}

impl MergeRight for TypeDefinition {

Check failure on line 180 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Check Examples

cannot find type `TypeDefinition` in this scope

Check failure on line 180 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests (WASM)

cannot find type `TypeDefinition` in this scope

Check failure on line 180 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests (Cloudflare)

cannot find type `TypeDefinition` in this scope

Check failure on line 180 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Formatter and Lint Check

cannot find type `TypeDefinition` in this scope

Check failure on line 180 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests on linux-x64-gnu

cannot find type `TypeDefinition` in this scope

Check failure on line 180 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Test AWS Lambda Build

cannot find type `TypeDefinition` in this scope

Check failure on line 180 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests on linux-x64-musl

cannot find type `TypeDefinition` in this scope

Check failure on line 180 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests on linux-arm64-musl

cannot find type `TypeDefinition` in this scope

Check failure on line 180 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests on darwin-arm64

cannot find type `TypeDefinition` in this scope

Check failure on line 180 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests on linux-arm64-gnu

cannot find type `TypeDefinition` in this scope

Check failure on line 180 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests on darwin-x64

cannot find type `TypeDefinition` in this scope

Check failure on line 180 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests on linux-ia32-gnu

cannot find type `TypeDefinition` in this scope

Check failure on line 180 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests on win32-x64-msvc

cannot find type `TypeDefinition` in this scope

Check failure on line 180 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests on win32-ia32-msvc

cannot find type `TypeDefinition` in this scope
fn merge_right(self, other: Self) -> Valid<Self, String> {
let mut errors = Vec::new();

// Check for type kind compatibility
if self.kind != other.kind {
errors.push(format!(
"Type kind conflict for '{}': {:?} vs {:?}",
self.name, self.kind, other.kind
));
}

// Merge fields
let merged_fields = self.fields.merge_right(other.fields);
if let Valid::Failure(err) = &merged_fields {
errors.push(err.clone());
}

// Merge directives
let merged_directives = self.directives.merge_right(other.directives);
if let Valid::Failure(err) = &merged_directives {
errors.push(err.clone());
}

if errors.is_empty() {
Valid::success(TypeDefinition {

Check failure on line 205 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Check Examples

cannot find struct, variant or union type `TypeDefinition` in this scope

Check failure on line 205 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests (WASM)

cannot find struct, variant or union type `TypeDefinition` in this scope

Check failure on line 205 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests (Cloudflare)

cannot find struct, variant or union type `TypeDefinition` in this scope

Check failure on line 205 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Formatter and Lint Check

cannot find struct, variant or union type `TypeDefinition` in this scope

Check failure on line 205 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests on linux-x64-gnu

cannot find struct, variant or union type `TypeDefinition` in this scope

Check failure on line 205 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Test AWS Lambda Build

cannot find struct, variant or union type `TypeDefinition` in this scope

Check failure on line 205 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests on linux-x64-musl

cannot find struct, variant or union type `TypeDefinition` in this scope

Check failure on line 205 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests on linux-arm64-musl

cannot find struct, variant or union type `TypeDefinition` in this scope

Check failure on line 205 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests on darwin-arm64

cannot find struct, variant or union type `TypeDefinition` in this scope

Check failure on line 205 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests on linux-arm64-gnu

cannot find struct, variant or union type `TypeDefinition` in this scope

Check failure on line 205 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests on darwin-x64

cannot find struct, variant or union type `TypeDefinition` in this scope

Check failure on line 205 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests on linux-ia32-gnu

cannot find struct, variant or union type `TypeDefinition` in this scope

Check failure on line 205 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests on win32-x64-msvc

cannot find struct, variant or union type `TypeDefinition` in this scope

Check failure on line 205 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests on win32-ia32-msvc

cannot find struct, variant or union type `TypeDefinition` in this scope
name: self.name,
kind: self.kind,
fields: merged_fields.unwrap_or_default(),
directives: merged_directives.unwrap_or_default(),
})
} else {
Valid::failure(errors.join("; "))
}
}
}

impl MergeRight for FieldDefinition {

Check failure on line 217 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Check Examples

cannot find type `FieldDefinition` in this scope

Check failure on line 217 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests (WASM)

cannot find type `FieldDefinition` in this scope

Check failure on line 217 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests (Cloudflare)

cannot find type `FieldDefinition` in this scope

Check failure on line 217 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Formatter and Lint Check

cannot find type `FieldDefinition` in this scope

Check failure on line 217 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests on linux-x64-gnu

cannot find type `FieldDefinition` in this scope

Check failure on line 217 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Test AWS Lambda Build

cannot find type `FieldDefinition` in this scope

Check failure on line 217 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests on linux-x64-musl

cannot find type `FieldDefinition` in this scope

Check failure on line 217 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests on linux-arm64-musl

cannot find type `FieldDefinition` in this scope

Check failure on line 217 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests on darwin-arm64

cannot find type `FieldDefinition` in this scope

Check failure on line 217 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests on linux-arm64-gnu

cannot find type `FieldDefinition` in this scope

Check failure on line 217 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests on darwin-x64

cannot find type `FieldDefinition` in this scope

Check failure on line 217 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests on linux-ia32-gnu

cannot find type `FieldDefinition` in this scope

Check failure on line 217 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests on win32-x64-msvc

cannot find type `FieldDefinition` in this scope

Check failure on line 217 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests on win32-ia32-msvc

cannot find type `FieldDefinition` in this scope
fn merge_right(self, other: Self) -> Valid<Self, String> {
let mut errors = Vec::new();

// Check if field types are identical
if self.field_type != other.field_type {
errors.push(format!(
"Field type conflict for '{}': {:?} vs {:?}",
self.name, self.field_type, other.field_type
));
}

// Merge arguments
let merged_args = self.arguments.merge_right(other.arguments);
if let Valid::Failure(err) = &merged_args {
errors.push(err.clone());
}

// Merge directives
let merged_directives = self.directives.merge_right(other.directives);
if let Valid::Failure(err) = &merged_directives {
errors.push(err.clone());
}

if errors.is_empty() {
Valid::success(FieldDefinition {

Check failure on line 242 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Check Examples

cannot find struct, variant or union type `FieldDefinition` in this scope

Check failure on line 242 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests (WASM)

cannot find struct, variant or union type `FieldDefinition` in this scope

Check failure on line 242 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests (Cloudflare)

cannot find struct, variant or union type `FieldDefinition` in this scope

Check failure on line 242 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Formatter and Lint Check

cannot find struct, variant or union type `FieldDefinition` in this scope

Check failure on line 242 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests on linux-x64-gnu

cannot find struct, variant or union type `FieldDefinition` in this scope

Check failure on line 242 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Test AWS Lambda Build

cannot find struct, variant or union type `FieldDefinition` in this scope

Check failure on line 242 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests on linux-x64-musl

cannot find struct, variant or union type `FieldDefinition` in this scope

Check failure on line 242 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests on linux-arm64-musl

cannot find struct, variant or union type `FieldDefinition` in this scope

Check failure on line 242 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests on darwin-arm64

cannot find struct, variant or union type `FieldDefinition` in this scope

Check failure on line 242 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests on linux-arm64-gnu

cannot find struct, variant or union type `FieldDefinition` in this scope

Check failure on line 242 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests on darwin-x64

cannot find struct, variant or union type `FieldDefinition` in this scope

Check failure on line 242 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests on linux-ia32-gnu

cannot find struct, variant or union type `FieldDefinition` in this scope

Check failure on line 242 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests on win32-x64-msvc

cannot find struct, variant or union type `FieldDefinition` in this scope

Check failure on line 242 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests on win32-ia32-msvc

cannot find struct, variant or union type `FieldDefinition` in this scope
name: self.name,
field_type: self.field_type,
arguments: merged_args.unwrap_or_default(),
directives: merged_directives.unwrap_or_default(),
})
} else {
Valid::failure(errors.join("; "))
}
}
}

impl MergeRight for Directive {

Check failure on line 254 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Check Examples

cannot find type `Directive` in this scope

Check failure on line 254 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests (WASM)

cannot find type `Directive` in this scope

Check failure on line 254 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests (Cloudflare)

cannot find type `Directive` in this scope

Check failure on line 254 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Formatter and Lint Check

cannot find type `Directive` in this scope

Check failure on line 254 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests on linux-x64-gnu

cannot find type `Directive` in this scope

Check failure on line 254 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Test AWS Lambda Build

cannot find type `Directive` in this scope

Check failure on line 254 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests on linux-x64-musl

cannot find type `Directive` in this scope

Check failure on line 254 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests on linux-arm64-musl

cannot find type `Directive` in this scope

Check failure on line 254 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests on darwin-arm64

cannot find type `Directive` in this scope

Check failure on line 254 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests on linux-arm64-gnu

cannot find type `Directive` in this scope

Check failure on line 254 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests on darwin-x64

cannot find type `Directive` in this scope

Check failure on line 254 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests on linux-ia32-gnu

cannot find type `Directive` in this scope

Check failure on line 254 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests on win32-x64-msvc

cannot find type `Directive` in this scope

Check failure on line 254 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests on win32-ia32-msvc

cannot find type `Directive` in this scope
fn merge_right(self, other: Self) -> Valid<Self, String> {
if self.name != other.name {
return Valid::failure(format!(
"Directive name conflict: '{}' vs '{}'",
self.name, other.name
));
}

// Merge arguments
let merged_arguments = self.arguments.merge_right(other.arguments);

if let Valid::Success(arguments) = merged_arguments {
Valid::success(Directive { name: self.name, arguments })

Check failure on line 267 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Check Examples

cannot find struct, variant or union type `Directive` in this scope

Check failure on line 267 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests (WASM)

cannot find struct, variant or union type `Directive` in this scope

Check failure on line 267 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests (Cloudflare)

cannot find struct, variant or union type `Directive` in this scope

Check failure on line 267 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Formatter and Lint Check

cannot find struct, variant or union type `Directive` in this scope

Check failure on line 267 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests on linux-x64-gnu

cannot find struct, variant or union type `Directive` in this scope

Check failure on line 267 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Test AWS Lambda Build

cannot find struct, variant or union type `Directive` in this scope

Check failure on line 267 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests on linux-x64-musl

cannot find struct, variant or union type `Directive` in this scope

Check failure on line 267 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests on linux-arm64-musl

cannot find struct, variant or union type `Directive` in this scope

Check failure on line 267 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests on darwin-arm64

cannot find struct, variant or union type `Directive` in this scope

Check failure on line 267 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests on linux-arm64-gnu

cannot find struct, variant or union type `Directive` in this scope

Check failure on line 267 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests on darwin-x64

cannot find struct, variant or union type `Directive` in this scope

Check failure on line 267 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests on linux-ia32-gnu

cannot find struct, variant or union type `Directive` in this scope

Check failure on line 267 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests on win32-x64-msvc

cannot find struct, variant or union type `Directive` in this scope

Check failure on line 267 in src/core/merge_right.rs

View workflow job for this annotation

GitHub Actions / Run Tests on win32-ia32-msvc

cannot find struct, variant or union type `Directive` in this scope
} else {
Valid::failure(format!(
"Directive '{}' argument conflict: {}",
self.name,
merged_arguments.unwrap_failure()
))
}
}
}

0 comments on commit 589a0b6

Please sign in to comment.