Example #1
0
        private static void ProcessType(
            Type type,
            V1JSONSchemaProps props,
            IList <V1CustomResourceColumnDefinition> additionalColumns,
            string jsonPath)
        {
            props.Type = Object;

            props.Properties = new Dictionary <string, V1JSONSchemaProps>(
                type.GetProperties()
                .Where(
                    info => jsonPath != string.Empty ||
                    !IgnoredToplevelProperties.Contains(info.Name.ToLowerInvariant()))
                .Select(
                    prop => KeyValuePair.Create(
                        GetPropertyName(prop),
                        MapProperty(prop, additionalColumns, $"{jsonPath}.{GetPropertyName(prop)}"))));
            props.Required = type.GetProperties()
                             .Where(prop => prop.GetCustomAttribute <RequiredAttribute>() != null)
                             .Select(prop => GetPropertyName(prop))
                             .ToList();
            if (props.Required.Count == 0)
            {
                props.Required = null;
            }
        }
Example #2
0
 private static void SetEmbeddedResourceProperties(V1JSONSchemaProps props)
 {
     props.Type       = Object;
     props.Properties = null;
     props.XKubernetesPreserveUnknownFields = true;
     props.XKubernetesEmbeddedResource      = true;
 }
        private static V1beta1JSONSchemaProps Convert(this V1JSONSchemaProps props)
        {
            var betaProps = new V1beta1JSONSchemaProps();

            betaProps.Nullable    = props.Nullable;
            betaProps.Description = props.Description;

            if (props.ExternalDocs != null)
            {
                betaProps.ExternalDocs = new V1beta1ExternalDocumentation(
                    props.ExternalDocs.Description,
                    props.ExternalDocs.Url);
            }

            betaProps.MaxItems    = props.MaxItems;
            betaProps.MinItems    = props.MinItems;
            betaProps.UniqueItems = props.UniqueItems;

            betaProps.MaxLength = props.MaxLength;
            betaProps.MinLength = props.MinLength;

            betaProps.MultipleOf = props.MultipleOf;

            betaProps.Pattern = props.Pattern;

            betaProps.Maximum          = props.Maximum;
            betaProps.ExclusiveMaximum = props.ExclusiveMaximum;

            betaProps.Minimum          = props.Minimum;
            betaProps.ExclusiveMinimum = props.ExclusiveMinimum;

            if (props.Properties != null)
            {
                betaProps.Properties = new Dictionary <string, V1beta1JSONSchemaProps>(
                    props.Properties.Select(p => KeyValuePair.Create(p.Key, p.Value.Convert())));
            }

            betaProps.Type         = props.Type;
            betaProps.Format       = props.Format;
            betaProps.Items        = (props.Items as V1JSONSchemaProps)?.Convert();
            betaProps.Required     = props.Required;
            betaProps.EnumProperty = props.EnumProperty;

            return(betaProps);
        }
Example #4
0
        private static void ProcessType(Type type, V1JSONSchemaProps props)
        {
            props.Type = Object;

            props.Properties = new Dictionary <string, V1JSONSchemaProps>(
                type.GetProperties()
                .Select(
                    prop => KeyValuePair.Create(
                        CamelCase(prop.Name),
                        MapProperty(prop))));
            props.Required = type.GetProperties()
                             .Where(prop => prop.GetCustomAttribute <RequiredAttribute>() != null)
                             .Select(prop => CamelCase(prop.Name))
                             .ToList();
            if (props.Required.Count == 0)
            {
                props.Required = null;
            }
        }
Example #5
0
        private V1JSONSchemaProps MapSchemaToKubernetesModels(JsonSchema schema)
        {
            var result = new V1JSONSchemaProps();

            result.Type    = (schema.Type & ~JsonSchemaType.Null).ToString().ToLower();
            result.Pattern = schema.Pattern;

            if (schema.Properties != null)
            {
                result.Properties = new Dictionary <string, V1JSONSchemaProps>();

                foreach (var property in schema.Properties)
                {
                    result.Properties.Add(property.Key, MapSchemaToKubernetesModels(property.Value));
                }
            }

            if (schema.Items != null)
            {
                result.Items = schema.Items.Select(item => MapSchemaToKubernetesModels(item));
            }

            return(result);
        }
