<commit_before>---
title: "Generated Code"
layout: docs
---

# Generated Code

This page describes the code generated by ScalaPB and how to use it.

## Default Package Structure

The generator will create a Scala file for each top-level message and enum
in your proto file. Using multiple files results in a better incremental
compilation performance.

The Scala package of the generated files will be determined as follows:

- If the `java_package` option is defined, then the Scala package will be
  `java_package.base_name` where `base_name` is the name of the proto file
  without the `.proto` extension.
- If the `java_package` is undefined, but the file specifies a package name
  via the `package` keyword then the Scala package will be
  `package.base_name`.
- If neither `java_package` nor `package` are specified, the Scala package
  will be just `base_name`.

From version 0.4.2, there is a way to customize the package name by using
[file-level options]({{site.baseurl}}/customizations.html).

## Messages

Each message corresponds to a final case class and a companion object.
Optional fields are wrapped in an `Option[]`, repeated fields are given as
`Seq[]`, and required fields (which you should not use, see "Required is
forever" in the [Language
Guide](https://developers.google.com/protocol-buffers/docs/proto#simple) are
normal members.

Note that in proto3, scalar (non-message) fields are not wrapped in `Option`,

For example, if your protocol buffer looks like this:

```protobuf
message Person {
    optional string name = 1;
    optional int32 age = 2;

    // Address is a message defined somewhere else.
    repeated Address addresses = 3;
}
```

...then the compiler will generate code that looks like this:

```scala
final case class Person(
    name: Option[String] = None,
    age: Option[Int] = None,
    addresses: Seq[Address] = Nil) extends GeneratedMessage {

    def toByteArray: Array[Byte] = { ... }

    // more stuff...
}

object Person extends GeneratedMessageCompanion[Person] {
    def parseFrom(bytes: Array[Byte]): Person = { ... }

    // more stuff...
}
```

The case class contains various methods for serialization, and the companion
object contains method for parsing. [See the source code for
`GeneratedMessage` and
`GeneratedMessageCompanion`](https://github.com/scalapb/ScalaPB/blob/master/scalapb-runtime/shared/src/main/scala/scalapb/GeneratedMessageCompanion.scala)<commit_msg>Add address protobuf definition (#663)<commit_after>---
title: "Generated Code"
layout: docs
---

# Generated Code

This page describes the code generated by ScalaPB and how to use it.

## Default Package Structure

The generator will create a Scala file for each top-level message and enum
in your proto file. Using multiple files results in a better incremental
compilation performance.

The Scala package of the generated files will be determined as follows:

- If the `java_package` option is defined, then the Scala package will be
  `java_package.base_name` where `base_name` is the name of the proto file
  without the `.proto` extension.
- If the `java_package` is undefined, but the file specifies a package name
  via the `package` keyword then the Scala package will be
  `package.base_name`.
- If neither `java_package` nor `package` are specified, the Scala package
  will be just `base_name`.

From version 0.4.2, there is a way to customize the package name by using
[file-level options]({{site.baseurl}}/customizations.html).

## Messages

Each message corresponds to a final case class and a companion object.
Optional fields are wrapped in an `Option[]`, repeated fields are given as
`Seq[]`, and required fields (which you should not use, see "Required is
forever" in the [Language
Guide](https://developers.google.com/protocol-buffers/docs/proto#simple) are
normal members.

Note that in proto3, scalar (non-message) fields are not wrapped in `Option`,

For example, if your protocol buffer looks like this:

```protobuf
message Person {
    optional string name = 1;
    optional int32 age = 2;

    repeated Address addresses = 3;
}

message Address {
  optional string street = 1;
  optional string city = 2;
}
```

...then the compiler will generate code that looks like this:

```scala
final case class Person(
    name: Option[String] = None,
    age: Option[Int] = None,
    addresses: Seq[Address] = Nil) extends GeneratedMessage {

    def toByteArray: Array[Byte] = { ... }

    // more stuff...
}

object Person extends GeneratedMessageCompanion[Person] {
    def parseFrom(bytes: Array[Byte]): Person = { ... }

    // more stuff...
}

// similar stuff for Address...
```

The case class contains various methods for serialization, and the companion
object contains method for parsing. [See the source code for
`GeneratedMessage` and
`GeneratedMessageCompanion`](https://github.com/scalapb/ScalaPB/blob/master/scalapb-runtime/shared/src/main/scala/scalapb/GeneratedMessageCompanion.scala)
{'fraction_non_alphanumeric': 0.08980827447023208, 'fraction_numerical': 0.004641775983854692, 'mean_word_length': 4.098765432098766, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4998511', 'n_tokens_mistral': 1409, 'n_tokens_neox': 1339, 'n_words': 600}