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

[LagomScala] Add Scala API generator for lagomframework #6900 #6901

Merged
merged 7 commits into from
Nov 21, 2017

Conversation

gmkumar2005
Copy link
Contributor

@gmkumar2005 gmkumar2005 commented Nov 7, 2017

PR checklist

  • Read the contribution guidelines.
  • Ran the shell script under ./bin/ to update Petstore sample so that CIs can verify the change. (For instance, only need to run ./bin/{LANG}-petstore.sh and ./bin/security/{LANG}-petstore.sh if updating the {LANG} (e.g. php, ruby, python, etc) code generator or {LANG} client's mustache templates). Windows batch files can be found in .\bin\windows\.
  • Filed the PR against the correct branch: 2.3.0 branch for changes related to OpenAPI spec 3.0. Default: master.
  • Copied the technical committee to review the pull request if your PR is targeting a particular programming langauge.

Description of the PR

This change will support code generation of Lagom Scala API. Lagom Scala Api project can be used for writing an implementation or as a client to consume an external service.
####Features which are not supported

  • Form Parameters
  • Seq in query parameters
  • File Handling

import java.io.File;
import java.util.*;

public abstract class AbstractScalaLagomCodegen extends DefaultCodegen {
Copy link
Contributor

Choose a reason for hiding this comment

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

@gmkumar2005 Instead of creating another abstract class, what about using AbstractScalaCodegen.java instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done


import java.util.*;

public class LagomScalaApiCodegen extends AbstractScalaLagomCodegen implements CodegenConfig {
Copy link
Contributor

Choose a reason for hiding this comment

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

I would suggest naming it as "ScalaLagomServerCodegen" following the naming convention of other server generator.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done


@Override
public CodegenType getTag() {
return CodegenType.CLIENT;
Copy link
Contributor

Choose a reason for hiding this comment

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

Please use CodegenType.SERVER instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done


@Override
public String getName() {
return "lagomScalaApi";
Copy link
Contributor

Choose a reason for hiding this comment

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

I would suggest scala-lagom to follow the naming convention "{language}-{lagom}"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed to scala-lagomApi to be able to distinguish from impl.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't understand that part about distinguish from impl, can you clarify?

@@ -81,3 +81,4 @@ io.swagger.codegen.languages.TypeScriptJqueryClientCodegen
io.swagger.codegen.languages.TypeScriptNodeClientCodegen
io.swagger.codegen.languages.UndertowCodegen
io.swagger.codegen.languages.ZendExpressivePathHandlerServerCodegen
io.swagger.codegen.languages.LagomScalaApiCodegen
Copy link
Contributor

Choose a reason for hiding this comment

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

Better to put it according to alpabetical order

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@@ -0,0 +1,10 @@
//
// Copyright (C) 2016 Lightbend Inc. <https://www.lightbend.com>
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we remove it? The autogenerated files are unlicensed by default.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@wing328
Copy link
Contributor

wing328 commented Nov 8, 2017

We can have another PR later to improve the code format.

Ref: Scala style guide - http://docs.scala-lang.org/style/


@Override
public String getHelp() {
return "Generates a Lagom API in scala";
Copy link
Contributor

Choose a reason for hiding this comment

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

FYI. I'll add the word "beta" later to help manage user expectation similar to what we've done for other new generators.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok

Copy link
Contributor

@jimschubert jimschubert left a comment

Choose a reason for hiding this comment

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

I won't have time to evaluate in depth for a few days.

At first glance, I only had one comment.

Although, I'm not familiar with Lagom… and the method signatures missing input parameters was weird to me. I'll have to run locally and see if the structure is just how Lagom does things. When I looked at it (I think pre-1.0 when it was only Java), it had a lot of abstractions even in Java.

public String toVarName(String name) {
// sanitize name
name = sanitizeName(
name); // FIXME: a parameter should not be assigned. Also declare the methods parameters as 'final'.
Copy link
Contributor

Choose a reason for hiding this comment

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

This looks like a copy/paste. Can it be updated/fixed here, or does that affect something elsewhere?

String modifiedName = name;

Then, use modifiedName within the method?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Jim, you are right it is a copy paste from ScalaClientCodegen.Java. I guess this is needed for scala variables. Not sure what will break. Hence retained the logic.
With reference to method signatures.
Please have a look at https://github.com/lagom/sbt-lagom-descriptor-generator
The method signature generated by this PR matches with the one generated in above project.
Please let me know if you find any gaps.

@wing328
Copy link
Contributor

wing328 commented Nov 14, 2017

I ran ./bin/lagomScalaApi-petstore.sh to update the Petstore sample and got the following errors when running sbt test:

[warn] Found version conflict(s) in library dependencies; some are suspected to be binary incompatible:
[warn] 
[warn] 	* com.google.guava:guava:19.0 is selected over 16.0.1
[warn] 	    +- com.lightbend.lagom:lagom-api_2.11:1.3.8           (depends on 16.0.1)
[warn] 	    +- com.google.inject:guice:4.0                        (depends on 16.0.1)
[warn] 
[warn] Run 'evicted' to see detailed eviction warnings
[info] Compiling 9 Scala sources to /private/tmp/swagger-codegen/samples/server/petstore/lagomScalaApi/target/scala-2.11/classes...
[error] /private/tmp/swagger-codegen/samples/server/petstore/lagomScalaApi/src/main/scala/io/swagger/client/api/PetApi.scala:34: type mismatch;
[error]  found   : Seq[String] => com.lightbend.lagom.scaladsl.api.ServiceCall[akka.NotUsed,Seq[io.swagger.client.model.Pet]]
[error]  required: com.lightbend.lagom.scaladsl.api.ServiceSupport.ScalaMethodServiceCall[?,?]
[error]       restCall(Method.GET, "/pet/findByTags?tags", findPetsByTags _), 
[error]                                                    ^
[error] one error found
[error] (compile:compileIncremental) Compilation failed
[error] Total time: 7 s, completed Nov 14, 2017 8:55:33 PM

@wing328
Copy link
Contributor

wing328 commented Nov 16, 2017

UPDATE: I did another test and no longer encountered those errors.

@wing328 wing328 merged commit 32abb72 into swagger-api:master Nov 21, 2017

@Override
public String getName() {
return "scala-lagomApi";
Copy link
Contributor

Choose a reason for hiding this comment

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

I'll change this to scala-lagom to conform to our naming convention.


@Override
public String getHelp() {
return "Generates a Lagom API in scala";
Copy link
Contributor

Choose a reason for hiding this comment

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

I'll add the word "beta" to the description.

@wing328
Copy link
Contributor

wing328 commented Nov 21, 2017

@gmkumar2005 I've merged #7011 (enhancements, etc) into master.

Please pull the latest master and let me know if you've any question.

@jimschubert
Copy link
Contributor

@wing328 lagom is a framework that supports generating both servers and clients. I see in that PR that you changed the generator to scala-lagom, but I'd recommend either scala-lagom-server or that we provide options for one or the other (or both). The options route would break from the project's standards of separating client and server generators, though. We could generate client/server code without options and require users to lean on ignore files, but that would have to be clearly documented, I'd think.

@wing328
Copy link
Contributor

wing328 commented Nov 21, 2017

@jimschubert sure. I'll rename it to scala-lagom-server. Thanks for the feedback.

@wing328
Copy link
Contributor

wing328 commented Nov 21, 2017

@jimschubert done via #7014

@wing328
Copy link
Contributor

wing328 commented Nov 21, 2017

@gmkumar2005 when you've time, please reach me at my email address listed on my Github profile. Thanks.

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

Successfully merging this pull request may close these issues.

3 participants