June 26, 2020

Golang range gotcha

In golang, when you range through an array of elements as in the following:

// THIS CODE IS WRONG; DO NOT USE THIS
for i,v := range elements {
    go do_something(v)
}

In the above example, the value of v may change while do_something tries to used it. This is a race condition and will definitely cause problem. To fix this, one should use elements[i] instead;

June 12, 2020

Vim typescript jump-to-definition

  1. Use vim 8.1 or newer (in older version of vim, tagstack doesn’t work with the solution below)
  2. Install ALE https://github.com/dense-analysis/ale
  3. bind Ctrl-] to ALEGoToDefinition using the following snippet of code
function ALELSPMappings()
    let l:lsp_found=0
    for l:linter in ale#linter#Get(&filetype) | if !empty(l:linter.lsp) | let l:lsp_found=1 | endif | endfor
    if (l:lsp_found)
        nnoremap <buffer> <C-]> :ALEGoToDefinition<CR>
        nnoremap <buffer> <C-^> :ALEFindReferences<CR>
    else
        silent! unmap <buffer> <C-]>
        silent! unmap <buffer> <C-^>
    endif
endfunction
autocmd BufRead,FileType * call ALELSPMappings()
  1. install tsserver if you have not done so using npm
  2. Done.

June 11, 2020

AWS API Gateway Wildcard Path

In API Gateway configuration, typically one specifies an complete API Path, such as /api/v1/fruits/list. Sometimes, it may be necessary to specify a wildcard path that includes multiple complete API paths. This is how you can do it.

  1. Use curly brackets. Specify /api/v1/fruits/{action} to allow any action to be accepted. Action can be any valid URL path segment, but it only covers one depth. It does not cover deeper URL like api/v1/fruits/list/all.
  2. To cover all sub-links, use {proxy+}. The + sign tells API Gateway to accept all sub-links.
  3. Note that you need a separate API for the root path itself, in this case /api/v1/fruits needs its own API. It’s possible to use /api/v1/fruits/{action?} to cover the root path. Needs to be verified since it’s not in the documentation.
  4. In GoLang, the wildcard value is passed to the Lambda handler in a string-to-string map named PathParameters. PathParameters:map[string]string{"id":"234"}

See more at https://aws.amazon.com/blogs/aws/api-gateway-update-new-features-simplify-api-development/