Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maybe Swagger::Object accept Crystal Class or Struct? #18

Open
icyleaf opened this issue Jul 2, 2020 · 0 comments
Open

Maybe Swagger::Object accept Crystal Class or Struct? #18

icyleaf opened this issue Jul 2, 2020 · 0 comments
Labels
help wanted Extra attention is needed

Comments

@icyleaf
Copy link
Owner

icyleaf commented Jul 2, 2020

I dig how macro works in Crystal and done a simple Reflect class, but Crystal can not accept Any Class or Struct as args of method. (also thanks @jhass in gitter)

module Swagger
  struct Object(T)
    property object : T
    property default_name : String
    property custom_name : String?

    def initialize(@object : T, @custom_name : String? = nil)
      @default_name = @custom_name ? @custom_name.as(String) : @object.class.name
    end

    def properties
      @object.instance_vars
    end
  end

  struct Mirror(T)
    property object

    def initialize(reflecting @object : T)
    end

    def instance_vars
      {% begin %}
        {{ ikeys = T.instance.instance_vars.map {|var| "#{var.type}.class" }.join('|').id }}
        vars = [] of Child({{ T.instance.instance_vars.map {|var| "#{var.type}.class" }.join('|').id }}, {{ T.instance.instance_vars.map {|var| var.type }.join('|').id }})
        {% for ivar in T.instance.instance_vars %}
          {{ iname = ivar.name.stringify }}
          {{ itype = ivar.type }}
          {{ irequired = !ivar.type.union? }}
          value = {% if T.class? || T.struct? %} @object.{{ ivar.name }} {% else %} {{ ivar.default_value }} {% end %}
          vars << Child.new({{ iname }}, {{ itype }}, value, {{ irequired }})
        {% end %}

        vars
      {% end %}
    end

    struct Child(K, V)
      getter :name, :type, :value, :required

      def initialize(@name : String, @type : K, @value : V, @required : Bool)
      end
    end
  end
end

Next add Object will be simple to add:

struct User
  property id, nickname, username, email, bio

  def initialize(@id : String, @nickname : String, @username : String, @email : String, @bio : String? = nil)
  end
end

user = User.new(
  UUID.random.to_s, "icyleaf wang", "icyleaf", "[email protected]", "Personal bio"
)

builder.add(Swagger::Object.new(user))
@icyleaf icyleaf added the help wanted Extra attention is needed label Jul 2, 2020
MathiusD added a commit to MathiusD/icyleaf-swagger that referenced this issue Nov 17, 2022
Add creation of Swagger::Object by some object instance

(Related to icyleaf#18)
MathiusD added a commit to MathiusD/icyleaf-swagger that referenced this issue Nov 17, 2022
Add creation of Swagger::Object by some object instance

(Related to icyleaf#18)
MathiusD added a commit to MathiusD/icyleaf-swagger that referenced this issue Dec 23, 2022
Add creation of Swagger::Object by some object instance

(Related to icyleaf#18)
MathiusD added a commit to MathiusD/icyleaf-swagger that referenced this issue Dec 23, 2022
Add management of enumeration
Add support of enumeration inside creation by instance of Swagger::Object
Add refs for other Swagger::Object for non primitive type in creation by instance

(Related to icyleaf#18)

TODO : add spec for enumeration addition
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

1 participant