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

Create FUUID macro annotation to auto-generate Id types #1

Merged
merged 15 commits into from
Jul 5, 2019
Merged

Create FUUID macro annotation to auto-generate Id types #1

merged 15 commits into from
Jul 5, 2019

Conversation

alejandrohdezma
Copy link
Contributor

@alejandrohdezma alejandrohdezma commented Jul 3, 2019

What has been done in this PR?

  • Create a @FUUID annotation that can be used on any kind of object to automatically create an inner Id tagged FUUID type with convenient methods for its creation. It also provides implicit instances for cats' Show, Hash and Order.

Example:

//This...
@FUUID object Institution

//...is transformed into this...
object Institution {

  trait IdTag
     
  type Id = shapeless.tag.@@[FUUID, IdTag]
   
  object Id {
   	
  	//Creates a new `Id` from a `FUUID`
  	def apply(fuuid: FUUID): Institution.Id =
  		shapeless.tag[IdTag][FUUID](fuuid)
   
  	//Creates a new `Id` from an `UUID` literal. This method
  	//uses a macro to compile check the literal value
  	def apply(s: String): Institution.Id =
  		macro FUUIDMacros.fuuidLiteral
   
  	//Creates a random `Id` wrapped in an `F`
  	def random[F[_]: Sync]: F[Institution.Id] =
  		FUUID.randomFUUID[F].map(apply)
  
  	object Unsafe {
      
  		//Creates an unwrapped random `Id`
  		def random: Institution.Id =
  			random.unsafeRunSync())

		}
       
  }

  implicit val IdHashOrderShowInstances: Hash[Institution.Id] with Order[Institution.Id] with Show[Institution.Id] = ???

}

IntelliJ IDEA users

If you are trying to review this PR using IntelliJ IDEA IDE you will probable be thinking that the FUUID annotation is not working, since all references to Id appear red:

Oh no!

No problem at all! This is caused by a known issue in IntelliJ IDEA Scala plugin. But it's easily solved with the help of a small plugin, and, guess what? This PR comes with its own support plugin. Just:

  • Download the plugin
  • Inside IntelliJ Preferences -> Plugins -> ... -> Install plugin from disk ... and select the downloaded jar

And all those nasty red lines will disappear:

Oh yeah!

References

Future work

  • Contribute the IntelliJ IDEA plugin as another module

* }}}
*/
@compileTimeOnly("enable macro paradise to expand macro annotations")
class FUUID extends StaticAnnotation {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this conflict on import?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mmmm, with FUUID, yep.

Maybe we can think of another name for the annotation... DeriveId?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that seems reasonable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to @DeriveId ef5aabb

@ChristopherDavenport
Copy link
Collaborator

Any objections to getting a base out?

@alejandrohdezma
Copy link
Contributor Author

Nope :)

@ChristopherDavenport ChristopherDavenport merged commit 8f29d9d into davenverse:master Jul 5, 2019
@alejandrohdezma alejandrohdezma deleted the fuuid-annotation branch July 5, 2019 20:00
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 this pull request may close these issues.

2 participants