Example #6
0
        private static V1JSONSchemaProps MapType(
            Type type,
            IList <V1CustomResourceColumnDefinition> additionalColumns,
            string jsonPath)
        {
            var props = new V1JSONSchemaProps();

            // this description is on the class
            props.Description ??= type.GetCustomAttributes <DescriptionAttribute>(true).FirstOrDefault()?.Description;

            var isSimpleType = IsSimpleType(type);

            if (type == typeof(V1ObjectMeta))
            {
                props.Type = Object;
            }
            else if (type.IsArray)
            {
                props.Type  = Array;
                props.Items = MapType(
                    type.GetElementType() ?? throw new NullReferenceException("No Array Element Type found"),
                    additionalColumns,
                    jsonPath);
            }
            else if (!isSimpleType &&
                     (typeof(IDictionary).IsAssignableFrom(type) ||
                      (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(IDictionary <,>)) ||
                      (type.IsGenericType &&
                       type.GetGenericArguments().FirstOrDefault()?.IsGenericType == true &&
                       type.GetGenericArguments().FirstOrDefault()?.GetGenericTypeDefinition() ==
                       typeof(KeyValuePair <,>))))
            {
                props.Type = Object;
                props.XKubernetesPreserveUnknownFields = true;
            }
            else if (!isSimpleType && IsGenericEnumerableType(type, out Type? closingType))
            {
                props.Type  = Array;
                props.Items = MapType(closingType, additionalColumns, jsonPath);
            }
            else if (type == typeof(IntstrIntOrString))
            {
                props.XKubernetesIntOrString = true;
            }
            else if (typeof(IKubernetesObject).IsAssignableFrom(type) &&
                     !type.IsAbstract &&
                     !type.IsInterface &&
                     type.Assembly == typeof(IKubernetesObject).Assembly)
            {
                SetEmbeddedResourceProperties(props);
            }
            else if (!isSimpleType)
            {
                ProcessType(type, props, additionalColumns, jsonPath);
            }
            else if (type == typeof(int) || Nullable.GetUnderlyingType(type) == typeof(int))
            {
                props.Type   = Integer;
                props.Format = Int32;
            }
            else if (type == typeof(long) || Nullable.GetUnderlyingType(type) == typeof(long))
            {
                props.Type   = Integer;
                props.Format = Int64;
            }
            else if (type == typeof(float) || Nullable.GetUnderlyingType(type) == typeof(float))
            {
                props.Type   = Number;
                props.Format = Float;
            }
            else if (type == typeof(double) || Nullable.GetUnderlyingType(type) == typeof(double))
            {
                props.Type   = Number;
                props.Format = Double;
            }
            else if (type == typeof(string) || Nullable.GetUnderlyingType(type) == typeof(string))
            {
                props.Type = String;
            }
            else if (type == typeof(bool) || Nullable.GetUnderlyingType(type) == typeof(bool))
            {
                props.Type = Boolean;
            }
            else if (type == typeof(DateTime) || Nullable.GetUnderlyingType(type) == typeof(DateTime))
            {
                props.Type   = String;
                props.Format = DateTime;
            }
            else if (type.IsEnum)
            {
                props.Type         = String;
                props.EnumProperty = new List <object>(Enum.GetNames(type));
            }
            else if (Nullable.GetUnderlyingType(type)?.IsEnum == true)
            {
                props.Type         = String;
                props.EnumProperty = new List <object>(Enum.GetNames(Nullable.GetUnderlyingType(type) !));
            }
            else
            {
                throw new CrdPropertyTypeException();
            }

            return(props);
        }
