Skip to content

Commit

Permalink
feat: Support Ruby handlers with :: in path definition (#1218)
Browse files Browse the repository at this point in the history
  • Loading branch information
njyjn committed Jun 1, 2021
1 parent 4dbab1a commit 3178501
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 4 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,6 @@ We try to follow [Airbnb's JavaScript Style Guide](https://github.com/airbnb/jav
:---: |:---: |:---: |:---: |:---: |
[lteacher](https://github.com/lteacher) |[martinmicunda](https://github.com/martinmicunda) |[nori3tsu](https://github.com/nori3tsu) |[ppasmanik](https://github.com/ppasmanik) |[ryanzyy](https://github.com/ryanzyy) |

[<img alt="m0ppers" src="https://avatars3.githubusercontent.com/u/819421?v=4&s=117" width="117">](https://github.com/m0ppers) |[<img alt="footballencarta" src="https://avatars0.githubusercontent.com/u/1312258?v=4&s=117" width="117">](https://github.com/footballencarta) |[<img alt="bryanvaz" src="https://avatars0.githubusercontent.com/u/9157498?v=4&s=117" width="117">](https://github.com/bryanvaz) |
:---: |:---: |:---: |
[m0ppers](https://github.com/m0ppers) |[footballencarta](https://github.com/footballencarta) |[bryanvaz](https://github.com/bryanvaz) |
[<img alt="m0ppers" src="https://avatars3.githubusercontent.com/u/819421?v=4&s=117" width="117">](https://github.com/m0ppers) |[<img alt="footballencarta" src="https://avatars0.githubusercontent.com/u/1312258?v=4&s=117" width="117">](https://github.com/footballencarta) |[<img alt="bryanvaz" src="https://avatars0.githubusercontent.com/u/9157498?v=4&s=117" width="117">](https://github.com/bryanvaz) |[<img alt="njyjn" src="https://avatars.githubusercontent.com/u/10694375?v=4&s=117" width="117">](https://github.com/njyjn) |
:---: |:---: |:---: |:---: |
[m0ppers](https://github.com/m0ppers) |[footballencarta](https://github.com/footballencarta) |[bryanvaz](https://github.com/bryanvaz) | [njyjn](https://github.com/njyjn) |[kdybicz](https://github.com/kdybicz) |
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@
"Utku Turunc (https://github.com/utkuturunc)",
"Vasiliy Solovey (https://github.com/miltador)",
"Dima Krutolianov (https://github.com/dimadk24)",
"Bryan Vaz (https://github.com/bryanvaz)"
"Bryan Vaz (https://github.com/bryanvaz)",
"Justin Ng (https://github.com/njyjn)"
],
"engines": {
"node": ">=10.13.0"
Expand Down
53 changes: 53 additions & 0 deletions src/utils/__tests__/splitHandlerPathAndName.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import splitHandlerPathAndName from '../splitHandlerPathAndName.js'

const tests = [
{
description: 'ruby handler with namespace resolution operator ::',
expected: ['./src/somefolder/source', 'LambdaFunctions::Handler.process'],
handler: './src/somefolder/source.LambdaFunctions::Handler.process',
},
{
description: 'ruby handler with multiple namespace resolution operators ::',
expected: [
'./src/somefolder/source',
'Functions::LambdaFunctions::Handler.process',
],
handler:
'./src/somefolder/source.Functions::LambdaFunctions::Handler.process',
},
{
description: 'ruby handler with namespace resolution operator ::, unnested',
expected: ['source', 'LambdaFunctions::Handler.process'],
handler: 'source.LambdaFunctions::Handler.process',
},
{
description:
'ruby handler with multiple namespace resolution operators ::, unnested',
expected: ['./source', 'Functions::LambdaFunctions::Handler.process'],
handler: './source.Functions::LambdaFunctions::Handler.process',
},
{
description: 'ruby handler from kernel',
expected: ['./src/somefolder/function', 'handler'],
handler: './src/somefolder/function.handler',
},
{
description: 'generic handler',
expected: ['./src/somefolder/.handlers/handler', 'run'],
handler: './src/somefolder/.handlers/handler.run',
},
{
description: 'generic handler, unnested',
expected: ['handler', 'run'],
handler: 'handler.run',
},
]

describe('splitHandlerPathAndName', () => {
tests.forEach(({ description, expected, handler }) => {
test(`should split ${description}`, () => {
const result = splitHandlerPathAndName(handler)
expect(result).toEqual(expected)
})
})
})
21 changes: 21 additions & 0 deletions src/utils/splitHandlerPathAndName.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,28 @@
// some-folder/src.index => some-folder/src
export default function splitHandlerPathAndName(handler) {
// Split handler into method name and path i.e. handler.run
// Support Ruby paths with namespace resolution operators e.g.
// ./src/somefolder/source.LambdaFunctions::Handler.process
// prepath: ./src/somefolder/
// postpath: source.LambdaFunctions::Handler.process
// filename: source
// path: ./src/somefolder/source
// name: LambdaFunctions::Handler.process
if (handler.match(/::/)) {
const prepathDelimiter = handler.lastIndexOf('/')
const prepath = handler.substr(0, prepathDelimiter + 1) // include '/' for path
const postpath = handler.substr(prepathDelimiter + 1)
const nameDelimiter = postpath.indexOf('.')
const filename = postpath.substr(0, nameDelimiter)
const path = prepath + filename
const name = postpath.substr(nameDelimiter + 1)

return [path, name]
}

// Support nested paths i.e. ./src/somefolder/.handlers/handler.run
// path: ./src/somefoler/.handlers/handler
// name: run
const delimiter = handler.lastIndexOf('.')
const path = handler.substr(0, delimiter)
const name = handler.substr(delimiter + 1)
Expand Down

0 comments on commit 3178501

Please sign in to comment.