@@ -108,56 +108,62 @@ protected function addHackEnumConstraintCheck(HackBuilder $hb): void {
108
108
if (! Shapes :: keyExists($schema , ' hackEnum' )) {
109
109
return ;
110
110
}
111
+ $generateHackEnum = $schema [' generateHackEnum' ] ?? false ;
112
+ if (! $generateHackEnum ) {
111
113
112
- try {
113
- $rc = new \ReflectionClass ($schema [' hackEnum' ]);
114
- } catch (\ ReflectionException $e ) {
115
- throw new \Exception (Str \format (" Hack enum '%s' does not exist" , $schema [' hackEnum' ]));
116
- }
117
-
118
- invariant ($rc -> isEnum(), " '%s' is not an enum" , $schema [' hackEnum' ]);
119
-
120
- $schema_type = $schema [' type' ] ?? null ;
121
- $hack_enum_values = keyset [];
122
- foreach ($rc -> getConstants() as $hack_enum_value ) {
123
- if ($schema_type === TSchemaType :: INTEGER_T ) {
124
- $hack_enum_value = $hack_enum_value ?as int ;
125
- } else {
126
- $hack_enum_value = $hack_enum_value ?as string ;
114
+ try {
115
+ $rc = new \ReflectionClass ($schema [' hackEnum' ]);
116
+ } catch (\ ReflectionException $e ) {
117
+ throw new \Exception (Str \format (" Hack enum '%s' does not exist" , $schema [' hackEnum' ]));
127
118
}
128
- invariant (
129
- $hack_enum_value is nonnull ,
130
- " '%s' must contain only values of type %s" ,
131
- $rc -> getName(),
132
- $schema_type === TSchemaType :: INTEGER_T ? ' int' : ' string' ,
133
- );
134
- $hack_enum_values [] = $hack_enum_value ;
135
- }
136
119
137
- if ( Shapes :: keyExists( $schema , ' enum' )) {
138
- // If both `enum` and `hackEnum` are specified, assert that `enum` is a subset of
139
- // `hackEnum` values. Any value not also in `hackEnum` can't be valid.
140
- foreach ( $schema [ ' enum ' ] as $enum_value ) {
141
- invariant (
142
- $enum_value is string ,
143
- " Enum value '%s' is not a valid value for '%s' " ,
144
- \ print_r ( $enum_value , true ),
145
- $rc -> getName(),
146
- );
120
+ invariant ( $rc -> isEnum(), " '%s' is not an enum" , $schema [ ' hackEnum ' ]);
121
+
122
+ $schema_type = $schema [ ' type ' ] ?? null ;
123
+ $hack_enum_values = keyset [];
124
+ foreach ( $rc -> getConstants() as $hack_enum_value ) {
125
+ if ( $schema_type === TSchemaType :: INTEGER_T ) {
126
+ $hack_enum_value = $hack_enum_value ? as int ;
127
+ } else {
128
+ $hack_enum_value = $hack_enum_value ? as string ;
129
+ }
147
130
invariant (
148
- C \contains_key ($hack_enum_values , $enum_value ),
149
- " Enum value '%s' is unexpectedly not present in '%s'" ,
150
- \print_r ($enum_value , true ),
131
+ $hack_enum_value is nonnull ,
132
+ " '%s' must contain only values of type %s" ,
151
133
$rc -> getName(),
134
+ $schema_type === TSchemaType :: INTEGER_T ? ' int' : ' string' ,
152
135
);
136
+ $hack_enum_values [] = $hack_enum_value ;
137
+ }
138
+
139
+ if (Shapes :: keyExists($schema , ' enum' )) {
140
+ // If both `enum` and `hackEnum` are specified, assert that `enum` is a subset of
141
+ // `hackEnum` values. Any value not also in `hackEnum` can't be valid.
142
+ foreach ($schema [' enum' ] as $enum_value ) {
143
+ invariant (
144
+ $enum_value is string ,
145
+ " Enum value '%s' is not a valid value for '%s'" ,
146
+ \print_r ($enum_value , true ),
147
+ $rc -> getName(),
148
+ );
149
+ invariant (
150
+ C \contains_key ($hack_enum_values , $enum_value ),
151
+ " Enum value '%s' is unexpectedly not present in '%s'" ,
152
+ \print_r ($enum_value , true ),
153
+ $rc -> getName(),
154
+ );
155
+ }
153
156
}
157
+ $enum_name = Str \format (' \%s::class' , $rc -> getName());
158
+ } else {
159
+ $enum_name = $schema [' hackEnum' ]. ' ::class' ;
154
160
}
155
161
156
162
$hb -> addMultilineCall(
157
163
' $typed = Constraints\HackEnumConstraint::check' ,
158
164
vec [
159
165
' $typed' ,
160
- Str \ format ( ' \%s::class ' , $rc -> getName()) ,
166
+ $enum_name ,
161
167
' $pointer' ,
162
168
],
163
169
);
0 commit comments