@@ -80,7 +80,7 @@ pub fn validate(def: RawModuleDefV9) -> Result<ModuleDef> {
80
80
. combine_errors ( )
81
81
. and_then ( |( mut tables, types, reducers) | {
82
82
let sched_exists = check_scheduled_reducers_exist ( & tables, & reducers) ;
83
- let default_values_work = default_values_work ( misc_exports, & mut validator, & mut tables) ;
83
+ let default_values_work = proccess_misc_exports ( misc_exports, & validator, & mut tables) ;
84
84
( sched_exists, default_values_work) . combine_errors ( ) ?;
85
85
86
86
Ok ( ( tables, types, reducers) )
@@ -348,21 +348,19 @@ impl ModuleValidator<'_> {
348
348
}
349
349
350
350
fn validate_column_default_value (
351
- & mut self ,
352
- tables : & mut HashMap < Identifier , TableDef > ,
353
- cdv : RawColumnDefaultValueV9 ,
354
- ) -> Result < ( ) > {
351
+ & self ,
352
+ tables : & HashMap < Identifier , TableDef > ,
353
+ cdv : & RawColumnDefaultValueV9 ,
354
+ ) -> Result < AlgebraicValue > {
355
355
let table_name = identifier ( cdv. table . clone ( ) ) ?;
356
356
357
357
// Extract the table. We cannot make progress otherwise.
358
- let table = tables
359
- . get_mut ( & table_name)
360
- . ok_or_else ( || ValidationError :: TableNotFound {
361
- table : cdv. table . clone ( ) ,
362
- } ) ?;
358
+ let table = tables. get ( & table_name) . ok_or_else ( || ValidationError :: TableNotFound {
359
+ table : cdv. table . clone ( ) ,
360
+ } ) ?;
363
361
364
362
// Get the column that a default is being added to.
365
- let Some ( col) = table. columns . get_mut ( cdv. col_id . idx ( ) ) else {
363
+ let Some ( col) = table. columns . get ( cdv. col_id . idx ( ) ) else {
366
364
return Err ( ValidationError :: ColumnNotFound {
367
365
table : cdv. table . clone ( ) ,
368
366
def : cdv. table . clone ( ) ,
@@ -383,17 +381,8 @@ impl ModuleValidator<'_> {
383
381
}
384
382
. into ( )
385
383
} ) ;
386
- // Ensure there's only one default value.
387
- if col. default_value . is_some ( ) {
388
- return Err ( ValidationError :: MultipleColumnDefaultValues {
389
- table : cdv. table . clone ( ) ,
390
- col_id : cdv. col_id ,
391
- }
392
- . into ( ) ) ;
393
- }
394
- col. default_value = field_value. ok ( ) ;
395
384
396
- Ok ( ( ) )
385
+ field_value
397
386
}
398
387
399
388
/// Validate a type definition.
@@ -944,20 +933,59 @@ fn check_scheduled_reducers_exist(
944
933
. collect_all_errors ( )
945
934
}
946
935
947
- fn default_values_work (
936
+ fn proccess_misc_exports (
948
937
misc_exports : Vec < RawMiscModuleExportV9 > ,
949
- validator : & mut ModuleValidator ,
938
+ validator : & ModuleValidator ,
950
939
tables : & mut IdentifierMap < TableDef > ,
951
940
) -> Result < ( ) > {
952
941
misc_exports
953
942
. into_iter ( )
954
943
. map ( |export| match export {
955
- RawMiscModuleExportV9 :: ColumnDefaultValue ( fdv ) => validator . validate_column_default_value ( tables , fdv ) ,
944
+ RawMiscModuleExportV9 :: ColumnDefaultValue ( cdv ) => process_column_default_value ( & cdv , validator , tables ) ,
956
945
_ => unimplemented ! ( "unknown misc export" ) ,
957
946
} )
958
947
. collect_all_errors :: < ( ) > ( )
959
948
}
960
949
950
+ fn process_column_default_value (
951
+ cdv : & RawColumnDefaultValueV9 ,
952
+ validator : & ModuleValidator ,
953
+ tables : & mut IdentifierMap < TableDef > ,
954
+ ) -> Result < ( ) > {
955
+ // Validate the default value
956
+ let validated_value = validator. validate_column_default_value ( tables, cdv) ?;
957
+
958
+ let table_name = identifier ( cdv. table . clone ( ) ) ?;
959
+ let table = tables
960
+ . get_mut ( & table_name)
961
+ . ok_or_else ( || ValidationError :: TableNotFound {
962
+ table : cdv. table . clone ( ) ,
963
+ } ) ?;
964
+
965
+ let column = table
966
+ . columns
967
+ . get_mut ( cdv. col_id . idx ( ) )
968
+ . ok_or_else ( || ValidationError :: ColumnNotFound {
969
+ table : cdv. table . clone ( ) ,
970
+ def : cdv. table . clone ( ) ,
971
+ column : cdv. col_id ,
972
+ } ) ?;
973
+
974
+ // Ensure there's only one default value.
975
+ if column. default_value . is_some ( ) {
976
+ return Err ( ValidationError :: MultipleColumnDefaultValues {
977
+ table : cdv. table . clone ( ) ,
978
+ col_id : cdv. col_id ,
979
+ }
980
+ . into ( ) ) ;
981
+ }
982
+
983
+ // Set the default value
984
+ column. default_value = Some ( validated_value) ;
985
+
986
+ Ok ( ( ) )
987
+ }
988
+
961
989
#[ cfg( test) ]
962
990
mod tests {
963
991
use crate :: def:: validate:: tests:: {
0 commit comments