Пример #1
0
        public void WriteField(CodeWriter writer, BaseSchemaMember schemaDefinition)
        {
            ErrorContext.Current.WithScope(this.Name, () =>
            {
                bool isVector = this.VectorType != VectorType.None;
                EnumDefinition enumDefinition = null;

                if (schemaDefinition.TryResolveName(this.FbsFieldType, out var typeDefinition))
                {
                    enumDefinition = typeDefinition as EnumDefinition;
                }

                string defaultValue = string.Empty;
                string clrType;
                bool isPrimitive = SchemaDefinition.TryResolveBuiltInScalarType(this.FbsFieldType, out IBuiltInScalarType builtInType);

                if (isPrimitive)
                {
                    clrType = builtInType.CSharpTypeName;
                }
                else
                {
                    clrType = typeDefinition?.GlobalName ?? this.FbsFieldType;
                }

                if (!string.IsNullOrEmpty(this.DefaultValue))
                {
                    if (isPrimitive)
                    {
                        defaultValue = builtInType.FormatLiteral(this.DefaultValue);
                    }
                    else if (enumDefinition != null)
                    {
                        if (enumDefinition.NameValuePairs.ContainsKey(this.DefaultValue))
                        {
                            // Referenced by name.
                            defaultValue = $"{clrType}.{this.DefaultValue}";
                        }
                        else
                        {
                            defaultValue = $"({clrType}){enumDefinition.UnderlyingType.FormatLiteral(this.DefaultValue)}";
                        }
                    }
                    else
                    {
                        ErrorContext.Current?.RegisterError($"Only primitive types and enums may have default values. Field '{this.Name}' declares a default value but has type '{this.FbsFieldType}'.");
                    }
                }

                this.WriteField(writer, this.GetClrTypeName(schemaDefinition), defaultValue, this.Name);
            });
        }
Пример #2
0
        public string GetClrTypeName(BaseSchemaMember baseMember)
        {
            string clrType;

            if (SchemaDefinition.TryResolveBuiltInScalarType(this.FbsFieldType, out IBuiltInScalarType builtInType))
            {
                clrType = builtInType.CSharpTypeName;
            }
            else
            {
                if (baseMember.TryResolveName(this.FbsFieldType, out var typeDefinition))
                {
                    if (typeDefinition is UnionDefinition unionDef)
                    {
                        clrType = unionDef.ClrTypeName;
                    }
                    else
                    {
                        clrType = typeDefinition.GlobalName;
                    }
                }
                else
                {
                    clrType = this.FbsFieldType;
                }
            }

            switch (this.VectorType)
            {
            case VectorType.Array:
                return($"{clrType}[]");

            case VectorType.IList:
                return($"IList<{clrType}>");

            case VectorType.IReadOnlyList:
                return($"IReadOnlyList<{clrType}>");

            case VectorType.Memory:
                return($"Memory<{clrType}>");

            case VectorType.ReadOnlyMemory:
                return($"ReadOnlyMemory<{clrType}>");

            case VectorType.None:
                return(clrType);

            default:
                throw new InvalidOperationException($"Unexpected value for vectortype: '{this.VectorType}'");
            }
        }
Пример #3
0
        public void WriteCopyConstructorLine(CodeWriter writer, string sourceName, BaseSchemaMember parent)
        {
            bool isBuiltIn = SchemaDefinition.TryResolve(this.FbsFieldType, out _);

            bool foundNodeType = parent.TryResolveName(this.FbsFieldType, out var nodeType);

            if (!isBuiltIn && !foundNodeType)
            {
                ErrorContext.Current.RegisterError($"Unable to resolve type '{this.FbsFieldType}' as a built in or defined type");
                return;
            }

            string selectStatement = this.GetLinqSelectStatement(isBuiltIn, nodeType);

            switch (this.VectorType)
            {
            case VectorType.IList:
            case VectorType.IReadOnlyList:
                writer.AppendLine($"this.{this.Name} = {sourceName}.{this.Name}?{selectStatement}.ToList();");
                break;

            case VectorType.Array:
                writer.AppendLine($"this.{this.Name} = {sourceName}.{this.Name}?{selectStatement}.ToArray();");
                break;

            case VectorType.Memory:
            case VectorType.ReadOnlyMemory:
                writer.AppendLine($"this.{this.Name} = {sourceName}.{this.Name}.ToArray();");
                break;

            case VectorType.IIndexedVector:
                writer.AppendLine($"this.{this.Name} = {sourceName}.{this.Name}?.Clone(x => {nodeType.GetCopyExpression("x")});");
                break;

            case VectorType.None:
            {
                if (isBuiltIn)
                {
                    writer.AppendLine($"this.{this.Name} = {sourceName}.{this.Name};");
                }
                else
                {
                    string cloneStatement = nodeType.GetCopyExpression($"{sourceName}.{this.Name}");
                    writer.AppendLine($"this.{this.Name} = {cloneStatement};");
                }
            }
            break;
            }
        }
