Skip to content

Commit fdac7f9

Browse files
Merge pull request #193 from fnproject/mww-190404-reverse-up
Update Java FDK string-reverse example
2 parents 45da3e0 + 79daa42 commit fdac7f9

File tree

7 files changed

+104
-80
lines changed

7 files changed

+104
-80
lines changed

examples/README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,39 @@
1-
# `fn` Java FDK Example Projects
1+
# Fn Java FDK Example Projects
22

33
In this directory you will find some example projects demonstrating different
4-
features of the `fn` Java FDK:
4+
features of the Fn Java FDK:
55

66
* Plain java code support (`string-reverse`)
77
* Functional testing of your functions (`regex-query` and `qr-code`)
88
* Built-in JSON coercion (`regex-query`)
99
* [InputEvent and OutputEvent](/docs/DataBinding.md) handling (`qr-code`)
1010

11-
## 1. String reverse
11+
## (1) [String reverse](string-reverse/README.md)
1212

1313
This function takes a string and returns the reverse of the string.
14-
The `fn` Java FDK runtime will handle marshalling data into your
15-
functions without the function having to have any knowledge of the FDK API.
14+
The Fn Java FDK handles marshalling data into your
15+
functions without the function having any knowledge of the FDK API.
1616

17-
## 2. Regex query
17+
## (2) Regex query
1818

1919
This function takes a JSON object containing a `text` field and a `regex`
2020
field and return a JSON object with a list of matches in the `matches`
2121
field. It demonstrates the builtin JSON support of the fn Java
22-
wrapper (provided through Jackson) and how the platform handles serialisation
22+
wrapper (provided through Jackson) and how the platform handles serialization
2323
of POJO return values.
2424

25-
## 3. QR Code gen
25+
## (3) QR Code gen
2626

2727
This function parses the query parameters of a GET request (through the
2828
`InputEvent` passed into the function) to generate a QR code. It demonstrates
2929
the `InputEvent` and `OutputEvent` interfaces which provide low level
3030
access to data entering the `fn` Java FDK.
3131

32-
## 4. Asynchronous thumbnails generation
32+
## (4) Asynchronous thumbnails generation
3333

3434
This example showcases the Fn Flow asynchronous execution API, by
3535
creating a workflow that takes an image and asynchronously generates three
3636
thumbnails for it, then uploads them to an object storage.
3737

38-
## 5. Gradle build
38+
## (5) Gradle build
3939
This shows how to use Gradle to build functions using the Java FDK.

examples/string-reverse/README.md

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,75 @@
1-
# Example oFunctions Project: String Reverse
1+
# Example Java Function: String Reverse
22

3-
This example provides an HTTP endpoint for reversing strings
3+
This example provides an HTTP trigger endpoint for reversing strings.
44

55
```bash
6-
$ curl -d "Hello, World!" "http://localhost:8080/r/string-reverse-app/reverse"
7-
!dlroW ,olleH
6+
$ curl -d "Hello World" http://localhost:8080/t/string-reverse-app/string-reverse
7+
dlroW olleH
88
```
99

1010

1111
## Demonstrated FDK features
1212

13-
This example uses **no** features of the fn Java FDK; in fact it doesn't have
14-
a dependency on the fn Java FDK, it just plain old Java code.
13+
This example uses **none** of the Fn Java FDK features, in fact it doesn't have
14+
any dependency on the Fn Java FDK. It is just plain old Java code.
1515

1616
## Step by step
1717

18-
Ensure you have the functions server running using, this will host your
19-
function and provide the HTTP endpoints that invoke it:
18+
Ensure you have the Fn server running to host your
19+
function and provide the HTTP endpoint that invokes it:
2020

21-
```bash
21+
(1) Start the server
22+
23+
```sh
2224
$ fn start
2325
```
2426

25-
Build the function locally
27+
(2) Create an app for the function
2628

27-
```bash
28-
$ fn build
29+
```sh
30+
$ fn create app string-reverse-app
2931
```
3032

31-
Create an app and route to host the function
33+
(3) Deploy the function to your app from the `string-reverse` directory.
3234

33-
```bash
34-
$ fn create app string-reverse-app
35-
$ fn create route string-reverse-app /reverse
35+
```sh
36+
fn deploy --app string-reverse-app --local
37+
```
38+
39+
(4) Invoke the function and reverse the string.
40+
41+
```sh
42+
echo "Hello World" | fn invoke string-reverse-app string-reverse
43+
dlroW olleH
3644
```
3745

38-
Invoke the function to reverse a string
46+
(5) Invoke the function using curl and a trigger to reverse a string.
3947

4048
```bash
41-
$ curl -d "Hello, World!" "http://localhost:8080/r/string-reverse-app/reverse"
42-
!dlroW ,olleH
49+
$ curl -d "Hello World" http://localhost:8080/t/string-reverse-app/string-reverse
50+
dlroW olleH
4351
```
4452

4553

4654
## Code walkthrough
4755

4856
The entrypoint to the function is specified in `func.yaml` in the `cmd` key.
49-
It is set this to `com.fnproject.fn.examples.StringReverse::reverse`. The whole class
57+
It is set this to `com.example.fn.StringReverse::reverse`. The whole class
5058
`StringReverse` is shown below:
5159

5260

5361
```java
54-
package com.fnproject.fn.examples;
62+
package com.example.fn;
5563

