Skip to content

Commit 578f897

Browse files
authored
Merge pull request #20 from lvivJavaClub/dslLiveCodding
implemented simple DSL to create appartaments
2 parents 4649086 + aa4bec9 commit 578f897

File tree

7 files changed

+124
-33
lines changed

7 files changed

+124
-33
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Sandbox to play with spring cloud features
1818
| Realtor service| Realtor Api Service | 8080| To call other services used Feign, RestTemplate |
1919
| Storage service| Storage of Apartment Records Service | 8091| H2 based service for ApartmentRecord data storage. |
2020
| API gateway service| Zull API Gateway Service | 8090| |
21+
| DSL executor service | DSL executor service | 8088| |
2122

2223

2324
# Dev
Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,15 @@
11
package com.lohika.jclub.dsl.service;
22

3-
import groovy.lang.GroovyShell;
4-
import groovy.util.DelegatingScript;
5-
63
import com.lohika.jclub.dsl.MyDsl;
7-
import com.lohika.jclub.rating.client.RatingServiceClient;
8-
import com.lohika.jclub.storage.client.StorageServiceClient;
94

10-
import org.codehaus.groovy.control.CompilerConfiguration;
115
import org.springframework.beans.factory.annotation.Autowired;
126
import org.springframework.beans.factory.annotation.Value;
137
import org.springframework.web.bind.annotation.GetMapping;
148
import org.springframework.web.bind.annotation.PathVariable;
159
import org.springframework.web.bind.annotation.RequestMapping;
1610
import org.springframework.web.bind.annotation.RestController;
1711

18-
import java.io.File;
1912
import java.io.IOException;
20-
import java.nio.file.Files;
21-
import java.nio.file.Paths;
2213

