Skip to content

Remote method generator - #124

Merged
bajtos merged 1 commit into
masterfrom
remote-method-generator
Dec 11, 2015
Merged

bajtos merged 1 commit into
masterfrom
remote-method-generator

Conversation

@jannyHou

Copy link
Copy Markdown
Contributor

hey @bajtos, this is the first implementation of remote-method generator
Further features are recorded in this issue: #94
Please review it when you have time. And I will add new fields and features in the near future.
Thanks a lot.

Comment thread remote-method/index.js Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please remove my name from the NOTE since it's not me who added it ;-) It's ok to keep the note itself though.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think we should come up with a convention for targeting people in comments: ie @bajtos like GH to make it obvious.

@bajtos

bajtos commented Nov 27, 2015

Copy link
Copy Markdown
Member

Thank you @jannyHou for the pull request. The implementation looks mostly good! I need you to add tests before this can be landed.

@bajtos bajtos mentioned this pull request Nov 27, 2015
8 tasks done
@jannyHou

Copy link
Copy Markdown
Contributor Author

thanks @bajtos ! I am working on test cases now and would commit them with some trivial improvements together in the near future.

@jannyHou

jannyHou commented Dec 1, 2015

Copy link
Copy Markdown
Contributor Author

hey @bajtos, I added testing and made some changes to code.

  1. In the testing, accepts and returns are set to empty, I am still thinking about how to do the loop when mock the generator.
  2. I extracted the typeChoices to be returned from helpers.js
  3. I set methodName to be the argument and temporarily canceled option.modelName, so now when you run
slc loopback:remote-method myMethodName

the default methodName would be myMethodName. If the option is still need to set, I think I d better to confirm something with you tmr:)
4. Two major method types are added, I would work on custom types later.

PS:Test failed because I did my two push too close LOL, would be fixed automatically next time I push a new commit.

Comment thread lib/helpers.js Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

please use the new workspace property in L5 above too.

@bajtos

bajtos commented Dec 2, 2015

Copy link
Copy Markdown
Member

I set methodName to be the argument and temporarily canceled option.modelName, so now when you run

slc loopback:remote-method myMethodName

the default methodName would be myMethodName. If the option is still need to set, I think I d better to confirm something with you tmr:)

I find that a bit unusual that one can set method name as an argument but cannot set the model name.

How about slc loopback:remote-method MyModel myMethodName?

If we want to get fancy, then we could even support dot-based notation, but I think that's probably not worth our effort.

slc loopback:remote-method MyModel.myMethodName
slc loopback:remote-method MyModel.prototype.myMethodName

Comment thread test/remote-method.test.js Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I find meths rather confusing, please use the full name methods.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

yes it's wired, I changed it.

Comment thread remote-method/index.js Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

FWIW, one method may be mapped to multiple HTTP endpoints - see strongloop/loopback-workspace#237 and a real-world example in loopback/lib/persisted-model.js.

For the first version, I think it's ok to support only one http endpoint in the generator. I.e. you can keep httpPath and httpVerb, but make sure you create this.http as an array here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think it's not that complex to turn it an array, same as the loop of accepts and returns, I can implement it as an array in next commit.

@bajtos

bajtos commented Dec 2, 2015

Copy link
Copy Markdown
Member

In the testing, accepts and returns are set to empty, I am still thinking about how to do the loop when mock the generator.

I think that would require you to write a custom mock of Yeoman's prompt object. I'm ok to leave this out of this PR (not covered by tests), we don't test looping questions in the model generator either.

I extracted the typeChoices to be returned from helpers.js

👍

Comment thread remote-method/index.js Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We use null for custom value in other places (see e.g. getTypeChoices), please stay consistent and use the same approach here too.

choices: [
  'post', 'put', 'delete', 'get', 'head', 'patch',
  { name: '(other)', value: null }
 ]

@jannyHou
jannyHou force-pushed the remote-method-generator branch from e2f3b4e to 151272a Compare December 2, 2015 21:52
@jannyHou

jannyHou commented Dec 2, 2015

Copy link
Copy Markdown
Contributor Author

Hey @bajtos, I applied your comments and here is the major changes I made today:

  1. Turn http to be an array, create a loop input as accepts and returns.
  2. Make modelName as an argument. My expected user cases are:
    (a). When modelName is provided in cmd, user doesn't need to type in again, the generator skips askForModel and goes to next prompt.
    (b). When no argument is provided in cmd, generator asks for modelName.
    (c). When user provides a wrong modelName in argument, warns him 'Model not found: ' + this.modelName + '. Please choose from Model List:', and generator asks for modelName.
    PS: I think the function is alright but I am not so satisfied with my code, I would make it concise tomorrow if possible:)
  3. Change others to { name: '(other)', value: null } to keep it consistent.
  4. Extract readJsonSync() to test/common.js.

Others are minor fixes like naming convention.

Comment thread remote-method/index.js Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

style: add a space after the assignment operator. (looks like a typo)

var typeChoices = helpers.getTypeChoices;
//               ^

Comment thread lib/helpers.js Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Trailing space in both lines above, please remove. I mean the space before \n in strings.

@bajtos

bajtos commented Dec 7, 2015

Copy link
Copy Markdown
Member

@jannyHou thank you for the update, I left few comments how to improve the solution.

Also please add sample jsdoc comment per #124 (comment)

@jannyHou
jannyHou force-pushed the remote-method-generator branch from 100235d to 185dc19 Compare December 7, 2015 21:41
@jannyHou

jannyHou commented Dec 7, 2015

Copy link
Copy Markdown
Contributor Author