5664
public class StringReverse {
5765
public String reverse(String str) {
58-
StringBuilder builder = new StringBuilder();
59-
for (int i = str.length() - 1; i >= 0; i--) {
60-
builder.append(str.charAt(i));
61-
}
62-
return builder.toString();
66+
return new StringBuilder(str).reverse().toString();
6367
}
6468
}
6569
```
6670

67-
As you can see, this is plain java with no references to the fn API. The
68-
fn Java FDK handles the marshalling of the HTTP body into the `str`
71+
As you can see, this is plain java with no references to the Fn API. The
72+
Fn Java FDK handles the marshalling of the HTTP body into the `str`
6973
parameter as well as the marshalling of the returned reversed string into the HTTP
7074
response body (see [Data Binding](/docs/DataBinding.md) for more
7175
information on how marshalling is performed).

examples/string-reverse/func.yaml

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
name: fn-example/string-reverse
2-
version: 0.0.1
1+
schema_version: 20180708
2+
name: string-reverse
3+
version: 0.0.2
34
runtime: java
4-
timeout: 30
5-
format: http
6-
cmd: com.fnproject.fn.examples.StringReverse::reverse
7-
path: /reverse
5+
build_image: fnproject/fn-java-fdk-build:jdk11-1.0.87
6+
run_image: fnproject/fn-java-fdk:jre11-1.0.87
7+
cmd: com.example.fn.StringReverse::reverse
8+
triggers:
9+
- name: string-reverse
10+
type: http
11+
source: /string-reverse

examples/string-reverse/pom.xml

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,50 @@
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
44
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
55
<modelVersion>4.0.0</modelVersion>
6-
7-
86
<properties>
97
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
10-
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
11-
8+
<fdk.version>1.0.87</fdk.version>
129
</properties>
13-
14-
<groupId>com.fnproject.fn.examples</groupId>
10+
<groupId>com.example.fn</groupId>
1511
<artifactId>string-reverse</artifactId>
16-
<version>1.0.0-SNAPSHOT</version>
12+
<version>1.0.0</version>
13+
14+
<repositories>
15+
<repository>
16+
<id>fn-release-repo</id>
17+
<url>https://dl.bintray.com/fnproject/fnproject</url>
18+
<releases>
19+
<enabled>true</enabled>
20+
</releases>
21+
<snapshots>
22+
<enabled>false</enabled>
23+
</snapshots>
24+
</repository>
25+
</repositories>
1726

1827
<dependencies>
28+
<dependency>
29+
<groupId>com.fnproject.fn</groupId>
30+
<artifactId>api</artifactId>
31+
<version>${fdk.version}</version>
32+
</dependency>
33+
<dependency>
34+
<groupId>com.fnproject.fn</groupId>
35+
<artifactId>testing-core</artifactId>
36+
<version>${fdk.version}</version>
37+
<scope>test</scope>
38+
</dependency>
39+
<dependency>
40+
<groupId>com.fnproject.fn</groupId>
41+
<artifactId>testing-junit4</artifactId>
42+
<version>${fdk.version}</version>
43+
<scope>test</scope>
44+
</dependency>
1945
<dependency>
2046
<groupId>junit</groupId>
2147
<artifactId>junit</artifactId>
2248
<version>4.12</version>
49+
<scope>test</scope>
2350
</dependency>
2451
</dependencies>
2552

@@ -28,27 +55,20 @@
2855
<plugin>
2956
<groupId>org.apache.maven.plugins</groupId>
3057
<artifactId>maven-compiler-plugin</artifactId>
31-
<version>3.8.0</version>
58+
<version>3.3</version>
3259
<configuration>
33-
<source>1.8</source>
34-
<target>1.8</target>
60+
<source>8</source>
61+
<target>8</target>
3562
</configuration>
3663
</plugin>
3764
<plugin>
38-
<groupId>org.apache.maven.plugins</groupId>
39-
<artifactId>maven-deploy-plugin</artifactId>
40-
<version>2.8.2</version>
41-
<configuration>
42-
<skip>true</skip>
43-
</configuration>
65+
<groupId>org.apache.maven.plugins</groupId>
66+
<artifactId>maven-surefire-plugin</artifactId>
67+
<version>2.22.1</version>
68+
<configuration>
69+
<useSystemClassLoader>false</useSystemClassLoader>
70+
</configuration>
4471
</plugin>
4572
</plugins>
4673
</build>
47-
48-
<repositories>
49-
<repository>
50-
<id>fn-maven-releases</id>
51-
<url>https://dl.bintray.com/fnproject/fnproject</url>
52-
</repository>
53-
</repositories>
5474
</project>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.example.fn;
2+
3+
public class StringReverse {
4+
public String reverse(String str) {
5+
return new StringBuilder(str).reverse().toString();
6+
}
7+
}

examples/string-reverse/src/main/java/com/fnproject/fn/examples/StringReverse.java

Lines changed: 0 additions & 11 deletions
This file was deleted.

examples/string-reverse/src/test/java/com/fnproject/examples/StringReverseTest.java renamed to examples/string-reverse/src/test/java/com/example/fn/testing/StringReverseTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
package com.fnproject.examples;
1+
package com.example.fn.testing;
22

3-
import com.fnproject.fn.examples.StringReverse;
3+
import com.example.fn.StringReverse;
44
import org.junit.Test;
55

66
import static junit.framework.TestCase.assertEquals;

0 commit comments

Comments
 (0)