案例类在Scala中映射(Case class to map in Scala)
有人知道如果有一个很好的方法,我可以转换一个Scala案例类实例,例如
case class MyClass(param1: String, param2: String) val x = MyClass("hello", "world")
进入某种映射,例如
getCCParams(x) returns "param1" -> "hi", "param2" -> "3"
它适用于任何案例类,而不仅仅是预定义的类。 我发现你可以通过写一个询问底层Product类的方法来提取case类名,例如
def getCCName(caseobj: Product) = caseobj.productPrefix getCCName(x) returns "MyClass"
所以我正在寻找一个类似的解决方案,但对于case类的字段。 我想象一个解决方案可能需要使用Java反射,但是如果案例类的底层实现发生变化,我会讨厌在未来的Scala版本中写出一些可能会出现的错误。
目前,我正在使用Scala服务器,并使用案例类定义协议及其所有的消息和异常,因为它们是一个如此美丽,简洁的结构。 但是,我需要将它们转换成Java映射,以便通过消息传递层发送以供客户端实现使用。 我目前的实现只是为每个案例类定义一个翻译,但是找到一个通用的解决方案是很好的。
Is there a nice way I can convert a Scala
case class
instance, e.g.case class MyClass(param1: String, param2: String) val x = MyClass("hello", "world")
into a mapping of some kind, e.g.
getCCParams(x) returns "param1" -> "hello", "param2" -> "world"
Which works for any case class, not just predefined ones. I've found you can pull the case class name out by writing a method that interrogates the underlying Product class, e.g.
def getCCName(caseobj: Product) = caseobj.productPrefix getCCName(x) returns "MyClass"
So I'm looking for a similar solution but for the case class fields. I'd imagine a solution might have to use Java reflection, but I'd hate to write something that might break in a future release of Scala if the underlying implementation of case classes changes.
Currently I'm working on a Scala server and defining the protocol and all its messages and exceptions using case classes, as they are such a beautiful, concise construct for this. But I then need to translate them into a Java map to send over the messaging layer for any client implementation to use. My current implementation just defines a translation for each case class separately, but it would be nice to find a generalised solution.
原文:https://stackoverflow.com/questions/1226555
最满意答案
据我所知,如果cookbook_file存在,就不可能有利于测试。 但是如果没有找到该文件,您可以让厨师继续运行而不是失败。
cookbook_file "/etc/cron.d/#{service}" do source "etc/cron.d/#{service}" owner "root" group "root" mode "0644" ignore_failure true end
As far as I know, it is not possible to test in advantage if the cookbook_file exists. But you can make chef continue the run instead of failing, if it didn't find the file.
cookbook_file "/etc/cron.d/#{service}" do source "etc/cron.d/#{service}" owner "root" group "root" mode "0644" ignore_failure true end
相关问答
更多-
据我所知,如果cookbook_file存在,就不可能有利于测试。 但是如果没有找到该文件,您可以让厨师继续运行而不是失败。 cookbook_file "/etc/cron.d/#{service}" do source "etc/cron.d/#{service}" owner "root" group "root" mode "0644" ignore_failure true end As far as I know, it is not possible to test in ...
-
您可能希望为此使用remote_directory资源,但更有可能您应该使用其他类型的文件打包/分发系统,因为Chef的内部工具非常小。 共享git存储库(带有git资源)或deb / RPM包(带有package资源)都是流行的选项。 You might want to be using a remote_directory resource for this, but more likely you should use some other kind of file packaging/distrib ...
-
目前还没有,除了一些手册require '../../../../othercookbook/test/integration/default/serverspec/other_spec'或类似的winkiness,这将取决于你碰巧使用的精确文件夹布局。 InSpec使这更容易,因为两个烹饪书都可以使用单个共享的InSpec测试配置文件。 Not currently, other than some manual require '../../../../othercookbook/test/integra ...
-
这不是我们真正支持的。 如果你深入挖掘内部API,你可以找到办法做到这一点,但这不是我们鼓励或支持的用例。 最好设置一个后备模板。 类似source ["#{filename}_#{node['hostname']}.erb", "#{filename}_default.erb"]或类似的东西,如果没有找到per-hostname模板,它会使用默认的一个(可能只是空的)。 This is not something we really support. If you go digging in the in ...
-
厨师更新配置文件(Chef update config file)[2022-10-16]
在厨师中,配方中的命令顺序是执行顺序。 如果你保存了nginx.conf的template ,并且它在你的cookbook_file命令之后,那么生成的模板将覆盖你的文件。 例如 # cookbook file cookbook_file "#{node[:nginx][:dir]}/nginx.conf" do source "my_nginx.conf" mode 0644 owner "root" group "root" end # template template "nginx ... -
阅读https://coderanger.net/two-pass/以了解如何强制编译时运行。 这就是说,请不要这样做。 通过Chef本身分发数据包密钥本身就会破坏安全模型的整个点,并且毫无意义。 Read https://coderanger.net/two-pass/ to see how to force things to run at compile time. That said, please don't do this. Distributing the data bag key throu ...
-
您需要在本地创建文件,然后运行您的windows_zipfile资源。 您试图在您的仓库中解压缩文件,而不是在您的节点上解压缩。 这里的source是本地文件系统上的文件。 cookbook_file 'c:/testapp.zip' do source 'files/default/test_app.zip' end windows_zipfile 'c:/test_app' do source 'c:/testapp.zip' action :unzip not_if {::File. ...
-
有AFAIK没有“好/干净”的方式来复制包含在食谱中的所有文件。 为了创建多个文件,你可以应用简单的ruby逻辑来遍历这些文件: ['a.txt', 'b.txt'].each do |file| cookbook_file "/home/user/work/somelocation/#{file}" do source "folder-name/#{file}" mode "0644" end end 这将创建多个cookbook_file资源( @jamesgaddum正在谈论 ...
-
这是预期的行为。 验证校验和,并将本地缓存与远程进行比较。 此过程的速度完全取决于您的网络连接。 This is expected behavior. Checksums are verified and local cache is compared with the remote. The speed of this process is entirely dependent on your network connection.
-
问题是由于我自己的愚蠢。 数据库中的AWS凭证用于旧的已撤销AWS账户,但用于创建w / knife实例的AWS凭证是针对当前AWS账户的。 一旦他们正确的密钥被放入数据库,成功创建和附加卷! 希望这可以帮助将来的某个人...... The problem was due to my own foolishnes. The AWS credentials in the databag were for an old decomissioned AWS account but the AWS credenti ...