What is an “annotation” in Apache Thrift, and what is it used for?

巧了我就是萌 提交于 2019-12-24 10:44:59

问题


The Thrift parser has a definition for an "annotation":

https://git-wip-us.apache.org/repos/asf?p=thrift.git;a=blob;f=compiler/cpp/src/thrifty.yy;h=da5c562f9d7adf87b21e4c3ae30fbb9657e0c8b3;hb=HEAD#l85

What are these meant to be used for, and can the information that they contain be accessed in the generated client and/or server code?


回答1:


Annotations are used to associate metadata with types defined in the Thrift definition (".thrift") file. The AnnotationThrift.test file in the source distribution has examples.

Here, for instance, is a struct with annotations (in parentheses):

struct foo {
  1: i32 bar ( presence = "required" );
  2: i32 baz ( presence = "manual", cpp.use_pointer = "", );
  3: i32 qux;
  4: i32 bop;
} (
  cpp.type = "DenseFoo",
  python.type = "DenseFoo",
  java.final = "",
  annotation.without.value,
)

Looking at the code, it seems annotations are only ever used to provide directives to the compiler—for instance the C++ compiler uses the cpp.type annotation, if it's present, to override a type's name in the generated code.

I see nothing that suggests the annotations themselves are ever reproduced in or accessible to the generated code, though if such code does exist it'd be located in compiler/cpp/src/generate/.




回答2:


As Simon pointed out, annotations can be imagined as extension points to the basic IDL language to control certain aspects of the code generation. They are quite often language specific, but not limited to that. In some cases annotations have become part of the core IDl later. Another thing is, that annotations are still quite poorly documented (I'm being kind here).

can the information that they contain be accessed in the generated client and/or server code?

It depends on the annotation and on what you mean by "accessed". If you change the base class by means of an annotation, or if you make a Java class final, the information is of course present in the generated code.




回答3:


We have leveraged annotations via a Go Thrift parser. Examples include a Thrift linter where annotations selectively disable checks.



来源:https://stackoverflow.com/questions/25631235/what-is-an-annotation-in-apache-thrift-and-what-is-it-used-for

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!