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

Creating a model with no "real" fields causes amber to fail #518

Closed
docelic opened this issue Jan 13, 2018 · 1 comment
Closed

Creating a model with no "real" fields causes amber to fail #518

docelic opened this issue Jan 13, 2018 · 1 comment

Comments

@docelic
Copy link
Contributor

docelic commented Jan 13, 2018

When a model is created with no "real" fields (i.e. when it has only id and timestamps) then some macro doesn't expand to vaild code.

Steps to reproduce:

amber new myapp
crystal src/myapp.cr <-- works

amber g model mymodel
crystal src/myapp.cr <-- fails with:

Error in macro 'inherited' /tmp/myapp/lib/granite_orm/src/granite_orm/base.cr:22, line 1:

>  1.     macro finished
   2.       __process_table
   3.       __process_fields
   4.       __process_querying
   5.       __process_transactions
   6.     end
   7.   

expanding macro
in macro 'finished' expanded macro: inherited:1, line 2:

   1.       __process_table
>  2.       __process_fields
   3.       __process_querying
   4.       __process_transactions
   5.     

macro didn't expand to a valid program, it expanded to:

================================================================================
--------------------------------------------------------------------------------
   1.     # Create the properties
   2.     
   3.     
   4.       property created_at : Time?
   5.       property updated_at : Time?
   6.     
   7. 
   8.     # keep a hash of the fields to be used for mapping
   9.     def self.fields(fields = [] of String)
  10.       
  11.       
  12.         fields << "created_at"
  13.         fields << "updated_at"
  14.       
  15.       return fields
  16.     end
  17. 
  18.     # keep a hash of the params that will be passed to the adapter.
  19.     def params
  20.       parsed_params = [] of DB::Any
  21.       
  22.       
  23.         parsed_params << created_at.not_nil!.to_s("%F %X")
  24.         parsed_params << updated_at.not_nil!.to_s("%F %X")
  25.       
  26.       return parsed_params
  27.     end
  28. 
  29.     def to_h
  30.       fields = {} of String => DB::Any
  31. 
  32.       fields["id"] = id
  33. 
  34.       
  35.       
  36.         fields["created_at"] = created_at.try(&.to_s("%F %X"))
  37.         fields["updated_at"] = updated_at.try(&.to_s("%F %X"))
  38.       
  39. 
  40.       return fields
  41.     end
  42. 
  43.     def to_json(json : JSON::Builder)
  44.       json.object do
  45.         json.field "id", id
  46. 
  47.         
  48. 
  49.         
  50.           json.field "created_at", created_at.try(&.to_s("%F %X"))
  51.           json.field "updated_at", updated_at.try(&.to_s("%F %X"))
  52.         
  53.       end
  54.     end
  55. 
  56.     def set_attributes(args : Hash(String | Symbol, Type))
  57.       args.each do |k, v|
  58.         cast_to_field(k, v.as(Type))
  59.       end
  60.     end
  61. 
  62.     def set_attributes(**args)
  63.       set_attributes(args.to_h)
  64.     end
  65. 
  66.     # Casts params and sets fields
  67.     private def cast_to_field(name, value : Type)
  68.       case name.to_s
  69.         
  70.       end
  71.     end
  72.   
--------------------------------------------------------------------------------
Syntax error in expanded macro: __process_fields:70: unexpected token: end (expecting when or else)

      end
      ^
@eliasjpr
Copy link
Contributor

@docelic closing this issue in favor of https://github.com/amberframework/granite-orm/issues/112 since is a Granite::ORM issue related

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

No branches or pull requests

2 participants