// From Value to string (serialization) private string GetValuesMap(ITypeSymbol type) { var typeName = type.GetDeclarationGenericFullName(); string[] values; if (type.FindAttribute("blabla") != null) { values = type .GetMembers() .OfType <IFieldSymbol>() // If the enum have multiple fields with the same value (eg. BlaBlaEnum.Default = BlaBlaEnum.HellWorld), the generated dictionary will be valid // and will throw a TypeInitializationException or ArgumentException. So keep only the first occurence. .Distinct(FuncEqualityComparer <IFieldSymbol> .Create(field => field.ConstantValue)) .Select(field => $@"{{{typeName}.{field.Name}, ""{field.ConstantValue.ToString()}""}},") .ToArray(); } else { values = type .GetMembers() .OfType <IFieldSymbol>() // If the enum have multiple fields with the same value (eg. BlaBlaEnum.Default = BlaBlaEnum.HellWorld), the generated dictionary will be valid // and will throw a TypeInitializationException or ArgumentException. So keep only the first occurence. .Distinct(FuncEqualityComparer <IFieldSymbol> .Create(field => field.ConstantValue)) .Select(field => $@"{{{typeName}.{field.Name}, ""{_codeAnalyzer.GetName(field)}""}},") .ToArray(); } return($@"new Dictionary<{typeName}, string>({values.Length}) {{ {values.JoinBy(Environment.NewLine)} }}" ); }
private SerializerClassesName GetClasses(ITypeSymbol type) { var builderType = type .FindAttribute("Uno.ImmutableBuilderAttribute") .ConstructorArguments[0] .Value as ITypeSymbol; return(new SerializerClassesName { TypeName = type.GetDeclarationGenericName(), TypeFullName = type.GetDeclarationGenericFullName(), SerializerName = $"{type.GetSerializedGenericFullName()}_StaticJsonSerializer", BuilderTypeFullName = builderType.GetDeclarationGenericFullName(), BuilderSerializerName = $"{builderType.GetSerializedGenericFullName()}_StaticJsonSerializer", }); }
public bool IsResolvable(ITypeSymbol type) { var attr = type.FindAttribute("Uno.ImmutableBuilderAttribute"); return(attr != null); }
private int DetermineByteCount(ITypeSymbol valueType) { var attributeSymbol = valueType.FindAttribute <BitCountAttribute>(_context); if (attributeSymbol != null) { return((int)attributeSymbol.FindArgument("count") !.Value.Value !*4); } switch (valueType.SpecialType) { case SpecialType.System_Boolean: case SpecialType.System_Byte: case SpecialType.System_SByte: return(1); case SpecialType.System_Int16: case SpecialType.System_UInt16: return(2); case SpecialType.System_UInt32: case SpecialType.System_Int32: case SpecialType.System_Single: return(4); case SpecialType.System_UInt64: case SpecialType.System_Int64: case SpecialType.System_Double: return(8); case SpecialType.None: { // Not a special type, recursively count from the properties. var byteCount = 0; foreach (var memberSymbol in valueType.GetMembers()) { if (!(memberSymbol is IPropertySymbol propertyMemberSymbol)) { continue; } // Skip static properties if (propertyMemberSymbol.IsStatic) { continue; } // Skip the property if the getter is compiler-generated. if (!(propertyMemberSymbol.GetMethod?.HasAttribute <CompilerGeneratedAttribute>(_context) ?? false)) { continue; } byteCount += DetermineByteCount(propertyMemberSymbol.Type); } return(byteCount); } default: // Diagnostic, not handled throw new NotImplementedException("oof"); } }