@@ -488,6 +488,54 @@ pub extern "C" fn ddog_ffe_evaluate(
488488 targeting_key : CharSlice < ' _ > ,
489489 attributes : * const FfeAttribute < ' _ > ,
490490 attributes_count : usize ,
491+ ) -> FfeResult {
492+ evaluate_with_dependencies (
493+ flag_key,
494+ expected_type,
495+ targeting_key,
496+ attributes,
497+ attributes_count,
498+ None ,
499+ std:: ptr:: null_mut ( ) ,
500+ )
501+ }
502+
503+ pub fn supports_dependent_flags ( ) -> bool {
504+ true
505+ }
506+
507+ pub type FfeDependencyCallback =
508+ extern "C" fn ( * mut std:: ffi:: c_void , CharSlice < ' _ > , FfeResult , bool ) ;
509+
510+ #[ no_mangle]
511+ pub extern "C" fn ddog_ffe_evaluate_with_dependencies (
512+ flag_key : CharSlice < ' _ > ,
513+ expected_type : i32 ,
514+ targeting_key : CharSlice < ' _ > ,
515+ attributes : * const FfeAttribute < ' _ > ,
516+ attributes_count : usize ,
517+ callback : FfeDependencyCallback ,
518+ callback_context : * mut std:: ffi:: c_void ,
519+ ) -> FfeResult {
520+ evaluate_with_dependencies (
521+ flag_key,
522+ expected_type,
523+ targeting_key,
524+ attributes,
525+ attributes_count,
526+ Some ( callback) ,
527+ callback_context,
528+ )
529+ }
530+
531+ fn evaluate_with_dependencies (
532+ flag_key : CharSlice < ' _ > ,
533+ expected_type : i32 ,
534+ targeting_key : CharSlice < ' _ > ,
535+ attributes : * const FfeAttribute < ' _ > ,
536+ attributes_count : usize ,
537+ callback : Option < FfeDependencyCallback > ,
538+ callback_context : * mut std:: ffi:: c_void ,
491539) -> FfeResult {
492540 if flag_key. as_raw_parts ( ) . 0 . is_null ( ) {
493541 return invalid_result ( ) ;
@@ -520,15 +568,13 @@ pub extern "C" fn ddog_ffe_evaluate(
520568 let context = EvaluationContext :: new ( targeting_key, Arc :: new ( attributes) ) ;
521569 let expects_object = matches ! ( expected_type, ExpectedFlagType :: Object ) ;
522570
523- FFE_STATE . with ( |state| {
571+ let ( result , evaluations ) = FFE_STATE . with ( |state| {
524572 let state = state. borrow ( ) ;
525- let assignment = ffe:: get_assignment (
526- state. config . as_ref ( ) ,
527- flag_key,
528- & context,
529- expected_type,
530- ffe:: now ( ) ,
531- ) ;
573+ let evaluation = match state. config . as_ref ( ) {
574+ Some ( config) => config. eval_flag_with_trace ( flag_key, & context, expected_type, ffe:: now ( ) , ffe:: EvaluationOptions :: default ( ) ) ,
575+ None => ffe:: EvaluationResult :: from_error ( EvaluationError :: ConfigurationMissing ) ,
576+ } ;
577+ let assignment = evaluation. result ;
532578 if let Err ( error) = & assignment {
533579 debug ! ( "Feature Flags: native resolution failed (flagKey={flag_key:?}, configVersion={}): {error}" , state. version) ;
534580 }
@@ -552,8 +598,30 @@ pub extern "C" fn ddog_ffe_evaluate(
552598 . config
553599 . as_ref ( )
554600 . is_some_and ( Configuration :: observe_full_evaluation_data) ;
555- result
556- } )
601+ ( result, evaluation. evaluations )
602+ } ) ;
603+ // Release the configuration borrow before calling into Zend; callbacks may reenter.
604+ if let Some ( callback) = callback {
605+ for record in evaluations {
606+ let mut dependency = match record. result {
607+ Err ( EvaluationError :: FlagDisabled | EvaluationError :: DefaultAllocationNull ) => {
608+ error_result ( ERROR_GENERAL , REASON_ERROR )
609+ }
610+ assignment => result_from_assignment ( assignment) ,
611+ } ;
612+ dependency. observe_full_evaluation_data = result. observe_full_evaluation_data ;
613+ let eligible = record. exposure_eligible
614+ && result. error_code == ERROR_NONE
615+ && result. variant . is_some ( ) ;
616+ callback (
617+ callback_context,
618+ CharSlice :: from ( record. flag_key . as_str ( ) ) ,
619+ dependency,
620+ eligible,
621+ ) ;
622+ }
623+ }
624+ result
557625}
558626
559627fn is_json_container ( value : & AssignmentValue ) -> bool {
@@ -851,6 +919,125 @@ mod tests {
851919 }
852920 }"# ;
853921
922+ extern "C" fn collect_dependency (
923+ context : * mut std:: ffi:: c_void ,
924+ key : CharSlice < ' _ > ,
925+ result : FfeResult ,
926+ eligible : bool ,
927+ ) {
928+ // SAFETY: tests pass an exclusive pointer to this vector for the synchronous call.
929+ let results = unsafe { & mut * context. cast :: < Vec < ( String , FfeResult , bool ) > > ( ) } ;
930+ results. push ( ( key. try_to_utf8 ( ) . unwrap ( ) . to_owned ( ) , result, eligible) ) ;
931+ }
932+
933+ #[ test]
934+ fn prerequisite_callbacks_keep_failed_and_rejected_evaluations ( ) {
935+ setup_zend_string_functions ( ) ;
936+ let wire = include_str ! ( "../../tests/ext/ffe/dependent_flags.json" ) ;
937+ let json = CString :: new ( wire) . unwrap ( ) ;
938+ assert ! ( ddog_ffe_load_config( char_slice( & json) ) ) ;
939+ for ( key, expected_error, prerequisite, eligible) in [
940+ ( "root" , ERROR_NONE , "parent" , true ) ,
941+ ( "rejected" , ERROR_NONE , "parent" , false ) ,
942+ ( "missing" , ERROR_GENERAL , "absent" , false ) ,
943+ ( "requires-disabled" , ERROR_GENERAL , "disabled" , false ) ,
944+ ] {
945+ let mut records: Vec < ( String , FfeResult , bool ) > = Vec :: new ( ) ;
946+ let result = ddog_ffe_evaluate_with_dependencies (
947+ CharSlice :: from ( key) ,
948+ TYPE_BOOLEAN ,
949+ CharSlice :: from ( "subject" ) ,
950+ std:: ptr:: null ( ) ,
951+ 0 ,
952+ collect_dependency,
953+ std:: ptr:: from_mut ( & mut records) . cast ( ) ,
954+ ) ;
955+ assert_eq ! ( result. error_code, expected_error, "{key}" ) ;
956+ assert_eq ! (
957+ records. len( ) ,
958+ 1 ,
959+ "{key}: memoization must not duplicate records"
960+ ) ;
961+ assert_eq ! ( records[ 0 ] . 0 , prerequisite) ;
962+ assert_eq ! ( records[ 0 ] . 2 , eligible) ;
963+ assert ! ( !records[ 0 ] . 1 . observe_full_evaluation_data) ;
964+ }
965+ clear_config ( ) ;
966+ }
967+
968+ #[ test]
969+ fn disabled_and_unassigned_prerequisites_are_errors_but_direct_results_are_not ( ) {
970+ setup_zend_string_functions ( ) ;
971+ let wire: serde_json:: Value =
972+ serde_json:: from_str ( include_str ! ( "../../tests/ext/ffe/dependent_flags.json" ) ) . unwrap ( ) ;
973+ for ( enabled, direct_reason) in [ ( false , REASON_DISABLED ) , ( true , REASON_DEFAULT ) ] {
974+ let mut config = wire. clone ( ) ;
975+ config[ "flags" ] [ "parent" ] [ "enabled" ] = enabled. into ( ) ;
976+ if enabled {
977+ config[ "flags" ] [ "parent" ] [ "allocations" ] = serde_json:: json!( [ ] ) ;
978+ }
979+ let json = CString :: new ( config. to_string ( ) ) . unwrap ( ) ;
980+ assert ! ( ddog_ffe_load_config( char_slice( & json) ) ) ;
981+ let mut records: Vec < ( String , FfeResult , bool ) > = Vec :: new ( ) ;
982+ let result = ddog_ffe_evaluate_with_dependencies (
983+ CharSlice :: from ( "root" ) ,
984+ TYPE_BOOLEAN ,
985+ CharSlice :: from ( "subject" ) ,
986+ std:: ptr:: null ( ) ,
987+ 0 ,
988+ collect_dependency,
989+ std:: ptr:: from_mut ( & mut records) . cast ( ) ,
990+ ) ;
991+ assert_eq ! ( result. error_code, ERROR_GENERAL ) ;
992+ assert_eq ! ( records. len( ) , 1 ) ;
993+ assert_eq ! ( records[ 0 ] . 0 , "parent" ) ;
994+ assert_eq ! ( records[ 0 ] . 1 . error_code, ERROR_GENERAL ) ;
995+ assert_eq ! ( records[ 0 ] . 1 . reason, REASON_ERROR ) ;
996+ assert ! ( records[ 0 ] . 1 . variant. is_none( ) ) ;
997+ assert ! ( !records[ 0 ] . 2 ) ;
998+
999+ let direct = ddog_ffe_evaluate (
1000+ CharSlice :: from ( "parent" ) ,
1001+ TYPE_BOOLEAN ,
1002+ CharSlice :: from ( "subject" ) ,
1003+ std:: ptr:: null ( ) ,
1004+ 0 ,
1005+ ) ;
1006+ assert_eq ! ( direct. error_code, ERROR_NONE ) ;
1007+ assert_eq ! ( direct. reason, direct_reason) ;
1008+ assert ! ( direct. variant. is_none( ) ) ;
1009+ }
1010+ clear_config ( ) ;
1011+ }
1012+
1013+ #[ test]
1014+ fn scalar_json_root_suppresses_prerequisite_exposures ( ) {
1015+ setup_zend_string_functions ( ) ;
1016+ let mut wire: serde_json:: Value =
1017+ serde_json:: from_str ( include_str ! ( "../../tests/ext/ffe/dependent_flags.json" ) ) . unwrap ( ) ;
1018+ wire[ "observeFullEvaluationData" ] = true . into ( ) ;
1019+ wire[ "flags" ] [ "root" ] [ "variationType" ] = "JSON" . into ( ) ;
1020+ wire[ "flags" ] [ "root" ] [ "variations" ] = serde_json:: json!( { "on" : { "key" : "on" , "value" : 42 } } ) ;
1021+ let json = CString :: new ( wire. to_string ( ) ) . unwrap ( ) ;
1022+ assert ! ( ddog_ffe_load_config( char_slice( & json) ) ) ;
1023+ let mut records: Vec < ( String , FfeResult , bool ) > = Vec :: new ( ) ;
1024+ let result = ddog_ffe_evaluate_with_dependencies (
1025+ CharSlice :: from ( "root" ) ,
1026+ TYPE_OBJECT ,
1027+ CharSlice :: from ( "subject" ) ,
1028+ std:: ptr:: null ( ) ,
1029+ 0 ,
1030+ collect_dependency,
1031+ std:: ptr:: from_mut ( & mut records) . cast ( ) ,
1032+ ) ;
1033+ assert_eq ! ( result. error_code, ERROR_TYPE_MISMATCH ) ;
1034+ assert_eq ! ( records. len( ) , 1 ) ;
1035+ assert ! ( !records[ 0 ] . 2 ) ;
1036+ assert ! ( records[ 0 ] . 1 . observe_full_evaluation_data) ;
1037+ assert ! ( supports_dependent_flags( ) ) ;
1038+ clear_config ( ) ;
1039+ }
1040+
8541041 #[ test]
8551042 fn empty_targeting_key_is_not_dropped ( ) {
8561043 setup_zend_string_functions ( ) ;
0 commit comments