Enumerations

Enumerations #

Before you can use enums in muSpecs, make sure that you define the globs in your .furo file.

1
2
3
4
muSpec:
  enums: # define a set of globs which matches your type definitions
    - "./muspecs/**/*enums.yaml"
    - "./muspecs/*enums.yaml"

If you want to use enums with grpc-gateway and the furo-web components, do not forget to set the MarshalOption UseEnumNumbers: true, .

Read mor about customizing your gateway here.

customized marshaller

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
gwruntime.WithMarshalerOption(gwruntime.MIMEWildcard, &marshaller.HTTPBodyMarshaler{
			Marshaler: &gwruntime.JSONPb{
				MarshalOptions: protojson.MarshalOptions{
					UseProtoNames:   true,
					EmitUnpopulated: false,
					UseEnumNumbers:  true,
				},
				UnmarshalOptions: protojson.UnmarshalOptions{
					DiscardUnknown: true,
				},
			},
		}),

Define enums in µSpec #

You can define multiple enums in a µSpec enum file.

It is possible to set the target: to a file which contains messages. It is not possible to mix them with service protos.

1
2
3
4
5
6
7
8
- enum: 'helloworld.Corpus #Description for a enum sample with aliases'
  values:
    UNKNOWN: 0
    STARTED: 1
    RUNNING: 1
    COMPLETE: 2
  target: enums.proto
  alias: true # this is needed when you want to allow aliases

sample.enums.yaml

As you can see, the Corpus enum’s first constant maps to zero: every enum definition must contain a constant that maps to zero as its first element. This is needed for the compatibility with protobuf.

Define enums in specs #

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
type: Corpus
description: Description for a enum sample with aliases
lifecycle: null
__proto:
    package: helloworld
    targetfile: enums.proto
    imports: []
    options:
        go_package: github.com/yourname/sample-specs/dist/pb/helloworld;helloworldpb
        java_multiple_files: "true"
        java_outer_classname: Corpus.EnumProto
        java_package: com.example.tutorial.enums
    allow_alias: true
values:
    UNKNOWN: 0
    STARTED: 1
    RUNNING: 1
    COMPLETE: 2

sample.enum.spec

Resulting proto file #

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
// Code generated by furo-proto-gen. DO NOT EDIT.
syntax = "proto3";
package helloworld;
option go_package = "github.com/yourname/sample-specs/dist/pb/helloworld;helloworldpb";
option java_multiple_files = true;
option java_outer_classname = "EnumsProto";
option java_package = "com.example.tutorial.enums";

// Description for a enum sample with aliases
enum Corpus {
  UNKNOWN = 0;
  STARTED = 1;
  RUNNING = 1;
  COMPLETE = 2;
  option allow_alias = true;
}

enum.proto

Enum in ES6Module (web client) #

Because js does not have enums, the fields with a enum type are generated as uint32 with an optionlist in the meta. The furo-ui5-select component can handle this metas.