Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to define a numeric range for a field in Protocol Buffer?

I live in a very safety/critical Ada-ic environment, and have taken the habit of having well defined and bounded type definition. I am also confronted to lots ASN1 usage/evangelists.

One of the key features that some people around me define as superior in ASN1 over protocol buffers is the ability to define, and therefore check, data range.

So,

  • Is it true that protocol buffers do not have any range definition system ?
  • Are there any means on top of protocol buffer that could provide such a feature ?

Usual numerical types definition such as int32, int64 is out of scope here.


I read this Google overview and found no reference to any data range definition. Except for features concerning the enum management/representation, and some ordering features.

To give some other references, I heard of/read the links below and became aware of an Ada implementation project.

Does anyone know of an Ada plugin for protocol buffers?

which led to

http://www.diva-portal.org/smash/get/diva2:690878/FULLTEXT02.pdf

https://github.com/persan/protobuf-ada

like image 451
LoneWanderer Avatar asked Mar 03 '23 07:03

LoneWanderer


2 Answers

It's true that protocol buffers do not have any built-in mechanism for numeric range.

The syntax however does support defining custom options:

extend google.protobuf.FieldOptions {
     optional int32 maxval = 50001;
     optional int32 minval = 50002;
}

message MyMessage {
     required int32 month = 1 [(minval) = 1, (maxval) = 12];
}

Depending on library used, these can then either be accessed in runtime using reflection, or you can use a custom code generator to automatically generate verification code.

like image 95
jpa Avatar answered Apr 27 '23 17:04

jpa


Is it true that protocol buffers do not have any range definition system ?

Yes

Are there any means on top of protocol buffer that could provide such a feature ?

You write code that checks the values are in the expected range.

like image 24
Marc Gravell Avatar answered Apr 27 '23 18:04

Marc Gravell