From 7f47c60fb043a3f9deba8e332107d7cca75ba903 Mon Sep 17 00:00:00 2001 From: Prerak Mann Date: Tue, 27 Jun 2023 00:33:49 +0530 Subject: [PATCH] Refactoring 1 --- CHANGELOG.md | 2 +- ffigen.schema.json | 1 + lib/src/config_provider/config.dart | 137 ++++++++++++++-------------- lib/src/config_provider/schema.dart | 36 ++++---- 4 files changed, 90 insertions(+), 86 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0f7c5eb5..c6919af9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ # 9.0.0 -- Updated validation logic to be more strict. +- Added a JSON schema for FFIgen config files. # 8.0.2 diff --git a/ffigen.schema.json b/ffigen.schema.json index 708fe621..a557a87c 100644 --- a/ffigen.schema.json +++ b/ffigen.schema.json @@ -1,5 +1,6 @@ { "$id": "https://json.schemastore.org/ffigen", + "$comment": "This file is generated. To regenerate run: dart tool/generate_json_schema.dart in github.com/dart-lang/ffigen", "$schema": "https://json-schema.org/draft/2020-12/schema", "type": "object", "additionalProperties": false, diff --git a/lib/src/config_provider/config.dart b/lib/src/config_provider/config.dart index b5ccb4b3..fe0b58e4 100644 --- a/lib/src/config_provider/config.dart +++ b/lib/src/config_provider/config.dart @@ -222,7 +222,7 @@ class Config { Schema _getRootSchema() { return FixedMapSchema( keys: [ - FixedMapKey( + FixedMapEntry( key: strings.llvmPath, valueSchema: ListSchema( childSchema: StringSchema(), @@ -231,7 +231,7 @@ class Config { defaultValue: (node) => findDylibAtDefaultLocations(), resultOrDefault: (node) => _libclangDylib = node.value as String, ), - FixedMapKey( + FixedMapEntry( key: strings.output, required: true, valueSchema: OneOfSchema( @@ -246,7 +246,7 @@ class Config { _symbolFile = (node.value as OutputConfig).symbolFile; }, )), - FixedMapKey( + FixedMapEntry( key: strings.language, valueSchema: EnumSchema( allowedValues: {strings.langC, strings.langObjC}, @@ -264,17 +264,17 @@ class Config { defaultValue: (node) => Language.c, resultOrDefault: (node) => _language = node.value as Language, ), - FixedMapKey( + FixedMapEntry( key: strings.headers, required: true, valueSchema: FixedMapSchema>( keys: [ - FixedMapKey( + FixedMapEntry( key: strings.entryPoints, valueSchema: ListSchema(childSchema: StringSchema()), required: true, ), - FixedMapKey( + FixedMapEntry( key: strings.includeDirectives, valueSchema: ListSchema(childSchema: StringSchema()), ), @@ -282,7 +282,7 @@ class Config { transform: (node) => headersExtractor(node.value, filename), result: (node) => _headers = node.value as Headers, )), - FixedMapKey( + FixedMapEntry( key: strings.compilerOpts, valueSchema: OneOfSchema>( childSchemas: [ @@ -296,15 +296,15 @@ class Config { defaultValue: (node) => [], resultOrDefault: (node) => _compilerOpts = node.value as List, ), - FixedMapKey( + FixedMapEntry( key: strings.compilerOptsAuto, valueSchema: FixedMapSchema( keys: [ - FixedMapKey( + FixedMapEntry( key: strings.macos, valueSchema: FixedMapSchema( keys: [ - FixedMapKey( + FixedMapEntry( key: strings.includeCStdLib, valueSchema: BoolSchema(), defaultValue: (node) => true, @@ -320,7 +320,7 @@ class Config { result: (node) => _compilerOpts.addAll( (node.value as CompilerOptsAuto).extractCompilerOpts()), )), - FixedMapKey( + FixedMapEntry( key: strings.libraryImports, valueSchema: DynamicMapSchema( keyValueSchemas: [ @@ -333,29 +333,29 @@ class Config { resultOrDefault: (node) => _libraryImports = (node.value) as Map, ), - FixedMapKey( + FixedMapEntry( key: strings.functions, valueSchema: FixedMapSchema( keys: [ ..._includeExcludeProperties(), ..._renameProperties(), ..._memberRenameProperties(), - FixedMapKey( + FixedMapEntry( key: strings.symbolAddress, valueSchema: _includeExcludeObject(), defaultValue: (node) => Includer.excludeByDefault(), ), - FixedMapKey( + FixedMapEntry( key: strings.exposeFunctionTypedefs, valueSchema: _includeExcludeObject(), defaultValue: (node) => Includer.excludeByDefault(), ), - FixedMapKey( + FixedMapEntry( key: strings.leafFunctions, valueSchema: _includeExcludeObject(), defaultValue: (node) => Includer.excludeByDefault(), ), - FixedMapKey( + FixedMapEntry( key: strings.varArgFunctions, valueSchema: _functionVarArgsSchema(), defaultValue: (node) => >{}, @@ -375,7 +375,7 @@ class Config { (node.value as Map)[strings.leafFunctions] as Includer; }, )), - FixedMapKey( + FixedMapEntry( key: strings.structs, valueSchema: FixedMapSchema( keys: [ @@ -383,7 +383,7 @@ class Config { ..._renameProperties(), ..._memberRenameProperties(), _dependencyOnlyFixedMapKey(), - FixedMapKey( + FixedMapEntry( key: strings.structPack, valueSchema: DynamicMapSchema( keyValueSchemas: [ @@ -411,7 +411,7 @@ class Config { as Map)[strings.dependencyOnly] as CompoundDependencies; }, )), - FixedMapKey( + FixedMapEntry( key: strings.unions, valueSchema: FixedMapSchema( keys: [ @@ -427,7 +427,7 @@ class Config { as CompoundDependencies; }, )), - FixedMapKey( + FixedMapEntry( key: strings.enums, valueSchema: FixedMapSchema( keys: [ @@ -440,7 +440,7 @@ class Config { node.value as Map); }, )), - FixedMapKey( + FixedMapEntry( key: strings.unnamedEnums, valueSchema: FixedMapSchema( keys: [ @@ -452,13 +452,13 @@ class Config { node.value as Map); }, )), - FixedMapKey( + FixedMapEntry( key: strings.globals, valueSchema: FixedMapSchema( keys: [ ..._includeExcludeProperties(), ..._renameProperties(), - FixedMapKey( + FixedMapEntry( key: strings.symbolAddress, valueSchema: _includeExcludeObject(), defaultValue: (node) => Includer.excludeByDefault(), @@ -469,7 +469,7 @@ class Config { node.value as Map); }, )), - FixedMapKey( + FixedMapEntry( key: strings.macros, valueSchema: FixedMapSchema( keys: [ @@ -481,7 +481,7 @@ class Config { node.value as Map); }, )), - FixedMapKey( + FixedMapEntry( key: strings.typedefs, valueSchema: FixedMapSchema( keys: [ @@ -493,14 +493,14 @@ class Config { node.value as Map); }, )), - FixedMapKey( + FixedMapEntry( key: strings.objcInterfaces, valueSchema: FixedMapSchema( keys: [ ..._includeExcludeProperties(), ..._renameProperties(), ..._memberRenameProperties(), - FixedMapKey( + FixedMapEntry( key: strings.objcModule, valueSchema: _objcInterfaceModuleObject(), defaultValue: (node) => ObjCModulePrefixer({}), @@ -513,11 +513,11 @@ class Config { as ObjCModulePrefixer; }, )), - FixedMapKey( + FixedMapEntry( key: strings.import, valueSchema: FixedMapSchema( keys: [ - FixedMapKey( + FixedMapEntry( key: strings.symbolFilesImport, valueSchema: ListSchema( childSchema: StringSchema(), @@ -530,79 +530,80 @@ class Config { ) ], )), - FixedMapKey( + FixedMapEntry( key: strings.typeMap, valueSchema: FixedMapSchema( keys: [ - FixedMapKey( + FixedMapEntry( key: strings.typeMapTypedefs, valueSchema: _mappedTypeObject(), defaultValue: (node) => >{}, ), - FixedMapKey( + FixedMapEntry( key: strings.typeMapStructs, valueSchema: _mappedTypeObject(), defaultValue: (node) => >{}, ), - FixedMapKey( + FixedMapEntry( key: strings.typeMapUnions, valueSchema: _mappedTypeObject(), defaultValue: (node) => >{}, ), - FixedMapKey( + FixedMapEntry( key: strings.typeMapNativeTypes, valueSchema: _mappedTypeObject(), defaultValue: (node) => >{}, ), ], result: (node) { + final nodeValue = node.value as Map; _typedefTypeMappings = makeImportTypeMapping( - (node.value[strings.typeMapTypedefs]) + (nodeValue[strings.typeMapTypedefs]) as Map>, _libraryImports, ); _structTypeMappings = makeImportTypeMapping( - (node.value[strings.typeMapStructs]) + (nodeValue[strings.typeMapStructs]) as Map>, _libraryImports, ); _unionTypeMappings = makeImportTypeMapping( - (node.value[strings.typeMapUnions]) + (nodeValue[strings.typeMapUnions]) as Map>, _libraryImports, ); _nativeTypeMappings = makeImportTypeMapping( - (node.value[strings.typeMapNativeTypes]) + (nodeValue[strings.typeMapNativeTypes]) as Map>, _libraryImports, ); }, )), - FixedMapKey( + FixedMapEntry( key: strings.excludeAllByDefault, valueSchema: BoolSchema(), defaultValue: (node) => false, resultOrDefault: (node) => _excludeAllByDefault = node.value as bool, ), - FixedMapKey( + FixedMapEntry( key: strings.sort, valueSchema: BoolSchema(), defaultValue: (node) => false, resultOrDefault: (node) => _sort = node.value as bool, ), - FixedMapKey( + FixedMapEntry( key: strings.useSupportedTypedefs, valueSchema: BoolSchema(), defaultValue: (node) => true, resultOrDefault: (node) => _useSupportedTypedefs = node.value as bool, ), - FixedMapKey( + FixedMapEntry( key: strings.comments, valueSchema: _commentSchema(), defaultValue: (node) => CommentType.def(), resultOrDefault: (node) => _commentType = node.value as CommentType, ), - FixedMapKey( + FixedMapEntry( key: strings.name, valueSchema: _dartClassNameStringSchema(), defaultValue: (node) { @@ -612,7 +613,7 @@ class Config { }, resultOrDefault: (node) => _wrapperName = node.value as String, ), - FixedMapKey( + FixedMapEntry( key: strings.description, valueSchema: _nonEmptyStringSchema(), defaultValue: (node) { @@ -622,25 +623,25 @@ class Config { }, resultOrDefault: (node) => _wrapperDocComment = node.value as String?, ), - FixedMapKey( + FixedMapEntry( key: strings.preamble, valueSchema: StringSchema( result: (node) => _preamble = node.value as String?, )), - FixedMapKey( + FixedMapEntry( key: strings.useDartHandle, valueSchema: BoolSchema(), defaultValue: (node) => true, resultOrDefault: (node) => _useDartHandle = node.value as bool, ), - FixedMapKey( + FixedMapEntry( key: strings.ffiNative, valueSchema: OneOfSchema( childSchemas: [ EnumSchema(allowedValues: {null}), FixedMapSchema( keys: [ - FixedMapKey( + FixedMapEntry( key: strings.ffiNativeAsset, valueSchema: StringSchema(), required: true, @@ -681,7 +682,7 @@ class Config { ), FixedMapSchema( keys: [ - FixedMapKey( + FixedMapEntry( key: strings.style, valueSchema: EnumSchema( allowedValues: {strings.doxygen, strings.any}, @@ -691,7 +692,7 @@ class Config { ), defaultValue: (node) => CommentStyle.doxygen, ), - FixedMapKey( + FixedMapEntry( key: strings.length, valueSchema: EnumSchema( allowedValues: {strings.brief, strings.full}, @@ -722,13 +723,13 @@ class Config { ListSchema(childSchema: StringSchema()), FixedMapSchema( keys: [ - FixedMapKey( + FixedMapEntry( key: strings.types, valueSchema: ListSchema(childSchema: StringSchema()), required: true, ), - FixedMapKey( + FixedMapEntry( key: strings.postfix, valueSchema: StringSchema(), ), @@ -746,21 +747,21 @@ class Config { FixedMapSchema _outputFullSchema() { return FixedMapSchema( keys: [ - FixedMapKey( + FixedMapEntry( key: strings.bindings, valueSchema: _filePathStringSchema(), required: true, ), - FixedMapKey( + FixedMapEntry( key: strings.symbolFile, valueSchema: FixedMapSchema( keys: [ - FixedMapKey( + FixedMapEntry( key: strings.output, valueSchema: _filePathStringSchema(), required: true, ), - FixedMapKey( + FixedMapEntry( key: strings.importPath, valueSchema: StringSchema(), required: true, @@ -794,13 +795,13 @@ class Config { ); } - List _includeExcludeProperties() { + List _includeExcludeProperties() { return [ - FixedMapKey( + FixedMapEntry( key: strings.include, valueSchema: _fullMatchOrRegexpList(), ), - FixedMapKey( + FixedMapEntry( key: strings.exclude, valueSchema: _fullMatchOrRegexpList(), defaultValue: (node) => [], @@ -815,9 +816,9 @@ class Config { ); } - List _renameProperties() { + List _renameProperties() { return [ - FixedMapKey( + FixedMapEntry( key: strings.rename, valueSchema: DynamicMapSchema( schemaDefName: "rename", @@ -829,9 +830,9 @@ class Config { ]; } - List _memberRenameProperties() { + List _memberRenameProperties() { return [ - FixedMapKey( + FixedMapEntry( key: strings.memberRename, valueSchema: DynamicMapSchema>( schemaDefName: "memberRename", @@ -860,8 +861,8 @@ class Config { ); } - FixedMapKey _dependencyOnlyFixedMapKey() { - return FixedMapKey( + FixedMapEntry _dependencyOnlyFixedMapKey() { + return FixedMapEntry( key: strings.dependencyOnly, valueSchema: EnumSchema( schemaDefName: "dependencyOnly", @@ -884,9 +885,9 @@ class Config { ( keyRegexp: ".*", valueSchema: FixedMapSchema(keys: [ - FixedMapKey(key: strings.lib, valueSchema: StringSchema()), - FixedMapKey(key: strings.cType, valueSchema: StringSchema()), - FixedMapKey(key: strings.dartType, valueSchema: StringSchema()), + FixedMapEntry(key: strings.lib, valueSchema: StringSchema()), + FixedMapEntry(key: strings.cType, valueSchema: StringSchema()), + FixedMapEntry(key: strings.dartType, valueSchema: StringSchema()), ]), ) ], diff --git a/lib/src/config_provider/schema.dart b/lib/src/config_provider/schema.dart index 321c08d6..cfd4e979 100644 --- a/lib/src/config_provider/schema.dart +++ b/lib/src/config_provider/schema.dart @@ -21,17 +21,17 @@ class SchemaNode { /// Contains the raw underlying node value. Would be null for fields populated /// but default values - final dynamic rawValue; + final Object? rawValue; SchemaNode({ required this.path, required this.value, - dynamic rawValue, + Object? rawValue, bool nullRawValue = false, }) : rawValue = nullRawValue ? null : (rawValue ?? value); /// Copy object with a different value. - SchemaNode withValue(T value, dynamic rawValue) { + SchemaNode withValue(T value, Object? rawValue) { return SchemaNode( path: path, value: value, @@ -82,7 +82,7 @@ class SchemaExtractionError extends Error { } /// Base class for all Schemas to extend. -abstract class Schema { +abstract class Schema { /// Used to generate and refer the reference definition generated in json /// schema. Must be unique for a nested Schema. String? schemaDefName; @@ -98,7 +98,7 @@ abstract class Schema { dynamic Function(SchemaNode node)? transform; /// Called when final result is prepared via [_extractNode]. - void Function(SchemaNode node)? result; + void Function(SchemaNode node)? result; Schema({ required this.schemaDefName, required this.schemaDescription, @@ -128,6 +128,8 @@ abstract class Schema { final schemaMap = _generateJsonSchemaNode(defs); return { r"$id": schemaId, + r"$comment": + "This file is generated. To regenerate run: dart tool/generate_json_schema.dart in github.com/dart-lang/ffigen", r"$schema": "https://json-schema.org/draft/2020-12/schema", ...schemaMap, r"$defs": defs, @@ -148,14 +150,14 @@ abstract class Schema { } } -class FixedMapKey { +class FixedMapEntry { final String key; final Schema valueSchema; final dynamic Function(SchemaNode o)? defaultValue; void Function(SchemaNode node)? resultOrDefault; final bool required; - FixedMapKey({ + FixedMapEntry({ required this.key, required this.valueSchema, this.defaultValue, @@ -165,11 +167,11 @@ class FixedMapKey { } /// Schema for a Map which has a fixed set of known keys. -class FixedMapSchema extends Schema> { - final List keys; +class FixedMapSchema extends Schema> { + final List keys; final Set allKeys; final Set requiredKeys; - final bool unknownWarning; + final bool additionalProperties; FixedMapSchema({ required this.keys, @@ -178,7 +180,7 @@ class FixedMapSchema extends Schema> { super.customValidation, super.transform, super.result, - this.unknownWarning = true, + this.additionalProperties = false, }) : requiredKeys = { for (final kv in keys.where((kv) => kv.required)) kv.key }, @@ -215,7 +217,7 @@ class FixedMapSchema extends Schema> { } } - if (unknownWarning && log) { + if (!additionalProperties && log) { for (final key in inputMap.keys) { if (!allKeys.contains(key)) { _logger.severe("Unknown key - '${[...o.path, key].join(' -> ')}'."); @@ -315,7 +317,7 @@ class FixedMapSchema extends Schema> { Map _generateJsonSchemaNode(Map defs) { return { "type": "object", - if (unknownWarning) "additionalProperties": false, + if (!additionalProperties) "additionalProperties": false, if (schemaDescription != null) "description": schemaDescription!, if (keys.isNotEmpty) "properties": { @@ -328,7 +330,7 @@ class FixedMapSchema extends Schema> { } /// Schema for a Map that can have any number of keys. -class DynamicMapSchema extends Schema> { +class DynamicMapSchema extends Schema> { /// [keyRegexp] will convert it's input to a String before matching. final List<({String keyRegexp, Schema valueSchema})> keyValueSchemas; @@ -438,7 +440,7 @@ class DynamicMapSchema extends Schema> { } /// Schema for a List. -class ListSchema extends Schema> { +class ListSchema extends Schema> { final Schema childSchema; ListSchema({ @@ -594,7 +596,7 @@ class IntSchema extends Schema { } /// Schema for an object where only specific values are allowed. -class EnumSchema extends Schema { +class EnumSchema extends Schema { Set allowedValues; EnumSchema({ required this.allowedValues, @@ -680,7 +682,7 @@ class BoolSchema extends Schema { } /// Schema which checks if atleast one of the underlying Schema matches. -class OneOfSchema extends Schema { +class OneOfSchema extends Schema { final List childSchemas; OneOfSchema({