2314
@RestController
2415
@RequestMapping(path = "/dsl")
@@ -28,27 +19,10 @@ public class DslController {
2819
private String basepath;
2920

3021
@Autowired
31-
private StorageServiceClient storageServiceClient;
32-
33-
@Autowired
34-
private RatingServiceClient ratingServiceClient;
22+
private DslService dslService;
3523

3624
@GetMapping(path = "/{scriptName}")
37-
public Object runScript(@PathVariable(name = "scriptName") String scriptName) throws IOException {
38-
File file = new File(basepath + scriptName + ".groovy");
39-
String script = new String(Files.readAllBytes(Paths.get(file.getPath())));
40-
41-
MyDsl dsl = new MyDsl(ratingServiceClient, storageServiceClient);
42-
43-
CompilerConfiguration configuration = new CompilerConfiguration();
44-
configuration.setScriptBaseClass(DelegatingScript.class.getName());
45-
46-
GroovyShell groovy = new GroovyShell(configuration);
47-
48-
DelegatingScript delegatingScript = (DelegatingScript) groovy.parse(script);
49-
delegatingScript.setDelegate(dsl);
50-
delegatingScript.run();
51-
52-
return dsl;
25+
public MyDsl runScript(@PathVariable(name = "scriptName") String scriptName) throws IOException {
26+
return dslService.runScript(scriptName);
5327
}
5428
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.lohika.jclub.dsl.service;
2+
3+
import groovy.lang.GroovyShell;
4+
import groovy.util.DelegatingScript;
5+
6+
import com.lohika.jclub.dsl.MyDsl;
7+
import com.lohika.jclub.rating.client.RatingServiceClient;
8+
import com.lohika.jclub.storage.client.StorageServiceClient;
9+
10+
import org.codehaus.groovy.control.CompilerConfiguration;
11+
import org.springframework.beans.factory.annotation.Autowired;
12+
import org.springframework.beans.factory.annotation.Value;
13+
import org.springframework.stereotype.Service;
14+
15+
import java.io.File;
16+
import java.io.IOException;
17+
import java.nio.file.Files;
18+
import java.nio.file.Paths;
19+
20+
@Service
21+
public class DslService {
22+
private static final String DSL_EXTENSION = ".mydsl";
23+
24+
@Value("${dsl.basepath}")
25+
private String basepath;
26+
27+
@Autowired
28+
private StorageServiceClient storageServiceClient;
29+
30+
@Autowired
31+
private RatingServiceClient ratingServiceClient;
32+
33+
public MyDsl runScript(String scriptName) throws IOException {
34+
String script = getScriptByName(scriptName);
35+
return run(script);
36+
}
37+
38+
private String getScriptByName(String scriptName) throws IOException {
39+
File file = new File(basepath + scriptName + DSL_EXTENSION);
40+
return new String(Files.readAllBytes(Paths.get(file.getPath())));
41+
}
42+
43+
private MyDsl run(String script) {
44+
MyDsl dsl = new MyDsl(ratingServiceClient, storageServiceClient);
45+
46+
CompilerConfiguration configuration = new CompilerConfiguration();
47+
configuration.setScriptBaseClass(DelegatingScript.class.getName());
48+
49+
GroovyShell groovy = new GroovyShell(configuration);
50+
51+
DelegatingScript delegatingScript = (DelegatingScript) groovy.parse(script);
52+
delegatingScript.setDelegate(dsl);
53+
delegatingScript.run();
54+
55+
return dsl;
56+
}
57+
}

dsl-scripts/simple.mydsl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
apartment {
2-
location "location"
2+
location "location11"
33
price 1
44
sqft 1
55
phone 'phone'
66
realtorName 'realtorName'
77
mail 'mail'
88
}
99

10-
apartment("location", {
10+
apartment("location22", {
1111
price 1
1212
sqft 1
1313
phone 'phone'
@@ -16,10 +16,11 @@ apartment("location", {
1616
})
1717

1818

19-
apartment("location") {
19+
apartment("location33") {
2020
price 1
2121
sqft 1
2222
phone 'phone'
2323
realtorName 'realtorName'
2424
mail 'mail'
2525
}
26+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.lohika.jclub.dsl
2+
3+
import com.lohika.jclub.storage.client.Apartment
4+
import groovy.transform.builder.Builder
5+
import groovy.transform.builder.SimpleStrategy
6+
7+
@Builder(prefix = "", builderStrategy = SimpleStrategy.class)
8+
class ApartmentDsl {
9+
String location
10+
double price
11+
double sqft
12+
String phone
13+
String realtorName
14+
String mail
15+
16+
def toEntity() {
17+
Apartment.builder()
18+
.location(location)
19+
.price(price)
20+
.sqft(sqft)
21+
.phone(phone)
22+
.realtorName(realtorName)
23+
.mail(mail)
24+
.build()
25+
}
26+
}

dsl/src/main/groovy/com/lohika/jclub/dsl/MyDsl.groovy

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,40 @@ class MyDsl {
1313
private RatingServiceClient ratingServiceClient
1414
private StorageServiceClient storageServiceClient
1515

16+
def apartments = []
17+
1618
MyDsl(RatingServiceClient ratingServiceClient, StorageServiceClient storageServiceClient) {
1719
this.storageServiceClient = storageServiceClient
1820
this.ratingServiceClient = ratingServiceClient
1921
}
22+
23+
def rating(String location, double myPrice, double mySqft) {
24+
Apartment apartment = Apartment.builder()
25+
.sqft(mySqft)
26+
.location(location)
27+
.price(myPrice)
28+
.build()
29+
30+
ratingServiceClient.getRating(apartment).rating
31+
}
32+
33+
def apartment(Closure closure) {
34+
ApartmentDsl apartment = new ApartmentDsl()
35+
closure.delegate = apartment
36+
closure()
37+
38+
storageServiceClient.create(apartment.toEntity())
39+
apartments.add(apartment)
40+
}
41+
42+
def apartment(String location, Closure closure) {
43+
ApartmentDsl apartment = new ApartmentDsl()
44+
apartment.location = location
45+
46+
closure.delegate = apartment
47+
closure()
48+
49+
storageServiceClient.create(apartment.toEntity())
50+
apartments.add(apartment)
51+
}
2052
}

dsl/src/main/resources/idea.gdsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ contributor([contributorBody]) {
2020
method name: 'sqft', type: 'void', params: [value: 'java.lang.Double']
2121
method name: 'realtorName', type: 'void', params: [value: 'java.lang.String']
2222
method name: 'mail', type: 'void', params: [value: 'java.lang.String']
23+
method name: 'phone', type: 'void', params: [value: 'java.lang.String']
2324
}
2425
}
25-

0 commit comments

Comments
 (0)