Skip to content

Commit

Permalink
Update Introspection PEG and improve Enum Value verification
Browse files Browse the repository at this point in the history
  • Loading branch information
Struan Judd committed Jan 14, 2024
1 parent 67626ea commit d9c893b
Show file tree
Hide file tree
Showing 25 changed files with 880 additions and 460 deletions.
36 changes: 25 additions & 11 deletions src/GqlPlus.Verifier/Verification/Schema/OutputContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace GqlPlus.Verifier.Verification.Schema;
internal record class OutputContext(IMap<AstDescribed> Types, ITokenMessages Errors, IMap<string> EnumValues)
: UsageContext(Types, Errors)
{
public bool GetEnumValue(string value, out string? type)
internal bool GetEnumValue(string value, out string? type)
=> EnumValues.TryGetValue(value, out type);

internal override void CheckArgumentType<TReference>(TReference type)
Expand All @@ -18,20 +18,34 @@ internal override void CheckArgumentType<TReference>(TReference type)
type.Name = enumType!;
}

if (!string.IsNullOrWhiteSpace(output.EnumValue)
&& GetType(type.TypeName, out var theType)) {
if (theType is EnumDeclAst enumDecl) {
if (!enumDecl.HasValue(output.EnumValue!)) {
AddError(type, "Output Argument Enum Value", $"'{output.EnumValue}' is not a Value of '{type.Name}'");
}
} else {
AddError(type, "Output Argument Enum", $"'{type.Name}' is not an Enum type");
if (!(string.IsNullOrWhiteSpace(output.EnumValue) || CheckEnumValue(output))) {
AddError(type, "Output Argument Enum Value", $"'{output.EnumValue}' is not a Value of '{type.Name}'");
}
}

base.CheckArgumentType(type);
}

internal bool CheckEnumValue(OutputReferenceAst output)
{
var enumType = output.Name!;
while (GetType(enumType, out var theType)) {
if (theType is EnumDeclAst enumDecl) {
if (enumDecl.HasValue(output.EnumValue!)) {
return true;
}

return;
if (string.IsNullOrWhiteSpace(enumDecl.Extends)) {
break;
}

enumType = enumDecl.Extends;
} else {
AddError(output, "Output Argument Enum", $"'{enumType}' is not an Enum type");
break;
}
}

base.CheckArgumentType(type);
return false;
}
}
12 changes: 3 additions & 9 deletions src/GqlPlus.Verifier/Verification/Schema/VerifyOutputTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,14 @@ protected override void UsageValue(OutputDeclAst usage, OutputContext context)
.Where(f => !string.IsNullOrWhiteSpace(f.Type.EnumValue));

