Example #1
0
    protected BaseReferenceTypeSchemaModel(Schema.Schema schema, FlatBufferObject table) : base(schema, table.Name, new FlatSharpAttributes(table.Attributes))
    {
        this.DeclaringFile = table.DeclarationFile;
        this.properties    = new Dictionary <int, PropertyFieldModel>();
        this.structVectors = new();
        this.table         = table;

        int previousIndex = -1;

        foreach (var field in table.Fields.OrderBy(x => x.Id))
        {
            if (PropertyFieldModel.TryCreate(this, field, previousIndex, out PropertyFieldModel? model))
            {
                previousIndex = model.Index;
                this.properties[model.Index] = model;
            }
            else if (StructVectorPropertyFieldModel.TryCreate(this, field, previousIndex, out StructVectorPropertyFieldModel? svModel))
            {
                for (int i = 0; i < svModel.Properties.Count; ++i)
                {
                    previousIndex = svModel.Properties[i].Index;
                    this.properties[previousIndex] = svModel.Properties[i];
                }

                this.structVectors.Add(svModel);
            }
        }

        this.AttributeValidator.NonVirtualValidator = (b) => AttributeValidationResult.Valid;
    }
    private TableSchemaModel(Schema.Schema schema, FlatBufferObject table) : base(schema, table)
    {
        FlatSharpInternal.Assert(table.IsStruct == false, "Not expecting struct");

        this.AttributeValidator.DeserializationOptionValidator = _ => AttributeValidationResult.Valid;
        this.AttributeValidator.DefaultConstructorValidator    = _ => AttributeValidationResult.Valid;
        this.AttributeValidator.ForceWriteValidator            = _ => AttributeValidationResult.Valid;
    }
    private void ValidateHasSerializer(FlatBufferObject obj)
    {
        FlatSharpInternal.Assert(!obj.IsStruct, "expecting only tables");
        FlatSharpAttributes attrs = new FlatSharpAttributes(obj.Attributes);

        if (attrs.DeserializationOption is null)
        {
            ErrorContext.Current.RegisterError($"RPC call '{this.fullName}' uses table '{obj.Name}', which does not specify the '{MetadataKeys.SerializerKind}' attribute.");
        }
    }
    public static bool TryCreate(Schema.Schema schema, FlatBufferObject table, [NotNullWhen(true)] out TableSchemaModel?model)
    {
        model = null;
        if (table.IsStruct)
        {
            return(false);
        }

        model = new TableSchemaModel(schema, table);
        return(true);
    }
    private ReferenceStructSchemaModel(Schema.Schema schema, FlatBufferObject @struct) : base(schema, @struct)
    {
        FlatSharpInternal.Assert(@struct.IsStruct, "Expecting struct");

        this.AttributeValidator.DefaultConstructorValidator = kind => kind switch
        {
            DefaultConstructorKind.Public or DefaultConstructorKind.PublicObsolete => AttributeValidationResult.Valid,
                                                                    _ => AttributeValidationResult.ValueInvalid,
        };

        this.AttributeValidator.WriteThroughValidator = _ => AttributeValidationResult.Valid;
    }
    public static bool TryCreate(Schema.Schema schema, FlatBufferObject @struct, [NotNullWhen(true)] out ValueStructSchemaModel?model)
    {
        model = null;
        if ([email protected])
        {
            return(false);
        }

        if (@struct.Attributes?.ContainsKey(MetadataKeys.ValueStruct) != true)
        {
            return(false);
        }

        model = new ValueStructSchemaModel(schema, @struct);
        return(true);
    }
    private ValueStructSchemaModel(Schema.Schema schema, FlatBufferObject @struct) : base(schema, @struct.Name, new FlatSharpAttributes(@struct.Attributes))
    {
        FlatSharpInternal.Assert(@struct.IsStruct, "Expecting struct");
        FlatSharpInternal.Assert(this.Attributes.ValueStruct == true, "Expecting value struct");

        this.@struct       = @struct;
        this.fields        = new();
        this.structVectors = new();

        foreach (Field field in [email protected](x => x.Id))
        {
            IFlatSharpAttributes attrs = new FlatSharpAttributes(field.Attributes);

            string fieldType = field.Type.ResolveTypeOrElementTypeName(schema, this.Attributes);
            if (field.Type.BaseType == BaseType.Array)
            {
                // struct vector
                int size = field.Type.ElementType.GetScalarSize();

                List <string> vectorFields = new();
                for (int i = 0; i < field.Type.FixedLength; ++i)
                {
                    string name = $"__flatsharp__{field.Name}_{i}";

                    MutableFlatSharpAttributes tempAttrs = new MutableFlatSharpAttributes(attrs)
                    {
                        UnsafeStructVector = null,
                    };

                    vectorFields.Add(name);
                    this.fields.Add(new(field.Offset + (i * size), name, "private", fieldType, $"{field.Name}({i})", null, this, tempAttrs));
                }

                this.structVectors.Add(new(fieldType, field.Name, vectorFields, this, field.Documentation, attrs));
            }
            else
            {
                this.fields.Add(new(field.Offset, field.Name, "public", fieldType, field.Name, field.Documentation, this, attrs));
            }
        }

        this.AttributeValidator.MemoryMarshalValidator = _ => AttributeValidationResult.Valid;
    }