Example #7
0
        private static V1JSONSchemaProps MapType(Type type)
        {
            var props = new V1JSONSchemaProps();

            // this description is on the class
            props.Description ??= type.GetCustomAttributes <DescriptionAttribute>(true).FirstOrDefault()?.Description;

            if (type == typeof(V1ObjectMeta))
            {
                props.Type = Object;
            }
            else if (type.IsArray)
            {
                props.Type  = Array;
                props.Items = MapType(
                    type.GetElementType() ?? throw new NullReferenceException("No Array Element Type found"));
            }
            else if (!IsSimpleType(type))
            {
                props.Type       = Object;
                props.Properties = new Dictionary <string, V1JSONSchemaProps>(
                    type.GetProperties()
                    .Select(
                        prop => KeyValuePair.Create(
                            CamelCase(prop.Name),
                            MapProperty(prop))));
                props.Required = type.GetProperties()
                                 .Where(prop => prop.GetCustomAttribute <RequiredAttribute>() != null)
                                 .Select(prop => CamelCase(prop.Name))
                                 .ToList();
                if (props.Required.Count == 0)
                {
                    props.Required = null;
                }
            }
            else if (type == typeof(int) || Nullable.GetUnderlyingType(type) == typeof(int))
            {
                props.Type   = Integer;
                props.Format = Int32;
            }
            else if (type == typeof(long) || Nullable.GetUnderlyingType(type) == typeof(long))
            {
                props.Type   = Integer;
                props.Format = Int64;
            }
            else if (type == typeof(float) || Nullable.GetUnderlyingType(type) == typeof(float))
            {
                props.Type   = Number;
                props.Format = Float;
            }
            else if (type == typeof(double) || Nullable.GetUnderlyingType(type) == typeof(double))
            {
                props.Type   = Number;
                props.Format = Double;
            }
            else if (type == typeof(string) || Nullable.GetUnderlyingType(type) == typeof(string))
            {
                props.Type = String;
            }
            else if (type == typeof(bool) || Nullable.GetUnderlyingType(type) == typeof(bool))
            {
                props.Type = Boolean;
            }
            else if (type == typeof(DateTime) || Nullable.GetUnderlyingType(type) == typeof(DateTime))
            {
                props.Type   = String;
                props.Format = DateTime;
            }
            else if (type.IsEnum)
            {
                props.Type         = String;
                props.EnumProperty = new List <object>(Enum.GetNames(type));
            }
            else if (Nullable.GetUnderlyingType(type)?.IsEnum == true)
            {
                props.Type         = String;
                props.EnumProperty = new List <object>(Enum.GetNames(Nullable.GetUnderlyingType(type) !));
            }

            return(props);
        }
        private static V1JSONSchemaProps MapType(Type type)
        {
            var props = new V1JSONSchemaProps();

            // this description is on the class
            props.Description ??= type.GetCustomAttributes <DescriptionAttribute>(true).FirstOrDefault()?.Description;

            if (type == typeof(V1ObjectMeta))
            {
                props.Type = Object;
            }
            else if (type.IsArray)
            {
                props.Type  = Array;
                props.Items = MapType(
                    type.GetElementType() ?? throw new NullReferenceException("No Array Element Type found"));
            }
            else if (!IsSimpleType(type) &&
                     (typeof(IDictionary).IsAssignableFrom(type) ||
                      (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(IDictionary <,>)) ||
                      (type.IsGenericType &&
                       type.GetGenericArguments().FirstOrDefault()?.IsGenericType == true && type.GetGenericArguments().FirstOrDefault()?.GetGenericTypeDefinition() ==
                       typeof(KeyValuePair <,>))))
            {
                props.Type = Object;
                props.XKubernetesPreserveUnknownFields = true;
            }
            else if (!IsSimpleType(type) && type.IsGenericType && typeof(IEnumerable <>).IsAssignableFrom(type.GetGenericTypeDefinition()))
            {
                props.Type  = Array;
                props.Items = MapType(type.GetGenericArguments()[0]);
            }
            else if (type == typeof(IntstrIntOrString))
            {
                props.XKubernetesIntOrString = true;
            }
            else if (!IsSimpleType(type))
            {
                ProcessType(type, props);
            }
            else if (type == typeof(int) || Nullable.GetUnderlyingType(type) == typeof(int))
            {
                props.Type   = Integer;
                props.Format = Int32;
            }
            else if (type == typeof(long) || Nullable.GetUnderlyingType(type) == typeof(long))
            {
                props.Type   = Integer;
                props.Format = Int64;
            }
            else if (type == typeof(float) || Nullable.GetUnderlyingType(type) == typeof(float))
            {
                props.Type   = Number;
                props.Format = Float;
            }
            else if (type == typeof(double) || Nullable.GetUnderlyingType(type) == typeof(double))
            {
                props.Type   = Number;
                props.Format = Double;
            }
            else if (type == typeof(string) || Nullable.GetUnderlyingType(type) == typeof(string))
            {
                props.Type = String;
            }
            else if (type == typeof(bool) || Nullable.GetUnderlyingType(type) == typeof(bool))
            {
                props.Type = Boolean;
            }
            else if (type == typeof(DateTime) || Nullable.GetUnderlyingType(type) == typeof(DateTime))
            {
                props.Type   = String;
                props.Format = DateTime;
            }
            else if (type.IsEnum)
            {
                props.Type         = String;
                props.EnumProperty = new List <object>(Enum.GetNames(type));
            }
            else if (Nullable.GetUnderlyingType(type)?.IsEnum == true)
            {
                props.Type         = String;
                props.EnumProperty = new List <object>(Enum.GetNames(Nullable.GetUnderlyingType(type) !));
            }

            return(props);
        }