DEV Community

Upkar Lidder
Upkar Lidder

Posted on • Originally published at Medium on

Polyglot with Open Source and Serverless

I often mentor at hackathons as part of my job as a developer advocate at IBM and one of the common problems teams face is which language to pick for their solution. Generally the person who originally thought of the idea will decide on a language. That is better than analysis paralysis, however there is a better way! If you can divide your application into multiple services and/or functions, you can use a polyglot framework such as openwhisk.

The OpenWhisk platform supports a programming model in which developers write functional logic (called Actions), in any supported programming language, that can be dynamically scheduled and run in response to associated events (via Triggers) from external sources ( Feeds) or from HTTP requests.

Using such a framework enables you to

  1. Write multiple functions in parallel increasing the effectiveness of the team.
  2. Use multiple languages ensuring everybody is able to participate to the best of their ability.

If you are new to OpenWhisk architecture, let’s do a quick crash course crash💥!

  • Action → your code that runs on the serverless platform
  • Trigger → fired when an external event occurs
  • Rule → connects an trigger to an action
  • Package → bundle or modularize your actions, triggers and rules and share with other code

Let’s recreate this simple example provided by James Thomas in his fabulous tutorial/bootcamp on OpenWhisk. Exercise 1.2 provides an example of action sequence, which simply is an action calling another action, which in turn calls a third action. The results of one action are passed onto the next one. If there is an error, the whole sequence errors out. Instead of implementing the whole sequence in a single language, we will use Javascript, Python and Swift! How cool is that! Here are are requirements

  1. Split: should take in a string and return an array of words split by space. We will write this function in Javascript.
  2. Reverse: should take in an array and return a new array that is reverse of the input array. We will write this function in Swift.
  3. Join: takes in an array and returns a new array that is the space delimited join of the elements. We will write this function in Python.

For example, if I pass in “hello world” to this sequence, I will get back “world, hello”. What do these functions looks like?

Create Actions

splitjs Javascript action

function main(params) {
    var text = params.text || "";
    console.log(`incoming text: ${text}`)
    var words = text.split(' ');
    return {words: words}
}
Enter fullscreen mode Exit fullscreen mode

reverseswift Swift action

struct Input: Codable {
    var words: [String]?
}
struct Output: Codable {
    let message: [String]
}
func main(param: Input, completion: (Output?, Error?) -> Void) -> Void {

    var result = Output(message: [] )

    if let words = param.words{
        result = Output(message: words.reversed() )
    }
    completion(result, nil)
}
Enter fullscreen mode Exit fullscreen mode

joinpy Python action

import sys

import json

def main (dict):

  print ('dict ' + json.dumps(dict))

  if "message" in dict:

     return {'message': ', '.join(dict['message'])}

  else:

     return { 'message': [] }
Enter fullscreen mode Exit fullscreen mode

That’s it! The following three lines deploy these code snippets as actions on openwhisk.

wsk action create splitjs splitjs.js
wsk action create reverseswift reverseswift.swift
wsk action create joinpy joinpy.py
Enter fullscreen mode Exit fullscreen mode

Let’s test these out now.

Testing our actions individually

Test split javascript action

$ wsk action invoke splitjs -r -p text "hello world"


{  
 "words": [  
 "hello",  
 "world"  
 ]  
}
Enter fullscreen mode Exit fullscreen mode

Test reverse swift action

$ wsk action invoke reverseswift -r -p words '["hello","world"]'


{  
 "message": [  
 "world",  
 "hello"  
 ]  
}
Enter fullscreen mode Exit fullscreen mode

Test join python action

$ wsk action invoke joinpy -r -p message '["world", "hello"]'


{  
 "message": "world, hello"  
}
Enter fullscreen mode Exit fullscreen mode

Creating a sequence

We can create a sequence

splitjs → reverseswift → joinpy

wsk action create split-reverse-join --sequence splitjs,reverseswift,joinpy
Enter fullscreen mode Exit fullscreen mode

Finally, let’s test it out!

$ wsk action invoke split-reverse-join -r -p text "Hello World"


{  
 "message": "World, Hello"  
}
Enter fullscreen mode Exit fullscreen mode

How cool is that! Let’s all celebrate with a Chandler dance!

If you are around the bay area, join me for a hands-on workshop on OpenWhisk on 08/08/19. You can register here. IBM Developer and the API wizard Max Katz will be around.

Oldest comments (0)