hey @bajtos , I applied your feedback and uploaded a sample screenshot on slack.
I also moved the function into index.js, that's more easy to log everything.
hmm...any ideas on chalk? I searched through the repository but didn't get many clues about the proper usage of it :) I used yellow to chalk the file that need edit and green to chalk all of the arguments(both return and accept).

@bajtos bajtos changed the title First cut implementation for adding remote-method generator Remote method generator Dec 8, 2015
Comment thread remote-method/index.js Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think it's better to use path.join, so that you get common\my-model.json on Windows. Thoughts?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Actually the file name is not just a model name with .js/.json, you should use ModelDefinition.getPath(this.modelDefinition.facetName, this.modelDefinition) to get the path of the model JSON file and this.modelDefinition.getScriptPath() to get the path of the .js file.

See https://github.com/strongloop/loopback-workspace/blob/73c2fd0e624a64c6e3beee9203caf867d37825b2/common/models/model-definition.js#L91-L106 and https://github.com/strongloop/loopback-workspace/blob/73c2fd0e624a64c6e3beee9203caf867d37825b2/common/models/model-definition.js#L195-L203 for the implementation of those methods.

@bajtos

bajtos commented Dec 9, 2015

Copy link
Copy Markdown
Member

@jannyHou

  1. The body of is buildSampleRemoteMethodSource rather long and difficult to visually navigate. It's a good practice to add an empty line as a delimiter between blocks of code, e.g.
  jsonFilePath = chalk.yellow(jsonFilePath);

  // Describe what's next step

However, if you feel like adding a comment to introduce a block of code, then it's usually a sign that the block should be extracted into a standalone function, I get back to this below.

  1. The inline \n characters make it a bit difficult to read the code, could you use join('\n') instead, as in this example?
[
  'line1',
  'line2',
  'line3 ' + myVar + ' here'
].join('\n')
  1. Instead of string concatenation via +, using util.format may make the code easier to read:
util.format('line3 %s here', myVar)
  1. Now back to extracting smaller functions. That would also help the function body (implementation) to stay at a single level of abstraction. So for example, buildSampleRemoteMethodSource should contain high-level instructions like "add what's next step, add jsdoc, add dummy function body, print the result" and each individual step should call a method that would deal with low-level implementation details of array-processing and string manipulation.

A mockup implementation to illustrate my point:

printSampleRemoteMethodSource: function() {
  var text = [
    buildIntroduction(this.modelDefinition),
    buildJsdoc(this),
    buildMethodSource(this)
  ].join('\n');
  console.log(text);
}

function buildMethodSource(def) {
  var ref = def.modelName + (def.isStatic ? '.' : '.prototype.') + def.methodName
  var sourceCode = [
    ref + ' = function(callback) {',
    '  // TODO',
    '  callback(%s);',
    '};'
  ].join('\n');

  var inputArgs = buildInputArgs(def.accepts);
  sourceCode = sourceCode.replace(/*...*/, inputArgs);

  var varDeclarations = buildVarDeclarations(def.returns);
  // string replace

  var callbackArgs = buildCallbackArgs(def.returns);
  // string replace

  return sourceCode;
}

Perhaps we could do this refactoring together later today?

Comment thread remote-method/index.js Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nitpick: we add a space after //, i.e.// TODO

@jannyHou

jannyHou commented Dec 9, 2015

Copy link
Copy Markdown
Contributor Author

hey @bajtos thank you so much for correcting my code convention problems. According to your feedback, I mainly did four changes to refactor printSampleRemoteMethodSource:

  1. Break it into small functions
  2. Replace + with util.format() whenever + is used more than once
  3. Use .join('\n') to connect multiple lines.
  4. Use modelDefinition apis to get file path.
    - I changed something here, as
    ModelDefinition.getPath() prints relative path while
    modelDefinition.getScriptPath() prints absolute path
    I used var jsFilePath = jsonFilePath.replace(/\.json$/, '.js'); to keep them both relative.
    If there is anything to be improved please tell me, thanks a lot!

@bajtos

bajtos commented Dec 10, 2015

Copy link
Copy Markdown
Member

Hi @jannyHou, the code building jsdoc comments looks much better now, I find it much easier to read and understand.

I left three more comments to address, see above.

@jannyHou
jannyHou force-pushed the remote-method-generator branch from 522969a to d7ae003 Compare December 10, 2015 22:37
@jannyHou

Copy link
Copy Markdown
Contributor Author

hey @bajtos I hope after my change printSampleRemoteMethodSource looks more consistent and readable now.
I rebased the branch to make history cleaner and also added commit description on it.

@jannyHou
jannyHou force-pushed the remote-method-generator branch from d7ae003 to 19ddaa5 Compare December 11, 2015 00:16
Implement a new generator `yo loopback:remote-method` for creating
new model methods exposed via strong-remoting.

The generator is also available as `slc loppback:remote-method`
@bajtos

bajtos commented Dec 11, 2015

Copy link
Copy Markdown
Member

@jannyHou I made few final cleaning myself in 3d934be to speed things up and then went ahead and squashed all commits into a single one.

@bajtos
bajtos force-pushed the remote-method-generator branch from 3d934be to ca3c506 Compare December 11, 2015 13:50
bajtos added a commit that referenced this pull request Dec 11, 2015
@bajtos
bajtos merged commit 17e01cc into master Dec 11, 2015
@bajtos
bajtos deleted the remote-method-generator branch December 11, 2015 14:01
@bajtos bajtos removed the #review label Dec 11, 2015
@bajtos

bajtos commented Dec 11, 2015

Copy link
Copy Markdown
Member

Landed 🎉 👏

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.

4 participants