示例#1
0
        private Schema.Types.Block AddAttrs(Schema.Types.Block block, Type t)
        {
            foreach (var p in t.GetProperties(BindingFlags.Public | BindingFlags.Instance))
            {
                var attrAttr = p.GetCustomAttribute <TFAttributeAttribute>();
                if (attrAttr == null)
                {
                    continue;
                }

                var propType = p.PropertyType;

                if (attrAttr.Nested)
                {
                    block.BlockTypes.Add(AddNested(p));
                }
                else
                {
                    block.Attributes.Add(new Schema.Types.Attribute
                    {
                        Name        = attrAttr.Name ?? string.Empty,
                        Type        = TypeMapper.From(propType),
                        Description = attrAttr.Description ?? string.Empty,
                        Computed    = attrAttr.Computed,
                        Optional    = attrAttr.Optional,
                        Required    = attrAttr.Required,
                        Sensitive   = attrAttr.Sensitive,
                    });
                }
            }

            return(block);
        }
示例#2
0
        public static Schema BuildSchema <T>()
        {
            var properties = typeof(T).GetProperties();

            var block = new Schema.Types.Block();

            foreach (var property in properties)
            {
                var key         = property.GetCustomAttribute <MessagePack.KeyAttribute>();
                var description = property.GetCustomAttribute <DescriptionAttribute>();
                var required    = property.GetCustomAttribute <RequiredAttribute>() != null;
                var computed    = property.GetCustomAttribute <ComputedAttribute>() != null;

                block.Attributes.Add(new Schema.Types.Attribute
                {
                    Name        = key.StringKey,
                    Type        = ByteString.CopyFromUtf8($"\"{GetTerraformType(property.PropertyType)}\""),
                    Description = description.Description,
                    Optional    = !required,
                    Required    = required,
                    Computed    = computed,
                });
            }

            return(new Schema
            {
                Version = 0,
                Block = block,
            });
        }
示例#3
0
    public Schema BuildSchema(Type type)
    {
        var schemaVersionAttribute = type.GetCustomAttribute <SchemaVersionAttribute>();

        if (schemaVersionAttribute == null)
        {
            _logger.LogWarning($"Missing {nameof(SchemaVersionAttribute)} when generating schema for {type.FullName}.");
        }

        var properties = type.GetProperties();

        var block = new Schema.Types.Block();

        foreach (var property in properties)
        {
            var key         = property.GetCustomAttribute <MessagePack.KeyAttribute>();
            var description = property.GetCustomAttribute <DescriptionAttribute>();
            var required    = IsRequiredAttribute(property);
            var computed    = property.GetCustomAttribute <ComputedAttribute>() != null;

            block.Attributes.Add(new Schema.Types.Attribute
            {
                Name        = key.StringKey,
                Type        = ByteString.CopyFromUtf8(GetTerraformType(property.PropertyType)),
                Description = description.Description,
                Optional    = !required,
                Required    = required,
                Computed    = computed,
            });
        }

        return(new Schema
        {
            Version = schemaVersionAttribute?.SchemaVersion ?? 0,
            Block = block,
        });
    }