|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +use crate::spec::{Datum, Literal, PrimitiveType, Struct}; |
| 19 | +use crate::{Error, ErrorKind}; |
| 20 | +use serde_derive::{Deserialize, Serialize}; |
| 21 | +use std::sync::Arc; |
| 22 | + |
| 23 | +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)] |
| 24 | +pub struct StructAccessor { |
| 25 | + position: usize, |
| 26 | + r#type: PrimitiveType, |
| 27 | + inner: Option<Box<StructAccessor>>, |
| 28 | +} |
| 29 | + |
| 30 | +pub(crate) type StructAccessorRef = Arc<StructAccessor>; |
| 31 | + |
| 32 | +impl StructAccessor { |
| 33 | + pub(crate) fn new(position: usize, r#type: PrimitiveType) -> Self { |
| 34 | + StructAccessor { |
| 35 | + position, |
| 36 | + r#type, |
| 37 | + inner: None, |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + pub(crate) fn wrap(position: usize, inner: Box<StructAccessor>) -> Self { |
| 42 | + StructAccessor { |
| 43 | + position, |
| 44 | + r#type: inner.r#type().clone(), |
| 45 | + inner: Some(inner), |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + pub(crate) fn position(&self) -> usize { |
| 50 | + self.position |
| 51 | + } |
| 52 | + |
| 53 | + pub(crate) fn r#type(&self) -> &PrimitiveType { |
| 54 | + &self.r#type |
| 55 | + } |
| 56 | + |
| 57 | + pub(crate) fn get<'a>(&'a self, container: &'a Struct) -> crate::Result<Datum> { |
| 58 | + match &self.inner { |
| 59 | + None => { |
| 60 | + if let Literal::Primitive(literal) = &container[self.position] { |
| 61 | + Ok(Datum::new(self.r#type().clone(), literal.clone())) |
| 62 | + } else { |
| 63 | + Err(Error::new( |
| 64 | + ErrorKind::Unexpected, |
| 65 | + "Expected Literal to be Primitive", |
| 66 | + )) |
| 67 | + } |
| 68 | + } |
| 69 | + Some(inner) => { |
| 70 | + if let Literal::Struct(wrapped) = &container[self.position] { |
| 71 | + inner.get(wrapped) |
| 72 | + } else { |
| 73 | + Err(Error::new( |
| 74 | + ErrorKind::Unexpected, |
| 75 | + "Nested accessor should only be wrapping a Struct", |
| 76 | + )) |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +#[cfg(test)] |
| 84 | +mod tests { |
| 85 | + use crate::expr::accessor::StructAccessor; |
| 86 | + use crate::spec::{Datum, Literal, PrimitiveType, Struct}; |
| 87 | + |
| 88 | + #[test] |
| 89 | + fn test_single_level_accessor() { |
| 90 | + let accessor = StructAccessor::new(1, PrimitiveType::Boolean); |
| 91 | + |
| 92 | + assert_eq!(accessor.r#type(), &PrimitiveType::Boolean); |
| 93 | + assert_eq!(accessor.position(), 1); |
| 94 | + |
| 95 | + let test_struct = |
| 96 | + Struct::from_iter(vec![Some(Literal::bool(false)), Some(Literal::bool(true))]); |
| 97 | + |
| 98 | + assert_eq!(accessor.get(&test_struct).unwrap(), Datum::bool(true)); |
| 99 | + } |
| 100 | + |
| 101 | + #[test] |
| 102 | + fn test_nested_accessor() { |
| 103 | + let nested_accessor = StructAccessor::new(1, PrimitiveType::Boolean); |
| 104 | + let accessor = StructAccessor::wrap(2, Box::new(nested_accessor)); |
| 105 | + |
| 106 | + assert_eq!(accessor.r#type(), &PrimitiveType::Boolean); |
| 107 | + //assert_eq!(accessor.position(), 1); |
| 108 | + |
| 109 | + let nested_test_struct = |
| 110 | + Struct::from_iter(vec![Some(Literal::bool(false)), Some(Literal::bool(true))]); |
| 111 | + |
| 112 | + let test_struct = Struct::from_iter(vec![ |
| 113 | + Some(Literal::bool(false)), |
| 114 | + Some(Literal::bool(false)), |
| 115 | + Some(Literal::Struct(nested_test_struct)), |
| 116 | + ]); |
| 117 | + |
| 118 | + assert_eq!(accessor.get(&test_struct).unwrap(), Datum::bool(true)); |
| 119 | + } |
| 120 | +} |
0 commit comments