Пример #4
0
        public void WriteField(CodeWriter writer, BaseSchemaMember schemaDefinition)
        {
            ErrorContext.Current.WithScope(this.Name, (Action)(() =>
            {
                bool isVector = this.VectorType != VectorType.None;
                EnumDefinition enumDefinition = null;

                if (schemaDefinition.TryResolveName(this.FbsFieldType, out var typeDefinition))
                {
                    enumDefinition = typeDefinition as EnumDefinition;
                }

                string defaultValue = string.Empty;
                string clrType;
                bool isBuiltInType = SchemaDefinition.TryResolve(this.FbsFieldType, out ITypeModel builtInType);

                if (isBuiltInType)
                {
                    clrType = builtInType.ClrType.FullName;
                }
                else
                {
                    clrType = typeDefinition?.GlobalName ?? this.FbsFieldType;
                }

                if (!string.IsNullOrEmpty(this.DefaultValue))
                {
                    if (isBuiltInType && builtInType.TryFormatStringAsLiteral(this.DefaultValue, out defaultValue))
                    {
                        // intentionally left blank.
                    }
                    else if (enumDefinition?.NameValuePairs.ContainsKey(this.DefaultValue) == true)
                    {
                        // Also ok.
                        defaultValue = $"{clrType}.{this.DefaultValue}";
                    }
                    else if (enumDefinition?.UnderlyingType.TryFormatStringAsLiteral(this.DefaultValue, out defaultValue) == true)
                    {
                        defaultValue = $"({clrType})({defaultValue})";
                    }
                    else
                    {
                        ErrorContext.Current?.RegisterError($"Only primitive types and enums may have default values. Field '{this.Name}' declares a default value but has type '{this.FbsFieldType}'.");
                    }
                }

                this.WriteField(writer, this.GetClrTypeName(schemaDefinition), defaultValue, this.Name);
            }));
        }
Пример #5
0
        public string GetClrTypeName(BaseSchemaMember baseMember)
        {
            string clrType;
            string sortKeyType = null;

            if (SchemaDefinition.TryResolve(this.FbsFieldType, out ITypeModel builtInType))
            {
                clrType = builtInType.ClrType.FullName;
            }
            else
            {
                if (baseMember.TryResolveName(this.FbsFieldType, out var typeDefinition))
                {
                    if (typeDefinition is UnionDefinition unionDef)
                    {
                        clrType = unionDef.ClrTypeName;
                    }
                    else
                    {
                        clrType = typeDefinition.GlobalName;
                    }

                    if (typeDefinition is TableOrStructDefinition tableOrStruct && tableOrStruct.IsTable)
                    {
                        sortKeyType = tableOrStruct.Fields.FirstOrDefault(x => x.IsKey)?.GetClrTypeName(baseMember);
                    }
                }
                else
                {
                    clrType = this.FbsFieldType;
                }
            }

            if (this.IsOptionalScalar)
            {
                // nullable.
                clrType = $"System.Nullable<{clrType}>";
            }
            else if (this.SharedString)
            {
                clrType = $"global::{typeof(SharedString).FullName}";
            }

            switch (this.VectorType)
            {
            case VectorType.Array:
                return($"{clrType}[]");

            case VectorType.IList:
                return($"IList<{clrType}>");

            case VectorType.IReadOnlyList:
                return($"IReadOnlyList<{clrType}>");

            case VectorType.Memory:
                return($"Memory<{clrType}>");

            case VectorType.ReadOnlyMemory:
                return($"ReadOnlyMemory<{clrType}>");

            case VectorType.IIndexedVector:
                if (string.IsNullOrWhiteSpace(sortKeyType))
                {
                    ErrorContext.Current.RegisterError($"Unable to determine key type for table {clrType}. Please make sure a property has the 'Key' metadata.");
                }
                return($"IIndexedVector<{sortKeyType}, {clrType}>");

            case VectorType.None:
                return(clrType);

            default:
                throw new InvalidOperationException($"Unexpected value for vectortype: '{this.VectorType}'");
            }
        }