From e4c220945ab93bac34c9fae3893abf3e8c03edd6 Mon Sep 17 00:00:00 2001 From: Ivan Verhun Date: Thu, 17 Aug 2017 12:39:34 +0300 Subject: [PATCH 1/2] implemented simple DSL to create appartaments --- .../jclub/dsl/service/DslController.java | 2 +- dsl-scripts/demo.mydsl | 4 +- dsl-scripts/simple.mydsl | 9 ++-- .../com/lohika/jclub/dsl/ApartmentDsl.groovy | 48 +++++++++++++++++++ .../groovy/com/lohika/jclub/dsl/MyDsl.groovy | 31 ++++++++++++ 5 files changed, 88 insertions(+), 6 deletions(-) create mode 100644 dsl/src/main/groovy/com/lohika/jclub/dsl/ApartmentDsl.groovy diff --git a/dsl-executor-service/src/main/java/com/lohika/jclub/dsl/service/DslController.java b/dsl-executor-service/src/main/java/com/lohika/jclub/dsl/service/DslController.java index fd9f1a8..9f3eb6f 100644 --- a/dsl-executor-service/src/main/java/com/lohika/jclub/dsl/service/DslController.java +++ b/dsl-executor-service/src/main/java/com/lohika/jclub/dsl/service/DslController.java @@ -35,7 +35,7 @@ public class DslController { @GetMapping(path = "/{scriptName}") public Object runScript(@PathVariable(name = "scriptName") String scriptName) throws IOException { - File file = new File(basepath + scriptName + ".groovy"); + File file = new File(basepath + scriptName + ".mydsl"); String script = new String(Files.readAllBytes(Paths.get(file.getPath()))); MyDsl dsl = new MyDsl(ratingServiceClient, storageServiceClient); diff --git a/dsl-scripts/demo.mydsl b/dsl-scripts/demo.mydsl index ac42911..3d67d4d 100644 --- a/dsl-scripts/demo.mydsl +++ b/dsl-scripts/demo.mydsl @@ -4,8 +4,8 @@ String myEmail = 'bot@company.name' (1..100).each { String location = "location-${it}" - double myPrice = 2 - double mySqft = 1 + (it as int) + int myPrice = 2 + int mySqft = 1 + (it as int) def rating = rating location, myPrice, mySqft if (rating > 400) { diff --git a/dsl-scripts/simple.mydsl b/dsl-scripts/simple.mydsl index 26b0867..3eb5916 100644 --- a/dsl-scripts/simple.mydsl +++ b/dsl-scripts/simple.mydsl @@ -1,5 +1,5 @@ apartment { - location "location" + location "location11" price 1 sqft 1 phone 'phone' @@ -7,7 +7,7 @@ apartment { mail 'mail' } -apartment("location", { +apartment("location22", { price 1 sqft 1 phone 'phone' @@ -16,10 +16,13 @@ apartment("location", { }) -apartment("location") { +apartment("location33") { + location "location44" price 1 sqft 1 phone 'phone' realtorName 'realtorName' mail 'mail' + location "location55" } + diff --git a/dsl/src/main/groovy/com/lohika/jclub/dsl/ApartmentDsl.groovy b/dsl/src/main/groovy/com/lohika/jclub/dsl/ApartmentDsl.groovy new file mode 100644 index 0000000..1cbea71 --- /dev/null +++ b/dsl/src/main/groovy/com/lohika/jclub/dsl/ApartmentDsl.groovy @@ -0,0 +1,48 @@ +package com.lohika.jclub.dsl + +import com.lohika.jclub.storage.client.Apartment + +//@Builder(prefix = "", builderStrategy = SimpleStrategy.class) +class ApartmentDsl { + String location ="location" + Integer price =2 + Integer sqft =2 + String phone ='phone' + String realtorName ='realtorName' + String mail ='mail' + + void location(String location) { + this.location = location + } + + void price(Integer price) { + this.price = price + } + + void sqft(Integer sqft) { + this.sqft = sqft + } + + void phone(String phone) { + this.phone = phone + } + + void realtorName(String realtorName) { + this.realtorName = realtorName + } + + void mail(String mail) { + this.mail = mail + } + + def toEntity() { + Apartment.builder() + .location(location) + .price(price) + .sqft(sqft) + .phone(phone) + .realtorName(realtorName) + .mail(mail) + .build() + } +} diff --git a/dsl/src/main/groovy/com/lohika/jclub/dsl/MyDsl.groovy b/dsl/src/main/groovy/com/lohika/jclub/dsl/MyDsl.groovy index 1dd3c63..6b5f67a 100644 --- a/dsl/src/main/groovy/com/lohika/jclub/dsl/MyDsl.groovy +++ b/dsl/src/main/groovy/com/lohika/jclub/dsl/MyDsl.groovy @@ -13,8 +13,39 @@ class MyDsl { private RatingServiceClient ratingServiceClient private StorageServiceClient storageServiceClient + def list = [] + MyDsl(RatingServiceClient ratingServiceClient, StorageServiceClient storageServiceClient) { this.storageServiceClient = storageServiceClient this.ratingServiceClient = ratingServiceClient } + + def rating (location, myPrice, mySqft) { + Apartment a = Apartment.builder() + .sqft (mySqft) + .location (location) + .price(myPrice) + .build() + + ratingServiceClient.getRating(a).rating + } + + def apartment (Closure closure) { + ApartmentDsl a = new ApartmentDsl() + closure.delegate = a + closure() + + storageServiceClient.create(a.toEntity()) + list.add(a) + } + + def apartment (String location, Closure closure) { + ApartmentDsl a = new ApartmentDsl() + a.location = location + closure.delegate = a + closure() + + storageServiceClient.create(a.toEntity()) + list.add(a) + } } From aa4bec9c968a5ccdc3008a5ce2e9b1d0f750b1ca Mon Sep 17 00:00:00 2001 From: Ihor Banadiga Date: Tue, 22 Aug 2017 16:59:33 +0300 Subject: [PATCH 2/2] DSL impl fixed --- README.md | 1 + .../jclub/dsl/service/DslController.java | 32 +---------- .../lohika/jclub/dsl/service/DslService.java | 57 +++++++++++++++++++ dsl-scripts/demo.mydsl | 4 +- dsl-scripts/simple.mydsl | 2 - .../com/lohika/jclub/dsl/ApartmentDsl.groovy | 48 +++++----------- .../groovy/com/lohika/jclub/dsl/MyDsl.groovy | 39 ++++++------- dsl/src/main/resources/idea.gdsl | 2 +- 8 files changed, 97 insertions(+), 88 deletions(-) create mode 100644 dsl-executor-service/src/main/java/com/lohika/jclub/dsl/service/DslService.java diff --git a/README.md b/README.md index ecd8898..2f9346c 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,7 @@ Sandbox to play with spring cloud features | Realtor service| Realtor Api Service | 8080| To call other services used Feign, RestTemplate | | Storage service| Storage of Apartment Records Service | 8091| H2 based service for ApartmentRecord data storage. | | API gateway service| Zull API Gateway Service | 8090| | +| DSL executor service | DSL executor service | 8088| | # Dev diff --git a/dsl-executor-service/src/main/java/com/lohika/jclub/dsl/service/DslController.java b/dsl-executor-service/src/main/java/com/lohika/jclub/dsl/service/DslController.java index 9f3eb6f..0ffaf89 100644 --- a/dsl-executor-service/src/main/java/com/lohika/jclub/dsl/service/DslController.java +++ b/dsl-executor-service/src/main/java/com/lohika/jclub/dsl/service/DslController.java @@ -1,13 +1,7 @@ package com.lohika.jclub.dsl.service; -import groovy.lang.GroovyShell; -import groovy.util.DelegatingScript; - import com.lohika.jclub.dsl.MyDsl; -import com.lohika.jclub.rating.client.RatingServiceClient; -import com.lohika.jclub.storage.client.StorageServiceClient; -import org.codehaus.groovy.control.CompilerConfiguration; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; @@ -15,10 +9,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; -import java.io.File; import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Paths; @RestController @RequestMapping(path = "/dsl") @@ -28,27 +19,10 @@ public class DslController { private String basepath; @Autowired - private StorageServiceClient storageServiceClient; - - @Autowired - private RatingServiceClient ratingServiceClient; + private DslService dslService; @GetMapping(path = "/{scriptName}") - public Object runScript(@PathVariable(name = "scriptName") String scriptName) throws IOException { - File file = new File(basepath + scriptName + ".mydsl"); - String script = new String(Files.readAllBytes(Paths.get(file.getPath()))); - - MyDsl dsl = new MyDsl(ratingServiceClient, storageServiceClient); - - CompilerConfiguration configuration = new CompilerConfiguration(); - configuration.setScriptBaseClass(DelegatingScript.class.getName()); - - GroovyShell groovy = new GroovyShell(configuration); - - DelegatingScript delegatingScript = (DelegatingScript) groovy.parse(script); - delegatingScript.setDelegate(dsl); - delegatingScript.run(); - - return dsl; + public MyDsl runScript(@PathVariable(name = "scriptName") String scriptName) throws IOException { + return dslService.runScript(scriptName); } } diff --git a/dsl-executor-service/src/main/java/com/lohika/jclub/dsl/service/DslService.java b/dsl-executor-service/src/main/java/com/lohika/jclub/dsl/service/DslService.java new file mode 100644 index 0000000..52a54ee --- /dev/null +++ b/dsl-executor-service/src/main/java/com/lohika/jclub/dsl/service/DslService.java @@ -0,0 +1,57 @@ +package com.lohika.jclub.dsl.service; + +import groovy.lang.GroovyShell; +import groovy.util.DelegatingScript; + +import com.lohika.jclub.dsl.MyDsl; +import com.lohika.jclub.rating.client.RatingServiceClient; +import com.lohika.jclub.storage.client.StorageServiceClient; + +import org.codehaus.groovy.control.CompilerConfiguration; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; + +@Service +public class DslService { + private static final String DSL_EXTENSION = ".mydsl"; + + @Value("${dsl.basepath}") + private String basepath; + + @Autowired + private StorageServiceClient storageServiceClient; + + @Autowired + private RatingServiceClient ratingServiceClient; + + public MyDsl runScript(String scriptName) throws IOException { + String script = getScriptByName(scriptName); + return run(script); + } + + private String getScriptByName(String scriptName) throws IOException { + File file = new File(basepath + scriptName + DSL_EXTENSION); + return new String(Files.readAllBytes(Paths.get(file.getPath()))); + } + + private MyDsl run(String script) { + MyDsl dsl = new MyDsl(ratingServiceClient, storageServiceClient); + + CompilerConfiguration configuration = new CompilerConfiguration(); + configuration.setScriptBaseClass(DelegatingScript.class.getName()); + + GroovyShell groovy = new GroovyShell(configuration); + + DelegatingScript delegatingScript = (DelegatingScript) groovy.parse(script); + delegatingScript.setDelegate(dsl); + delegatingScript.run(); + + return dsl; + } +} diff --git a/dsl-scripts/demo.mydsl b/dsl-scripts/demo.mydsl index 3d67d4d..ac42911 100644 --- a/dsl-scripts/demo.mydsl +++ b/dsl-scripts/demo.mydsl @@ -4,8 +4,8 @@ String myEmail = 'bot@company.name' (1..100).each { String location = "location-${it}" - int myPrice = 2 - int mySqft = 1 + (it as int) + double myPrice = 2 + double mySqft = 1 + (it as int) def rating = rating location, myPrice, mySqft if (rating > 400) { diff --git a/dsl-scripts/simple.mydsl b/dsl-scripts/simple.mydsl index 3eb5916..3d54975 100644 --- a/dsl-scripts/simple.mydsl +++ b/dsl-scripts/simple.mydsl @@ -17,12 +17,10 @@ apartment("location22", { apartment("location33") { - location "location44" price 1 sqft 1 phone 'phone' realtorName 'realtorName' mail 'mail' - location "location55" } diff --git a/dsl/src/main/groovy/com/lohika/jclub/dsl/ApartmentDsl.groovy b/dsl/src/main/groovy/com/lohika/jclub/dsl/ApartmentDsl.groovy index 1cbea71..6edcb53 100644 --- a/dsl/src/main/groovy/com/lohika/jclub/dsl/ApartmentDsl.groovy +++ b/dsl/src/main/groovy/com/lohika/jclub/dsl/ApartmentDsl.groovy @@ -1,42 +1,20 @@ package com.lohika.jclub.dsl import com.lohika.jclub.storage.client.Apartment +import groovy.transform.builder.Builder +import groovy.transform.builder.SimpleStrategy -//@Builder(prefix = "", builderStrategy = SimpleStrategy.class) +@Builder(prefix = "", builderStrategy = SimpleStrategy.class) class ApartmentDsl { - String location ="location" - Integer price =2 - Integer sqft =2 - String phone ='phone' - String realtorName ='realtorName' - String mail ='mail' - - void location(String location) { - this.location = location - } - - void price(Integer price) { - this.price = price - } - - void sqft(Integer sqft) { - this.sqft = sqft - } - - void phone(String phone) { - this.phone = phone - } - - void realtorName(String realtorName) { - this.realtorName = realtorName - } - - void mail(String mail) { - this.mail = mail - } - - def toEntity() { - Apartment.builder() + String location + double price + double sqft + String phone + String realtorName + String mail + + def toEntity() { + Apartment.builder() .location(location) .price(price) .sqft(sqft) @@ -44,5 +22,5 @@ class ApartmentDsl { .realtorName(realtorName) .mail(mail) .build() - } + } } diff --git a/dsl/src/main/groovy/com/lohika/jclub/dsl/MyDsl.groovy b/dsl/src/main/groovy/com/lohika/jclub/dsl/MyDsl.groovy index 6b5f67a..b5ea678 100644 --- a/dsl/src/main/groovy/com/lohika/jclub/dsl/MyDsl.groovy +++ b/dsl/src/main/groovy/com/lohika/jclub/dsl/MyDsl.groovy @@ -13,39 +13,40 @@ class MyDsl { private RatingServiceClient ratingServiceClient private StorageServiceClient storageServiceClient - def list = [] + def apartments = [] MyDsl(RatingServiceClient ratingServiceClient, StorageServiceClient storageServiceClient) { this.storageServiceClient = storageServiceClient this.ratingServiceClient = ratingServiceClient } - def rating (location, myPrice, mySqft) { - Apartment a = Apartment.builder() - .sqft (mySqft) - .location (location) - .price(myPrice) - .build() + def rating(String location, double myPrice, double mySqft) { + Apartment apartment = Apartment.builder() + .sqft(mySqft) + .location(location) + .price(myPrice) + .build() - ratingServiceClient.getRating(a).rating + ratingServiceClient.getRating(apartment).rating } - def apartment (Closure closure) { - ApartmentDsl a = new ApartmentDsl() - closure.delegate = a + def apartment(Closure closure) { + ApartmentDsl apartment = new ApartmentDsl() + closure.delegate = apartment closure() - storageServiceClient.create(a.toEntity()) - list.add(a) + storageServiceClient.create(apartment.toEntity()) + apartments.add(apartment) } - def apartment (String location, Closure closure) { - ApartmentDsl a = new ApartmentDsl() - a.location = location - closure.delegate = a + def apartment(String location, Closure closure) { + ApartmentDsl apartment = new ApartmentDsl() + apartment.location = location + + closure.delegate = apartment closure() - storageServiceClient.create(a.toEntity()) - list.add(a) + storageServiceClient.create(apartment.toEntity()) + apartments.add(apartment) } } diff --git a/dsl/src/main/resources/idea.gdsl b/dsl/src/main/resources/idea.gdsl index 621cb21..eb4859a 100644 --- a/dsl/src/main/resources/idea.gdsl +++ b/dsl/src/main/resources/idea.gdsl @@ -20,6 +20,6 @@ contributor([contributorBody]) { method name: 'sqft', type: 'void', params: [value: 'java.lang.Double'] method name: 'realtorName', type: 'void', params: [value: 'java.lang.String'] method name: 'mail', type: 'void', params: [value: 'java.lang.String'] + method name: 'phone', type: 'void', params: [value: 'java.lang.String'] } } -