Sha256: 9030ad591efbd454afeebbdf6ff76d97575ae3ffa903d37624ebeb478a768435

Contents?: true

Size: 1.21 KB

Versions: 1

Compression:

Stored size: 1.21 KB

Contents

# lib/lutaml/model/schema/yaml_schema.rb
require "yaml"

module Lutaml
  module Model
    module Schema
      class YamlSchema
        def self.generate(klass, options = {})
          schema = {
            "type" => "map",
            "mapping" => generate_mapping(klass),
          }
          YAML.dump(schema)
        end

        private

        def self.generate_mapping(klass)
          klass.attributes.each_with_object({}) do |(name, attr), mapping|
            mapping[name.to_s] = { "type" => get_yaml_type(attr.type) }
          end
        end

        def self.get_yaml_type(type)
          case type
          when Lutaml::Model::Type::String
            "str"
          when Lutaml::Model::Type::Integer
            "int"
          when Lutaml::Model::Type::Boolean
            "bool"
          when Lutaml::Model::Type::Float
            "float"
          when Lutaml::Model::Type::Decimal
            "float" # YAML does not have a separate decimal type, so we use float
          when Lutaml::Model::Type::Array
            "seq"
          when Lutaml::Model::Type::Hash
            "map"
          else
            "str" # Default to string for unknown types
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lutaml-model-0.1.0 lib/lutaml/model/schema/yaml_schema.rb