Search code examples
thrift

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


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?


Solution

  • 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/.