You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@openwhisk.apache.org by Michele Sciabarra <mi...@sciabarra.com> on 2019/03/06 10:34:15 UTC

Rust for OpenWhisk

Thanks to the effort of Roberto Diaz who provided the actionloop in rust, I built the Rust for OpenWhisk (ActionLoop powered, of course):


```
$ wsk action create hello-rust src/lib.rs --docker actionloop/actionloop-rust-v1.32
ok: created action hello-rust
$ wsk action invoke hello-rust -r
{
    "greeting": "Hello, stranger"
}
$ wsk action invoke hello-rust -r -p name Mike
{
    "greeting": "Hello, Mike"
}
```

This is the rust hello world (probably it can be written better I am an absolute beginner in Rust...):

```
extern crate serde_json;

use std::collections::HashMap;
use serde_json::Value;

pub fn main(args: HashMap<String, Value>) -> HashMap<String, Value> {
    let name_opt = args.get("name");
    let name = if name_opt.is_some() {
        name_opt.unwrap().as_str().unwrap()
    } else {
        "stranger"
    };
    let mut out = HashMap::new();
    out.insert("greeting".to_string(), Value::String(format!("Hello, {}", name)));
    out
}
```

Now we should add all the tests and provide the runtimes for integrating into OpenWhisk... 

-- 
  Michele Sciabarra
  michele@sciabarra.com

Re: Rust for OpenWhisk

Posted by Carlos Santana <cs...@gmail.com>.
Ha good point I didn’t know much about HasMap was only using jsonValue to Marshall and unmarshall objects since that what I learn for initial tutorials 

If the the value of the hashmap is jsonValue then it might ok, I just need to see some examples of how to unmarshall and Marshall those HashMaps inside my action code. 



- Carlos Santana
@csantanapr

