groovy

How do I create and access the global variables in Groovy?

五迷三道 提交于 2020-01-18 19:38:18
问题 I need to store a value in a variable in one method and then I need to use that value from that variable in another method or closure. How can I share this value? 回答1: In a Groovy script the scoping can be different than expected. That is because a Groovy script in itself is a class with a method that will run the code, but that is all done runtime. We can define a variable to be scoped to the script by either omitting the type definition or in Groovy 1.8 we can add the @Field annotation.

How do I create and access the global variables in Groovy?

无人久伴 提交于 2020-01-18 19:38:12
问题 I need to store a value in a variable in one method and then I need to use that value from that variable in another method or closure. How can I share this value? 回答1: In a Groovy script the scoping can be different than expected. That is because a Groovy script in itself is a class with a method that will run the code, but that is all done runtime. We can define a variable to be scoped to the script by either omitting the type definition or in Groovy 1.8 we can add the @Field annotation.

Generate A Weighted Random Number

♀尐吖头ヾ 提交于 2020-01-18 02:23:05
问题 I'm trying to devise a (good) way to choose a random number from a range of possible numbers where each number in the range is given a weight. To put it simply: given the range of numbers (0,1,2) choose a number where 0 has an 80% probability of being selected, 1 has a 10% chance and 2 has a 10% chance. It's been about 8 years since my college stats class, so you can imagine the proper formula for this escapes me at the moment. Here's the 'cheap and dirty' method that I came up with. This

Generate A Weighted Random Number

南笙酒味 提交于 2020-01-18 02:22:11
问题 I'm trying to devise a (good) way to choose a random number from a range of possible numbers where each number in the range is given a weight. To put it simply: given the range of numbers (0,1,2) choose a number where 0 has an 80% probability of being selected, 1 has a 10% chance and 2 has a 10% chance. It's been about 8 years since my college stats class, so you can imagine the proper formula for this escapes me at the moment. Here's the 'cheap and dirty' method that I came up with. This

How to change utf-8mb4 to UTF-8 in groovy?

ε祈祈猫儿з 提交于 2020-01-17 12:26:04
问题 Currently, I have a problem with receiving string input from user mobile. The string was inputed and contained utf8mb4 characters (smiley, emoji, etc). This caused error in my backend (mysql) since it only accepts utf-8 input. Now, how can I just replace all utf-8mb4 input to utf-8? def utf8mb4string = '👳👳👳👳👳👳👳'; // parse the utf8mb4string to utf8 // logic here //possible utf8 result maybe: '�������' I have also found similar question here How would I convert UTF-8mb4 to UTF-8? but no clear

URLMapping to direct all request to a single controller/action

泄露秘密 提交于 2020-01-17 11:15:50
问题 I like to develop an API manager. I want, one single controller to accept all requests. How can I direct all web requests to the same controller/action, where I should decide for farther processing. I do not know how to configure the URLMappings.groovy. Possibly I could do that with filter but again I do not know. I will appreciate your support. Thanks 回答1: In URLMappings.groovy , static mappings = { "/**"(controller: "foo", action: "bar") } will direct all URLs to FooController 's bar action

Regex that works on regex101 does not work in Groovy

て烟熏妆下的殇ゞ 提交于 2020-01-17 06:45:40
问题 I have the following regex def formula = math:min(math:round($$value1$$ * $$value2$$) ) def m = formula =~ /\$\$\w+\$\$/ println m.group(1) Above should ideally print $$value1$$ . Now this regex for the following string works fine on regex101.com but same does not work on Groovy. Ideally it should find two groups $$value1$$ and $$value2$$ using Matcher API, but it does not. Is there anything wrong in this regex? 回答1: Assuming formula is: def formula = 'math:min(math:round($$value1$$ * $

Swagger grails Integration

假如想象 提交于 2020-01-17 06:14:25
问题 I am new to swagger and I want to integrate swagger to Restful API project using grails framework. Please help if anybody have any idea what i am doing wrong? my grails specification as below: | Grails Version: 3.0.7 | Groovy Version: 2.4.4 | JVM Version: 1.8.0_71 Did some settings for swagger as below: in build.gradle: dependencies { ... compile "io.swagger:swagger-core:1.5.3" compile "io.swagger:swagger-jaxrs:1.5.3" ... } in resources.groovy import io.swagger.jaxrs.config.BeanConfig beans =

auto-starting Mule flow

北城以北 提交于 2020-01-17 06:12:28
问题 I'm trying this approach to autostart a Mule flow Starting a mule flow programmatically using groovy and it does not start the flow. Here's a very simple test run in Mule Studio with the 3.4.0 CE <?xml version="1.0" encoding="UTF-8"?> <mule xmlns:scripting="http://www.mulesoft.org/schema/mule/scripting" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:spring="http://www.springframework.org/schema/beans" version="CE-3.4.0"

How to access all soap request in a single groovy script

痴心易碎 提交于 2020-01-17 05:58:10
问题 I am writing a groovy script to test my all services in one single step. I imported the WSDL and then all the SOAP request are generated automatically. I want to reduce my manual work of testing all the SOAP services one by one. So, I want to do it via groovy if possible. From here in addressScript - I want to access all the SOAP requests in all the test cases later. so is it possible to implement it via some looping in context ..? Below is sample code am trying . My main moto is to reduce