foreach (var enumField in enumFields) {
if (context.GetType(enumField.Type.TypeName, out var type)) {
if (type is EnumDeclAst enumDecl) {
if (!enumDecl.HasValue(enumField.Type.EnumValue!)) {
context.AddError(enumField, "Output Field Enum Value", $"'{enumField.Type.EnumValue}' is not a Value of '{enumField.Type.Name}'");
}
} else {
context.AddError(enumField, "Output Field Enum Value", $"'{enumField.Type.Name}' is not an Enum type");
}
} else {
if (string.IsNullOrWhiteSpace(enumField.Type.Name)) {
if (context.GetEnumValue(enumField.Type.EnumValue!, out var enumType)) {
enumField.Type.Name = enumType!;
} else {
context.AddError(enumField, "Output Field Enum", $"Enum Value '{enumField.Type.EnumValue}' not defined");
}
} else if (!context.CheckEnumValue(enumField.Type)) {
context.AddError(enumField, "Output Field Enum Value", $"'{enumField.Type.EnumValue}' is not a Value of '{enumField.Type.Name}'");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ output _Categories {
output _Category {
: _Aliased
resolution: _Resolution
output: String
output: _TypeRef<_TypeKind.Output>
modifiers: _Modifier[]
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
output _Constant {
enum: _BaseType<_TypeKind.Enum>
value: String
| Internal
| Simple
| _Simple
| _ConstantList
| _ConstantMap
}

output _Simple {
| Boolean
| _ScalarValue<_Scalar.Number Number>
| _ScalarValue<_Scalar.String String>
| _EnumValue
}

output _ConstantList {
| _Constant[]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ output _Categories {
output _Category {
: _Aliased
resolution: _Resolution
output: String
output: _TypeRef<_TypeKind.Output>
modifiers: _Modifier[]
}

Expand Down Expand Up @@ -76,22 +76,33 @@ output _BaseType<$kind> {
kind: $kind
}

enum _TypeKind { Basic Enum Internal Input Output Scalar }
enum _SimpleKind { Basic Enum Internal Scalar }

enum _TypeKind { : _SimpleKind Input Output }

output _TypeRef<$kind> {
kind: $kind
name: String
}

output _TypeSimple {
| _BaseType<_TypeKind.Basic>
| _BaseType<_TypeKind.Scalar>
| _BaseType<_TypeKind.Enum>
| _TypeRef<_TypeKind.Basic>
| _TypeRef<_TypeKind.Scalar>
| _TypeRef<_TypeKind.Enum>
}
output _Constant {
enum: _BaseType<_TypeKind.Enum>
value: String
| Internal
| Simple
| _Simple
| _ConstantList
| _ConstantMap
}

output _Simple {
| Boolean
| _ScalarValue<_Scalar.Number Number>
| _ScalarValue<_Scalar.String String>
| _EnumValue
}

output _ConstantList {
| _Constant[]
}
Expand Down Expand Up @@ -122,15 +133,20 @@ output _ModifierDictionary {
}
output _TypeEnum {
: _BaseType<_TypeKind.Enum>
base: String?
values: _Aliased[]
allValues: _EnumValue[]
base: _TypeRef<_TypeKind.Enum>?
members: _Aliased[]
allMembers: _EnumMember[]
}

output _EnumValue {
output _EnumMember {
: _Aliased
enum: String
}

output _EnumValue {
: _TypeRef<_TypeKind.Enum>
value: String
}
enum _Scalar { Number String Union }

output _TypeScalar {
Expand All @@ -139,9 +155,14 @@ output _TypeScalar {
| _ScalarUnion
}

output _ScalarRef<$base> {
: _TypeRef<_TypeKind.Scalar>
scalar: $base
}

output _BaseScalar<$base> {
: _BaseType<_TypeKind.Scalar>
base: $base
base: _ScalarRef<$base>
}

output _ScalarNumber {
Expand Down Expand Up @@ -170,6 +191,11 @@ output _ScalarUnion {
: _BaseScalar<_Scalar.Union>
references: _TypeSimple[]
}

output _ScalarValue<$base $value> {
: _ScalarRef<$base>
value: $value
}
output _TypeObject<$kind $base $field> {
: _BaseType<$kind>
base: $base?
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
output _TypeEnum {
: _BaseType<_TypeKind.Enum>
base: String?
values: _Aliased[]
allValues: _EnumValue[]
base: _TypeRef<_TypeKind.Enum>?
members: _Aliased[]
allMembers: _EnumMember[]
}

output _EnumValue {
output _EnumMember {
: _Aliased
enum: String
}

output _EnumValue {
: _TypeRef<_TypeKind.Enum>
value: String
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@ output _TypeScalar {
| _ScalarUnion
}

output _ScalarRef<$base> {
: _TypeRef<_TypeKind.Scalar>
scalar: $base
}

output _BaseScalar<$base> {
: _BaseType<_TypeKind.Scalar>
base: $base
base: _ScalarRef<$base>
}

output _ScalarNumber {
Expand Down Expand Up @@ -37,3 +42,8 @@ output _ScalarUnion {
: _BaseScalar<_Scalar.Union>
references: _TypeSimple[]
}

output _ScalarValue<$base $value> {
: _ScalarRef<$base>
value: $value
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,17 @@ output _BaseType<$kind> {
kind: $kind
}

enum _TypeKind { Basic Enum Internal Input Output Scalar }
enum _SimpleKind { Basic Enum Internal Scalar }

enum _TypeKind { : _SimpleKind Input Output }

output _TypeRef<$kind> {
kind: $kind
name: String
}

output _TypeSimple {
| _BaseType<_TypeKind.Basic>
| _BaseType<_TypeKind.Scalar>
| _BaseType<_TypeKind.Enum>
| _TypeRef<_TypeKind.Basic>
| _TypeRef<_TypeKind.Scalar>
| _TypeRef<_TypeKind.Enum>
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ Success
output
:
I@17/9
String
_TypeRef
<
I@26/9
_TypeKind.Output
>
!OF I@9/10
modifiers
:
Expand Down
Loading

0 comments on commit d9c893b

Please sign in to comment.