> On Mar 7, 2019, at 7:11 AM, Rodric Rabbah <ro...@gmail.com> wrote:
> 
> JsonValue is an enum that encompases any valid JSON value
> https://docs.rs/json/0.2.1/json/enum.JsonValue.html.
> So that signature is too generic IMO. The input should be a JSON object, so
> the dictionary has keys of type String and values of type JsonValue.
> 
> For the result, perhaps using JsonResult instead is better
> https://docs.rs/json/0.2.1/json/type.JsonResult.html.
> 
> -r
> 
>> On Thu, Mar 7, 2019 at 6:56 AM Carlos Santana <cs...@gmail.com> wrote:
>> 
>> Hi Michele
>> 
>>   Thanks for all the work on helping on this I know you are very busy +1
>> 
>> I wanted to discuss the main method signature and open a github issue but
>> issues are not enable in the rust repo [1]
>> 
>> Did you open an INFRA ticket for infra people to configure and enable
>> Github Issues? Please share the link I want ping them on Slack
>> 
>> I was trying to debate on the usage of HashMap vs. jsonValue for the main
>> handler method
>> 
>> For example:
>> fn handler_b(param: JsonValue) -> Result<JsonValue, JsonValue> {
>>    let name = param["name"].as_str().unwrap();
>>    if name == "" {
>>        error!("Empty name in request");
>>        return Err(serde_json::from_str(r#"{"message": "Empty name in
>> param",}"#).unwrap());
>>    } else {
>>        println!("The name is {}", name);
>>        let json_str = r#"{"body": "Hello World",}"#;
>>        let res = serde_json::from_str(json_str).unwrap();
>>        return Ok(res);
>>    }
>> }
>> 
>> You can see the whole program in this gist [2] where I was playing with
>> different Types
>> 
>> I also checked on how AWS Rust handler signature looked
>> 
>> [1] https://github.com/apache/incubator-openwhisk-runtime-rust
>> [2]
>> 
>> https://gist.github.com/csantanapr/50cae6a62b27192f32b1bd4801d8d7c4#file-rust_playground-rs-L40
>> [3] https://aws.amazon.com/blogs/opensource/rust-runtime-for-aws-lambda/
>> 
>> 
>> 
>> On Wed, Mar 6, 2019 at 5:58 AM Michele Sciabarra <mi...@sciabarra.com>
>> wrote:
>> 
>>> Thanks to the effort of Roberto Diaz who provided the actionloop in rust,
>>> I built the Rust for OpenWhisk (ActionLoop powered, of course):
>>> 
>>> 
>>> ```
>>> $ wsk action create hello-rust src/lib.rs --docker
>>> actionloop/actionloop-rust-v1.32
>>> ok: created action hello-rust
>>> $ wsk action invoke hello-rust -r
>>> {
>>>    "greeting": "Hello, stranger"
>>> }
>>> $ wsk action invoke hello-rust -r -p name Mike
>>> {
>>>    "greeting": "Hello, Mike"
>>> }
>>> ```
>>> 
>>> This is the rust hello world (probably it can be written better I am an
>>> absolute beginner in Rust...):
>>> 
>>> ```
>>> extern crate serde_json;
>>> 
>>> use std::collections::HashMap;
>>> use serde_json::Value;
>>> 
>>> pub fn main(args: HashMap<String, Value>) -> HashMap<String, Value> {
>>>    let name_opt = args.get("name");
>>>    let name = if name_opt.is_some() {
>>>        name_opt.unwrap().as_str().unwrap()
>>>    } else {
>>>        "stranger"
>>>    };
>>>    let mut out = HashMap::new();
>>>    out.insert("greeting".to_string(), Value::String(format!("Hello, {}",
>>> name)));
>>>    out
>>> }
>>> ```
>>> 
>>> Now we should add all the tests and provide the runtimes for integrating
>>> into OpenWhisk...
>>> 
>>> --
>>>  Michele Sciabarra
>>>  michele@sciabarra.com
>>> 
>> 
>> 
>> --
>> Carlos Santana
>> <cs...@gmail.com>
>> 

Re: Rust for OpenWhisk

Posted by Rodric Rabbah <ro...@gmail.com>.
JsonValue is an enum that encompases any valid JSON value
https://docs.rs/json/0.2.1/json/enum.JsonValue.html.
So that signature is too generic IMO. The input should be a JSON object, so
the dictionary has keys of type String and values of type JsonValue.

For the result, perhaps using JsonResult instead is better
https://docs.rs/json/0.2.1/json/type.JsonResult.html.

-r

On Thu, Mar 7, 2019 at 6:56 AM Carlos Santana <cs...@gmail.com> wrote:

> Hi Michele
>
>    Thanks for all the work on helping on this I know you are very busy +1
>
> I wanted to discuss the main method signature and open a github issue but
> issues are not enable in the rust repo [1]
>
> Did you open an INFRA ticket for infra people to configure and enable
> Github Issues? Please share the link I want ping them on Slack
>
> I was trying to debate on the usage of HashMap vs. jsonValue for the main
> handler method
>
> For example:
> fn handler_b(param: JsonValue) -> Result<JsonValue, JsonValue> {
>     let name = param["name"].as_str().unwrap();
>     if name == "" {
>         error!("Empty name in request");
>         return Err(serde_json::from_str(r#"{"message": "Empty name in
> param",}"#).unwrap());
>     } else {
>         println!("The name is {}", name);
>         let json_str = r#"{"body": "Hello World",}"#;
>         let res = serde_json::from_str(json_str).unwrap();
>         return Ok(res);
>     }
> }
>
> You can see the whole program in this gist [2] where I was playing with
> different Types
>
> I also checked on how AWS Rust handler signature looked
>
> [1] https://github.com/apache/incubator-openwhisk-runtime-rust
> [2]
>
> https://gist.github.com/csantanapr/50cae6a62b27192f32b1bd4801d8d7c4#file-rust_playground-rs-L40
> [3] https://aws.amazon.com/blogs/opensource/rust-runtime-for-aws-lambda/
>
>
>
> On Wed, Mar 6, 2019 at 5:58 AM Michele Sciabarra <mi...@sciabarra.com>
> wrote:
>
> > Thanks to the effort of Roberto Diaz who provided the actionloop in rust,
> > I built the Rust for OpenWhisk (ActionLoop powered, of course):
> >
> >
> > ```
> > $ wsk action create hello-rust src/lib.rs --docker
> > actionloop/actionloop-rust-v1.32
> > ok: created action hello-rust
> > $ wsk action invoke hello-rust -r
> > {
> >     "greeting": "Hello, stranger"
> > }
> > $ wsk action invoke hello-rust -r -p name Mike
> > {
> >     "greeting": "Hello, Mike"
> > }
> > ```
> >
> > This is the rust hello world (probably it can be written better I am an
> > absolute beginner in Rust...):
> >
> > ```
> > extern crate serde_json;
> >
> > use std::collections::HashMap;
> > use serde_json::Value;
> >
> > pub fn main(args: HashMap<String, Value>) -> HashMap<String, Value> {
> >     let name_opt = args.get("name");
> >     let name = if name_opt.is_some() {
> >         name_opt.unwrap().as_str().unwrap()
> >     } else {
> >         "stranger"
> >     };
> >     let mut out = HashMap::new();
> >     out.insert("greeting".to_string(), Value::String(format!("Hello, {}",
> > name)));
> >     out
> > }
> > ```
> >
> > Now we should add all the tests and provide the runtimes for integrating
> > into OpenWhisk...
> >
> > --
> >   Michele Sciabarra
> >   michele@sciabarra.com
> >
>
>
> --
> Carlos Santana
> <cs...@gmail.com>
>

Re: Rust for OpenWhisk

Posted by Carlos Santana <cs...@gmail.com>.
You can use this INFRA ticket [1] as template to open a new issue to setup
rust Github repo

[1] https://issues.apache.org/jira/browse/INFRA-16770


On Thu, Mar 7, 2019 at 6:55 AM Carlos Santana <cs...@gmail.com> wrote:

> Hi Michele
>
>    Thanks for all the work on helping on this I know you are very busy +1
>
> I wanted to discuss the main method signature and open a github issue but
> issues are not enable in the rust repo [1]
>
> Did you open an INFRA ticket for infra people to configure and enable
> Github Issues? Please share the link I want ping them on Slack
>
> I was trying to debate on the usage of HashMap vs. jsonValue for the main
> handler method
>
> For example:
> fn handler_b(param: JsonValue) -> Result<JsonValue, JsonValue> {
>     let name = param["name"].as_str().unwrap();
>     if name == "" {
>         error!("Empty name in request");
>         return Err(serde_json::from_str(r#"{"message": "Empty name in
> param",}"#).unwrap());
>     } else {
>         println!("The name is {}", name);
>         let json_str = r#"{"body": "Hello World",}"#;
>         let res = serde_json::from_str(json_str).unwrap();
>         return Ok(res);
>     }
> }
>
> You can see the whole program in this gist [2] where I was playing with
> different Types
>
> I also checked on how AWS Rust handler signature looked
>
> [1] https://github.com/apache/incubator-openwhisk-runtime-rust
> [2]
> https://gist.github.com/csantanapr/50cae6a62b27192f32b1bd4801d8d7c4#file-rust_playground-rs-L40
> [3] https://aws.amazon.com/blogs/opensource/rust-runtime-for-aws-lambda/
>
>
>
> On Wed, Mar 6, 2019 at 5:58 AM Michele Sciabarra <mi...@sciabarra.com>
> wrote:
>
>> Thanks to the effort of Roberto Diaz who provided the actionloop in rust,
>> I built the Rust for OpenWhisk (ActionLoop powered, of course):
>>
>>
>> ```
>> $ wsk action create hello-rust src/lib.rs --docker
>> actionloop/actionloop-rust-v1.32
>> ok: created action hello-rust
>> $ wsk action invoke hello-rust -r
>> {
>>     "greeting": "Hello, stranger"
>> }
>> $ wsk action invoke hello-rust -r -p name Mike
>> {
>>     "greeting": "Hello, Mike"
>> }
>> ```
>>
>> This is the rust hello world (probably it can be written better I am an
>> absolute beginner in Rust...):
>>
>> ```
>> extern crate serde_json;
>>
>> use std::collections::HashMap;
>> use serde_json::Value;
>>
>> pub fn main(args: HashMap<String, Value>) -> HashMap<String, Value> {
>>     let name_opt = args.get("name");
>>     let name = if name_opt.is_some() {
>>         name_opt.unwrap().as_str().unwrap()
>>     } else {
>>         "stranger"
>>     };
>>     let mut out = HashMap::new();
>>     out.insert("greeting".to_string(), Value::String(format!("Hello, {}",
>> name)));
>>     out
>> }
>> ```
>>
>> Now we should add all the tests and provide the runtimes for integrating
>> into OpenWhisk...
>>
>> --
>>   Michele Sciabarra
>>   michele@sciabarra.com
>>
>
>
> --
> Carlos Santana
> <cs...@gmail.com>
>


-- 
Carlos Santana
<cs...@gmail.com>

Re: Rust for OpenWhisk

Posted by Carlos Santana <cs...@gmail.com>.
Hi Michele

   Thanks for all the work on helping on this I know you are very busy +1

I wanted to discuss the main method signature and open a github issue but
issues are not enable in the rust repo [1]

Did you open an INFRA ticket for infra people to configure and enable
Github Issues? Please share the link I want ping them on Slack

I was trying to debate on the usage of HashMap vs. jsonValue for the main
handler method

For example:
fn handler_b(param: JsonValue) -> Result<JsonValue, JsonValue> {
    let name = param["name"].as_str().unwrap();
    if name == "" {
        error!("Empty name in request");
        return Err(serde_json::from_str(r#"{"message": "Empty name in
param",}"#).unwrap());
    } else {
        println!("The name is {}", name);
        let json_str = r#"{"body": "Hello World",}"#;
        let res = serde_json::from_str(json_str).unwrap();
        return Ok(res);
    }
}

You can see the whole program in this gist [2] where I was playing with
different Types

I also checked on how AWS Rust handler signature looked

[1] https://github.com/apache/incubator-openwhisk-runtime-rust
[2]
https://gist.github.com/csantanapr/50cae6a62b27192f32b1bd4801d8d7c4#file-rust_playground-rs-L40
[3] https://aws.amazon.com/blogs/opensource/rust-runtime-for-aws-lambda/



On Wed, Mar 6, 2019 at 5:58 AM Michele Sciabarra <mi...@sciabarra.com>
wrote:

> Thanks to the effort of Roberto Diaz who provided the actionloop in rust,
> I built the Rust for OpenWhisk (ActionLoop powered, of course):
>
>
> ```
> $ wsk action create hello-rust src/lib.rs --docker
> actionloop/actionloop-rust-v1.32
> ok: created action hello-rust
> $ wsk action invoke hello-rust -r
> {
>     "greeting": "Hello, stranger"
> }
> $ wsk action invoke hello-rust -r -p name Mike
> {
>     "greeting": "Hello, Mike"
> }
> ```
>
> This is the rust hello world (probably it can be written better I am an
> absolute beginner in Rust...):
>
> ```
> extern crate serde_json;
>
> use std::collections::HashMap;
> use serde_json::Value;
>
> pub fn main(args: HashMap<String, Value>) -> HashMap<String, Value> {
>     let name_opt = args.get("name");
>     let name = if name_opt.is_some() {
>         name_opt.unwrap().as_str().unwrap()
>     } else {
>         "stranger"
>     };
>     let mut out = HashMap::new();
>     out.insert("greeting".to_string(), Value::String(format!("Hello, {}",
> name)));
>     out
> }
> ```
>
> Now we should add all the tests and provide the runtimes for integrating
> into OpenWhisk...
>
> --
>   Michele Sciabarra
>   michele@sciabarra.com
>


-- 
Carlos Santana
<cs...@gmail.com>