private static TypeInfo CreateTypeInfo(Type type) { TypeInfo result; if (type == typeof(void)) { result = new VoidTypeInfo(); } else if (type.GetMethod("Parse", new[] { typeof(string) }) != null && type.GetMethod("Parse", new[] { typeof(string) }).ReturnType == type) { result = new ParseableTypeInfo(type); } else if (type.IsArray) { result = new ArrayTypeInfo(type); } else if (type.IsEnum) { result = new EnumTypeInfo(type); } else if (type.ContainsGenericParameters) { result = new ReflectedTypeInfo(type); } else if (OPTIMIZED_TYPES.Contains(type)) { result = new OptimizedTypeInfo(type); } else { result = new ReflectedTypeInfo(type); } return(result); }
private static void MergeNextType( ISchemaMergeContext context, List <EnumTypeInfo> notMerged) { EnumTypeInfo left = notMerged[0]; var leftValueSet = new HashSet <string>( left.Definition.Values.Select(t => t.Name.Value)); var readyToMerge = new List <EnumTypeInfo>(); readyToMerge.Add(left); for (int i = 1; i < notMerged.Count; i++) { if (CanBeMerged(leftValueSet, notMerged[i].Definition)) { readyToMerge.Add(notMerged[i]); } } MergeType(context, readyToMerge); notMerged.RemoveAll(readyToMerge.Contains); }
public ParseStatus ParseTypeSymbol( ITypeSymbol typeSymbol, bool isReadonly, out ITypeInfo typeInfo) { this.ParseNullable_(ref typeSymbol, out var isNullable); var primitiveType = SchemaPrimitiveTypesUtil.GetPrimitiveTypeFromTypeSymbol( typeSymbol); if (primitiveType != SchemaPrimitiveType.UNDEFINED) { switch (primitiveType) { case SchemaPrimitiveType.BOOLEAN: { typeInfo = new BoolTypeInfo( typeSymbol, isReadonly, isNullable); return(ParseStatus.SUCCESS); } case SchemaPrimitiveType.BYTE: case SchemaPrimitiveType.SBYTE: case SchemaPrimitiveType.INT16: case SchemaPrimitiveType.UINT16: case SchemaPrimitiveType.INT32: case SchemaPrimitiveType.UINT32: case SchemaPrimitiveType.INT64: case SchemaPrimitiveType.UINT64: { typeInfo = new IntegerTypeInfo( typeSymbol, SchemaTypeKind.INTEGER, SchemaPrimitiveTypesUtil.ConvertNumberToInt( SchemaPrimitiveTypesUtil .ConvertPrimitiveToNumber(primitiveType)), isReadonly, isNullable); return(ParseStatus.SUCCESS); } case SchemaPrimitiveType.SN8: case SchemaPrimitiveType.UN8: case SchemaPrimitiveType.SN16: case SchemaPrimitiveType.UN16: case SchemaPrimitiveType.SINGLE: case SchemaPrimitiveType.DOUBLE: { typeInfo = new FloatTypeInfo( typeSymbol, SchemaTypeKind.FLOAT, SchemaPrimitiveTypesUtil .ConvertPrimitiveToNumber(primitiveType), isReadonly, isNullable); return(ParseStatus.SUCCESS); } case SchemaPrimitiveType.CHAR: { typeInfo = new CharTypeInfo( typeSymbol, isReadonly, isNullable); return(ParseStatus.SUCCESS); } case SchemaPrimitiveType.ENUM: { typeInfo = new EnumTypeInfo( typeSymbol, isReadonly, isNullable); return(ParseStatus.SUCCESS); } default: throw new ArgumentOutOfRangeException(); } } if (typeSymbol.SpecialType == SpecialType.System_String) { typeInfo = new StringTypeInfo( typeSymbol, isReadonly, isNullable); return(ParseStatus.SUCCESS); } if (typeSymbol.SpecialType is SpecialType .System_Collections_Generic_IReadOnlyList_T) { var listTypeSymbol = typeSymbol as INamedTypeSymbol; var containedTypeSymbol = listTypeSymbol.TypeArguments[0]; var containedParseStatus = this.ParseTypeSymbol( containedTypeSymbol, true, out var containedTypeInfo); if (containedParseStatus != ParseStatus.SUCCESS) { typeInfo = default; return(containedParseStatus); } typeInfo = new SequenceTypeInfo( typeSymbol, isReadonly, isNullable, false, isReadonly, containedTypeInfo); return(ParseStatus.SUCCESS); } if (typeSymbol.TypeKind is TypeKind.Array) { var arrayTypeSymbol = typeSymbol as IArrayTypeSymbol; var containedTypeSymbol = arrayTypeSymbol.ElementType; var containedParseStatus = this.ParseTypeSymbol( containedTypeSymbol, false, out var containedTypeInfo); if (containedParseStatus != ParseStatus.SUCCESS) { typeInfo = default; return(containedParseStatus); } typeInfo = new SequenceTypeInfo( typeSymbol, isReadonly, isNullable, true, isReadonly, containedTypeInfo); return(ParseStatus.SUCCESS); } if (typeSymbol.SpecialType is SpecialType .System_Collections_Generic_IList_T) { var listTypeSymbol = typeSymbol as INamedTypeSymbol; var containedTypeSymbol = listTypeSymbol.TypeArguments[0]; var containedParseStatus = this.ParseTypeSymbol( containedTypeSymbol, false, out var containedTypeInfo); if (containedParseStatus != ParseStatus.SUCCESS) { typeInfo = default; return(containedParseStatus); } typeInfo = new SequenceTypeInfo( typeSymbol, isReadonly, isNullable, false, false, containedTypeInfo); return(ParseStatus.SUCCESS); } if (typeSymbol is INamedTypeSymbol namedTypeSymbol) { typeInfo = new StructureTypeInfo( namedTypeSymbol, isReadonly, isNullable); return(ParseStatus.SUCCESS); } if (typeSymbol is ITypeParameterSymbol typeParameterSymbol) { var constraintTypeInfos = typeParameterSymbol .ConstraintTypes .Select(constraintType => { var parseStatus = this.ParseTypeSymbol( constraintType, isReadonly, out var constraintTypeInfo); Asserts.Equal(ParseStatus.SUCCESS, parseStatus); return(constraintTypeInfo); }) .ToArray(); typeInfo = new GenericTypeInfo( constraintTypeInfos, typeParameterSymbol, isReadonly, isNullable); return(ParseStatus.SUCCESS); } typeInfo = default; return(ParseStatus.NOT_IMPLEMENTED); }