Last active
March 19, 2021 14:13
-
-
Save gontard/ba73d5f32cfe15080edbe4280b25d71e to your computer and use it in GitHub Desktop.
Customize ScalaPB code generation using ScalaPB transformations on protoc-gen-validate rules
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
syntax = "proto2"; | |
package teads.api; | |
import "validate/validate.proto"; | |
message CreateAccountRequest { | |
required string first_name = 1 [(validate.rules).string.max_len = 255]; | |
required string last_name = 2 [(validate.rules).string.max_len = 255]; | |
required string email = 3 [(validate.rules).string.email = true]; | |
optional string website = 4 [(validate.rules).string.uri = true]; | |
repeated string roles = 5 [(validate.rules).repeated = {min_items: 1, unique: true}]; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package teads.api | |
final case class CreateAccountRequest( | |
firstName: String, | |
lastName: String, | |
email: Email, | |
website: Option[URI] = None, | |
roles: cats.data.NonEmptySet[String] | |
) extends scalapb.GeneratedMessage |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package teads.api; | |
import "scalapb/scalapb.proto"; | |
import "scalapb/validate.proto"; | |
import "validate/validate.proto"; | |
option (scalapb.options) = { | |
scope: PACKAGE | |
field_transformations: [ | |
{ | |
when: {options: {[validate.rules] {string {email: true }}}} | |
set: {[scalapb.field] {type: "teads.api.Email" }} | |
}, | |
{ | |
when: {options: {[validate.rules] {string {uri: true }}}} | |
set: {[scalapb.field] {type: "teads.api.URI" }} | |
} | |
] | |
// there are even built-in transformations for cats NonEmpty collections! | |
preprocessors: ["scalapb-validate-preprocessor"] | |
[scalapb.validate.file] { | |
cats_transforms : true | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment