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

Please make AASM compatible with JSON::Serializable #7

Open
drhuffman12 opened this issue Jan 23, 2020 · 2 comments · May be fixed by #8
Open

Please make AASM compatible with JSON::Serializable #7

drhuffman12 opened this issue Jan 23, 2020 · 2 comments · May be fixed by #8

Comments

@drhuffman12
Copy link

I want to use JSON::Serializable with AASM. In the example below, I get this error: Error: no overload matches 'AASM::StateMachine#to_json' with type JSON::Builder.

Crystal code (having 'aasm' shard already installed in the Crystal app):

require "json"
require "aasm"

struct Foo
  # This is just one set of inputs_given, one set of outputs_guessed, and one set of weights (i.e.: no hidden layers)
  # It can be run (via wrapper?) in states (e.g.: init, load, guess, calc_output_errors, calc_output_deltas, adjust_weights, calc_input_deltas, done)

  include AASM
  include JSON::Serializable

  def act_as_state_machine
    aasm.state :pending, initial: true
  end

  # # NOTE: Initializer must have a param; TODO: Find out why.
  # # .... Otherwise, you get something like:
  #  24 | f = Foo.new
  #               ^--
  # Error: wrong number of arguments for 'Foo.new' (given 0, expected 1)
  #
  # Overloads are:
  #  - Foo.new(pull : ::JSON::PullParser)
  def initialize(@bar : String)
    act_as_state_machine
  end
end

f = Foo.new("test")

f.to_json

... will display:

=> Foo(@aasm=#<AASM::StateMachine:0x7f6e302ae740 @states={:pending => AASM::State(@enter=nil, @guard=nil)}, @events={}, @transition_table={}, @current_state_name=:pending>, @bar="test")
icr(0.32.1) > 
icr(0.32.1) > f.to_json
Showing last frame. Use --error-trace for full trace.

There was a problem expanding macro 'macro_140610915503952'

Code in /home/drhuffman/.anyenv/envs/crenv/versions/0.32.1/share/crystal/src/json/serialization.cr:262:7

 262 | {% begin %}
       ^
Called macro defined in /home/drhuffman/.anyenv/envs/crenv/versions/0.32.1/share/crystal/src/json/serialization.cr:262:7

 262 | {% begin %}

Which expanded to:

 > 28 | 
 > 29 |                 
 > 30 |                   _aasm.to_json(json)
                                ^------
Error: no overload matches 'AASM::StateMachine#to_json' with type JSON::Builder

Overloads are:
 - Object#to_json(io : IO)
 - Object#to_json()
@drhuffman12 drhuffman12 linked a pull request Jan 23, 2020 that will close this issue
@drhuffman12
Copy link
Author

I forked the repo and modified it to look like:

class AASM::StateMachine
  getter current_state_name

  include JSON::Serializable
  def to_json
    {
      states: @states,
      events: @events,
      transition_table: @transition_table,
      current_state_name: @current_state_name
    }.to_json
  end

  def initialize
 ...

.. and added updated spec/aasm/state_machine_spec.cr to like:

require "../spec_helper"

def one_state_machine(enter = ->{}, guard = ->{ true })
...

    end

    describe "#to_json" do
      it "creates a StateMachine based on given json" do

        # s = two_states_machine
        # s.next_state.should eq :active

        s1 = two_states_machine
        s1.fire_event :activate
        s1.next_state.should eq nil

        state_json = s1.to_json

        s2 = two_states_machine.from_json(state_json)
        s2.state.should eq (s1.state)
      end
    end
  end
end

But, this complained that I need to implement AASM::State#to_json. So I made this look like:

struct AASM::State
  include JSON::Serializable
  def to_json
    {
      opts: @opts
      # ,
      # enter: @enter,
      # guard: @guard
    }.to_json
  end

  getter enter : (-> Nil)?
...

This complained with: Error: no overload matches 'Proc(Nil)#to_json' with type JSON::Builder. At this point, I'm kinda our of time for this and not quite sure what the best path forward would be.

See: #8

@veelenga
Copy link
Owner

Hey, this is a nice idea.

It happens because there is no way to convert a proc to json and back.

Thanks for the investigation, I will take a look at what could be done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants