如何从 Play 框架中的 YAML 固定装置加载(静态嵌套)枚举值?

发布于 2024-10-07 01:49:54 字数 465 浏览 0 评论 0原文

public class Request extends Model {
    public static enum Category {
        First, Second, Third
    }
        public Category category;
}

我似乎无法在 YAML 测试装置/初始数据中正确创建带有类别的请求。我尝试过以下方法:

Request(areq):
    category: Request.Category.Third

以及其他几种变体。没有什么真正有效。 SnakeYAML 页面 给了我一些诱人的提示,但我不明白如何正确引用我的应用程序的包。正确的语法是什么?

public class Request extends Model {
    public static enum Category {
        First, Second, Third
    }
        public Category category;
}

I seem unable to properly create a Request with a Category in my YAML test fixtures / initial data. I've tried things like:

Request(areq):
    category: Request.Category.Third

And several other variations. Nothing really works. The SnakeYAML page gives me some tantalizing hints but I don't see how to properly reference my app's packages. What is the right syntax for this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦里的微风 2024-10-14 01:49:54

为什么不只使用

...
category: Third

SnakeYAML 应该识别类别的类型并在对象构造期间将第三个字符串转换为类别枚举的第三个值。

Why don't you use just

...
category: Third

SnakeYAML should recognize type of category and convert Third string to Third value of Category enum during object construction.

魂ガ小子 2024-10-14 01:49:54

在Play1.2.x中您还可以使用这种形式:

Fixtures:

EnumTest(enumtest01):
  status: ${models.EnumTest.Status.ACTIVE}

EnumTest(enumtest02):
  status: ${models.EnumTest.Status.DELETED}

对于此模型:

@Entity
public class EnumTest extends Model {

    public enum Status {
        ACTIVE, DELETED
    }

    public Status status;

}

就像您可以在routes和application.conf文件中使用Groovy模板一样,您也可以在yml文件中使用。

In Play1.2.x you can also use this form:

Fixtures:

EnumTest(enumtest01):
  status: ${models.EnumTest.Status.ACTIVE}

EnumTest(enumtest02):
  status: ${models.EnumTest.Status.DELETED}

For this model:

@Entity
public class EnumTest extends Model {

    public enum Status {
        ACTIVE, DELETED
    }

    public Status status;

}

Like you can use Groovy templates in the routes, and application.conf file, you can also use in the yml files.

Hello爱情风 2024-10-14 01:49:54

老实说,我不是一个有经验的用户,但我认为 Request 是一个不好的名字,因为该类存在于 play-framework 中。所以我建议重命名它。但对于你的问题来说,这是没有必要的。使用完整名称,即 models.Request.Category.Third

To be honest I'm not an experienced user, but I think Request is a bad name, because the class exists in the play-framework. So I would recommend to rename it. But for your problem it's not necessary. Use the complete name, meaning models.Request.Category.Third.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文