Skip to content
SandroGrzicic edited this page Aug 30, 2011 · 7 revisions

API

In addition to running the command-line interface, ScalaBuff can also be used programatically, from your own code.

For this purpose, the ScalaBuff object offers several methods. Each of these methods returns a ScalaClass case class that contains the String body of the generated class, along with the full path (package name) to the file and file name, if applicable.

  • apply(resourcePath: String): the default method, runs the ScalaBuff generator on the specified resource path (file path or URL).

  • fromString(input: String): instead of parsing the parameter as a file path or URL, ScalaBuff.fromString takes the input directly as a String.

The processing is split into two phases: Parsing the input and generating the output using the parsed input. On a parsing error, a ParsingFailureException is thrown. If parsing succeeds but generation fails, a GenerationFailureException will be thrown. Both of these RuntimeExceptions contain a helpful message regarding what went wrong with processing the input.

Parser

If you'd like to utilize ScalaBuff's Parser, you can do so by utilizing any of the Parser.apply methods. Each one is suited for a particular input: a java.io.Reader, java.io.InputStream, java.io.File and a simple String. Optionally, you can specify the input name if not passing a File, and the input encoding if passing an InputStream. All of these methods return a List[Node], which can then be used directly as an input to the Generator, or for other purposes in your code.

Examples

Parser(
    new File("some/proto/file.proto")
)

Parser(
    "package test; message TestMessage { required int32 field_name = 1; } }", "testProto"
)

Generator

The Generator.apply method takes a List[Node] tree structure and a source file name and returns the resulting generated ScalaClass object. Since the parsing and generation phases are separated, it is possible to take the output from the Parser, modify it and then call Generator.apply with the modified input.

Examples

Generator(
    Parser(
        "package test; message TestMessage { required int32 field_name = 1; } }", "testProto"
    )
)
Clone this wiki locally