15
15
// specific language governing permissions and limitations
16
16
// under the License.
17
17
18
- use crate :: spec:: { Datum , Literal , PrimitiveType , Struct } ;
19
- use crate :: { Error , ErrorKind } ;
20
18
use serde_derive:: { Deserialize , Serialize } ;
21
19
use std:: sync:: Arc ;
22
20
21
+ use crate :: spec:: { Datum , Literal , PrimitiveType , Struct } ;
22
+ use crate :: { Error , ErrorKind , Result } ;
23
+
23
24
#[ derive( Debug , Serialize , Deserialize , Clone , PartialEq , Eq ) ]
24
25
pub struct StructAccessor {
25
26
position : usize ,
@@ -54,11 +55,13 @@ impl StructAccessor {
54
55
& self . r#type
55
56
}
56
57
57
- pub ( crate ) fn get < ' a > ( & ' a self , container : & ' a Struct ) -> crate :: Result < Datum > {
58
+ pub ( crate ) fn get < ' a > ( & ' a self , container : & ' a Struct ) -> Result < Option < Datum > > {
58
59
match & self . inner {
59
60
None => {
60
- if let Literal :: Primitive ( literal) = & container[ self . position ] {
61
- Ok ( Datum :: new ( self . r#type ( ) . clone ( ) , literal. clone ( ) ) )
61
+ if container. is_null_at_index ( self . position ) {
62
+ Ok ( None )
63
+ } else if let Literal :: Primitive ( literal) = & container[ self . position ] {
64
+ Ok ( Some ( Datum :: new ( self . r#type ( ) . clone ( ) , literal. clone ( ) ) ) )
62
65
} else {
63
66
Err ( Error :: new (
64
67
ErrorKind :: Unexpected ,
@@ -95,7 +98,19 @@ mod tests {
95
98
let test_struct =
96
99
Struct :: from_iter ( vec ! [ Some ( Literal :: bool ( false ) ) , Some ( Literal :: bool ( true ) ) ] ) ;
97
100
98
- assert_eq ! ( accessor. get( & test_struct) . unwrap( ) , Datum :: bool ( true ) ) ;
101
+ assert_eq ! ( accessor. get( & test_struct) . unwrap( ) , Some ( Datum :: bool ( true ) ) ) ;
102
+ }
103
+
104
+ #[ test]
105
+ fn test_single_level_accessor_null ( ) {
106
+ let accessor = StructAccessor :: new ( 1 , PrimitiveType :: Boolean ) ;
107
+
108
+ assert_eq ! ( accessor. r#type( ) , & PrimitiveType :: Boolean ) ;
109
+ assert_eq ! ( accessor. position( ) , 1 ) ;
110
+
111
+ let test_struct = Struct :: from_iter ( vec ! [ Some ( Literal :: bool ( false ) ) , None ] ) ;
112
+
113
+ assert_eq ! ( accessor. get( & test_struct) . unwrap( ) , None ) ;
99
114
}
100
115
101
116
#[ test]
@@ -115,6 +130,25 @@ mod tests {
115
130
Some ( Literal :: Struct ( nested_test_struct) ) ,
116
131
] ) ;
117
132
118
- assert_eq ! ( accessor. get( & test_struct) . unwrap( ) , Datum :: bool ( true ) ) ;
133
+ assert_eq ! ( accessor. get( & test_struct) . unwrap( ) , Some ( Datum :: bool ( true ) ) ) ;
134
+ }
135
+
136
+ #[ test]
137
+ fn test_nested_accessor_null ( ) {
138
+ let nested_accessor = StructAccessor :: new ( 0 , PrimitiveType :: Boolean ) ;
139
+ let accessor = StructAccessor :: wrap ( 2 , Box :: new ( nested_accessor) ) ;
140
+
141
+ assert_eq ! ( accessor. r#type( ) , & PrimitiveType :: Boolean ) ;
142
+ //assert_eq!(accessor.position(), 1);
143
+
144
+ let nested_test_struct = Struct :: from_iter ( vec ! [ None , Some ( Literal :: bool ( true ) ) ] ) ;
145
+
146
+ let test_struct = Struct :: from_iter ( vec ! [
147
+ Some ( Literal :: bool ( false ) ) ,
148
+ Some ( Literal :: bool ( false ) ) ,
149
+ Some ( Literal :: Struct ( nested_test_struct) ) ,
150
+ ] ) ;
151
+
152
+ assert_eq ! ( accessor. get( & test_struct) . unwrap( ) , None ) ;
119
153
}
120
154
}
0 commit comments