是否可以在Groovy中动态地向对象添加属性或方法?这是我到目前为止所尝试的:
class Greet {
def name
Greet(who) { name = who[0].toUpperCase() + [1..-1] }
def salute() { println "Hello $name!" }
}
g = new Greet('world') // create object
g.salute() // Output "Hello World!"
g.bye = { println "Goodbye, $name" }
g.bye()
Run Code Online (Sandbox Code Playgroud)
但我得到以下异常:
Hello World!
Caught: groovy.lang.MissingPropertyException: No such property: bye for class: Greet
Possible solutions: name
at test.run(greet.groovy:11)
Run Code Online (Sandbox Code Playgroud) 我有一个像这样的xml:
<todo>
<doLaundry cost="1"/>
<washCar cost="10"/>
<tidyBedroom cost="0" experiencePoints="5000"/>
</todo>
Run Code Online (Sandbox Code Playgroud)
它的XSD架构是:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="todo">
<xs:sequence>
<xs:choice maxOccurs="unbounded">
<xs:element name="doLaundry" type="doLaundry" />
<xs:element name="washCar" type="washCar" />
<xs:element name="tidyBedroom" type="tidyBedroom" />
</xs:choice>
</xs:sequence>
</xs:complexType>
<xs:complexType name="doLaundry">
<xs:attribute name="cost" type="xs:int" />
</xs:complexType>
<xs:complexType name="washCar">
<xs:attribute name="cost" type="xs:int" />
</xs:complexType>
<xs:complexType name="tidyBedroom">
<xs:attribute name="cost" type="xs:int" />
<xs:attribute name="experiencePoints" type="xs:int" />
</xs:complexType>
</xs:schema>
Run Code Online (Sandbox Code Playgroud)
当我通过JAXB处理这个模式时,我得到一个类,其方法如下:
public class Todo {
public List<Object> getDoLaundryOrWashCarOrTidyBedroom() {
...
}
}
Run Code Online (Sandbox Code Playgroud)
理想情况下,我想要的是一种定义所有其他XSD类型扩展的通用基类型的方法.从XSD架构生成的Jaxb类应该有一个返回通用任务列表的方法.这样可以很容易地将新任务添加到待办事项列表中:
public class Todo {
public List<Task> getTasks() { …Run Code Online (Sandbox Code Playgroud) 我使用git-svn创建了许多作为SVN存储库克隆的git项目.我们已将SVN存储库迁移到新的提供程序,因此URL现已更改.如何更新git clone的远程SVN URL?
一种可能性是我从新的SVN存储库重新克隆,但我不想这样做,因为这个过程可能需要几天才能完成整个历史记录.
提出这个问题的另一种方法是,git在哪里存储有关您正在使用的远程SVN存储库位置的信息?即,当您输入'git svn info'时,URL信息来自哪里?
我刚刚将第一个chrome扩展发布到了网上商店.但是我看到搜索结果中扩展名旁边显示的图像只是我在发布时上传的128x128图标的一个小型低质量版本.请注意第二个扩展如何显示高质量的图像:

这是我可以提供的图像列表:
其中,哪一个显示在上面屏幕截图中显示的搜索结果中?
我应该补充一点,我仅在2天前发布了扩展程序,虽然我只上传了最小的促销图片,但它似乎已经过审核和批准,因为在扩展程序的设置中没有显示待处理或拒绝的消息:

如何存储一个方法,以便在给定一个我不期望的值时,它会返回一个默认值?
例如:
Map<String, String> map = mock(Map.class);
when(map.get("abcd")).thenReturn("defg");
when(map.get("defg")).thenReturn("ghij");
when(map.get(anyString())).thenReturn("I don't know that string");
Run Code Online (Sandbox Code Playgroud)
第2部分:如上所述但抛出异常:
Map<String, String> map = mock(Map.class);
when(map.get("abcd")).thenReturn("defg");
when(map.get("defg")).thenReturn("ghij");
when(map.get(anyString())).thenThrow(new IllegalArgumentException("I don't know that string"));
Run Code Online (Sandbox Code Playgroud)
在上面的示例中,最后一个存根优先,因此映射将始终返回默认值.
如果我有一个枚举,我可以使用方便的EnumSet类创建一个EnumSet
enum Suit { CLUBS, DIAMONDS, HEARTS, SPADES }
EnumSet<Suit> reds = EnumSet.of(Suit.HEARTS, Suit.DIAMONDS);
EnumSet<Suit> blacks = EnumSet.of(Suit.CLUBS, Suit.SPADES);
Run Code Online (Sandbox Code Playgroud)
给两个EnumSets,我怎样才能创建一个包含这两个集的并集的新EnumSet?
EnumSet<Suit> redAndBlack = ?
我已经使用GitPython克隆了一个存储库,现在我想检查一个分支并使用该分支的内容更新本地存储库的工作树.理想情况下,我还可以在执行此操作之前检查分支是否存在.这是我到目前为止:
import git
repo_clone_url = "git@github.com:mygithubuser/myrepo.git"
local_repo = "mytestproject"
test_branch = "test-branch"
repo = git.Repo.clone_from(repo_clone_url, local_repo)
# Check out branch test_branch somehow
# write to file in working directory
repo.index.add(["test.txt"])
commit = repo.index.commit("Commit test")
Run Code Online (Sandbox Code Playgroud)
我不知道在上述评论的位置放什么.该文件似乎给出了如何拆卸头,但不知道如何检出一个名为分支的例子.
这是我的对象类:
public class Address
{
public final String line1;
public final String town;
public final String postcode;
public Address(final String line1, final String town, final String postcode)
{
this.line1 = line1;
this.town = town;
this.postcode = postcode;
}
}
Run Code Online (Sandbox Code Playgroud)
我将它添加到速度上下文中,如下所示:
Address theAddress = new Address("123 Fake St", "Springfield", "SP123");
context.put("TheAddress", theAddress);
Run Code Online (Sandbox Code Playgroud)
但是,在编写模板时,以下内容不会呈现地址字段(但是,当我将getter添加到Address类时,它可以正常工作)
<Address>
<Line1>${TheAddress.line1}</Line1>
<Town>${TheAddress.town}</Town>
<Postcode>${TheAddress.postcode}</Postcode>
</Address>
Run Code Online (Sandbox Code Playgroud)
是否可以在不添加getter的情况下访问Velocity上的对象的公共字段?
我想存储和查询大量原始事件数据。我想要使用的架构是“数据湖”架构,其中 S3 保存实际的事件数据,DynamoDB 用于对其进行索引并提供元数据。这是一个在很多地方都在谈论和推荐的架构:
但是,我很难理解如何使用 DynamoDB 来查询 S3 中的事件数据。在上面的 AWS 博客链接中,他们使用了存储多个不同服务器生成的客户事件的示例:
S3路径格式:[4-digit hash]/[server id]/[year]-[month]-[day]-[hour]-[minute]/[customer id]-[epoch timestamp].data
例如:a5b2/i-31cc02/2015-07-05-00-25/87423-1436055953839.data
在 DynamoDB 中记录此事件的架构如下所示:
Customer ID (Partition Key), Timestamp-Server (Sort Key), S3-Key, Size
87423, 1436055953839-i-31cc02, a5b2/i-31cc02/2015-07-05-00-25/87423-1436055953839.data, 1234
Run Code Online (Sandbox Code Playgroud)
我想执行一个查询,例如:“获取过去 24 小时内所有服务器生成的所有客户事件”,但据我了解,不使用分区键就不可能有效地查询 DynamoDB。我无法为此类查询指定分区键。
鉴于此要求,我是否应该使用 DynamoDB 以外的数据库来记录我的事件在 S3 中的位置?或者我只需要使用不同类型的 DynamoDB 架构?
我目前正在使用此处定义的XML词法分析器语法:
https://github.com/antlr/grammars-v4/blob/master/xml/XMLLexer.g4
使用给定的输入,我会生成以下词法分析器事件:
XML输入:
<item>
<![CDATA[
My CDATA Block
]]>
</item>
Run Code Online (Sandbox Code Playgroud)
Lexer输出
[@-1,0:0='<',<7>,1:0]
[@-1,1:4='item',<16>,1:1]
[@-1,5:5='>',<10>,1:5]
[@-1,6:8='\n ',<6>,1:6]
[@-1,9:42='<![CDATA[\n My CDATA Block\n ]]>',<2>,2:2]
[@-1,43:43='\n',<6>,4:5]
[@-1,44:44='<',<7>,5:0]
[@-1,45:45='/',<13>,5:1]
[@-1,46:49='item',<16>,5:2]
[@-1,50:50='>',<10>,5:6]
Run Code Online (Sandbox Code Playgroud)
但是,我想将'<[[CDATA'开始标记和']]>'结束标记与CDATA事件分开,以便我可以拥有一个仅包含该块内容的事件.我尝试了以下语法几乎可以工作,除了因为CDATA块使用非贪婪匹配,块中的每个字符都会创建一个新事件:
新语法:
https://gist.github.com/alexspurling/2e243b1c806a4482697700ea1f686d44
(差异:https://gist.github.com/alexspurling/2e243b1c806a4482697700ea1f686d44/revisions)
输出:
[@-1,0:0='<',<6>,1:0]
[@-1,1:4='item',<15>,1:1]
[@-1,5:5='>',<9>,1:5]
[@-1,6:8='\n ',<5>,1:6]
[@-1,9:17='<![CDATA[',<2>,2:2]
[@-1,18:18='\n',<19>,2:11]
[@-1,19:19=' ',<19>,3:0]
[@-1,20:20=' ',<19>,3:1]
[@-1,21:21=' ',<19>,3:2]
[@-1,22:22=' ',<19>,3:3]
[@-1,23:23='M',<19>,3:4]
[@-1,24:24='y',<19>,3:5]
[@-1,25:25=' ',<19>,3:6]
[@-1,26:26='C',<19>,3:7]
[@-1,27:27='D',<19>,3:8]
[@-1,28:28='A',<19>,3:9]
[@-1,29:29='T',<19>,3:10]
[@-1,30:30='A',<19>,3:11]
[@-1,31:31=' ',<19>,3:12]
[@-1,32:32='B',<19>,3:13]
[@-1,33:33='l',<19>,3:14]
[@-1,34:34='o',<19>,3:15]
[@-1,35:35='c',<19>,3:16]
[@-1,36:36='k',<19>,3:17]
[@-1,37:37='\n',<19>,3:18]
[@-1,38:38=' ',<19>,4:0]
[@-1,39:39=' ',<19>,4:1]
[@-1,40:42=']]>',<18>,4:2]
[@-1,43:43='\n',<5>,4:5]
[@-1,44:44='<',<6>,5:0]
[@-1,45:45='/',<12>,5:1]
[@-1,46:49='item',<15>,5:2]
[@-1,50:50='>',<9>,5:6]
Run Code Online (Sandbox Code Playgroud)
我想要的输出是:
[@-1,0:0='<',<7>,1:0] …Run Code Online (Sandbox Code Playgroud)