You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@openwhisk.apache.org by GitBox <gi...@apache.org> on 2018/06/09 18:04:33 UTC

[GitHub] csantanapr opened a new issue #42: Change function Main signature to match OpenWhisk base Conformance JSON IN<->OUT

csantanapr opened a new issue #42: Change function Main signature to match OpenWhisk base Conformance JSON IN<->OUT
URL: https://github.com/apache/incubator-openwhisk-runtime-go/issues/42
 
 
   Currently the main function signature takes 1 input (raw []bytes) and returns 2 outputs (raw [] bytes, error)
   I think is ok to have alternative signatures like we do for Swift and Codable, and 1 input, an 0 output.
   This is done to adapt better with developer experience and the language idioms
   
   But the base OW signature is the building block and the first one to be offer.
   
   Currently the signature is
   ```
   func Main(event json.RawMessage) (json.RawMessage, error)
   ```
   In example:
   ```golang
   // Main is the function implementing the action
   func Main(event json.RawMessage) (json.RawMessage, error) {
     // decode the json
     var obj map[string]interface{}
     json.Unmarshal(event, &obj)
     // do your work
     name, ok := obj["name"].(string)
     if !ok {
       name = "Stranger"
     }
     msg := map[string]string{"message": ("Hello, " + name + "!")}
     // log in stdout or in stderr
     log.Printf("name=%s\n", name)
     // encode the result back in json
     return json.Marshal(msg)
   }
   ``` 
   This also has a drawback that dev will be 99% will be unmarshalling the input and the output.
   in the way in
   ```
     var obj map[string]interface{}
     json.Unmarshal(event, &obj)
   ```
   in the way out
   ```
   json.Marshal(msg)
   ```
   
   I propose we start with this base signature instead:
   ```
   func Main(params map[string]interface{}) map[string]interface{} 
   ```
   in example much simple for developer to deal with
   ```golang
   func Main(params map[string]interface{}) map[string]interface{} {
   	name, ok := params["name"].(string)
   	if !ok {
   		name = "World"
   	}
   	var result = make(map[string]interface{})
   	result["body"] = "Hello " + name
   	return result
   }
   ```
   to return error same  base API for OW, return a `error` key in the out
   ```golang
   ...
   myError["message"] = "My error msg here for OW to bubble up"
   result["error"] = myError
   return result
   ```
   
   cc @sciabarra @rabbah 
   

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services