Class ProtoSchema

java.lang.Object
org.microproxy.contentviews.ProtoSchema

public final class ProtoSchema extends Object
Protobuf type definitions, read from a FileDescriptorSet (what protoc --descriptor_set_out=types.desc --include_imports writes), so that messages decode with field names, enum names and declared types instead of guesses, and gRPC calls decode with their method's input and output types:

 ProtoSchema schema = ProtoSchema.load(Path.of("types.desc"));
 ProtoMessage m = Protobuf.decode(body, schema, "example.Order");
 ContentViews views = ContentViews.defaults().withSchema(schema);
 

The descriptor set is parsed with this package's own protobuf decoder; no protobuf library is involved. Every schema also knows a few well-known types, which gRPC error details use: google.protobuf.Any and Duration, google.rpc.Status and the standard error details (ErrorInfo, RetryInfo, BadRequest, ...). Definitions in the descriptor set take precedence over them. Extensions, options other than packed and map_entry, and default values are ignored.

  • Method Details

    • empty

      public static ProtoSchema empty()
      Returns a schema with only the well-known types.
      Returns:
      a schema with only the well-known types
    • load

      public static ProtoSchema load(Path file) throws IOException
      Reads a FileDescriptorSet file, as protoc --descriptor_set_out writes it.
      Parameters:
      file - the descriptor set
      Returns:
      the schema
      Throws:
      IOException - if the file cannot be read, or is not a valid descriptor set
    • parse

      public static ProtoSchema parse(byte[] fileDescriptorSet) throws DecodeException
      Parses a serialized FileDescriptorSet.
      Parameters:
      fileDescriptorSet - the descriptor set's bytes
      Returns:
      the schema, which also knows the well-known types
      Throws:
      DecodeException - if the bytes are not a valid descriptor set
    • merge

      public ProtoSchema merge(ProtoSchema other)
      This schema with the definitions of other added; where both define a name, other's definition wins.
      Parameters:
      other - the schema to add
      Returns:
      the combined schema
    • message

      public Optional<ProtoSchema.MessageType> message(String fullName)
      The message type named fullName.
      Parameters:
      fullName - the full name, with or without a leading dot
      Returns:
      the type, or empty if the schema has none
    • enumType

      public Optional<ProtoSchema.EnumType> enumType(String fullName)
      The enum type named fullName.
      Parameters:
      fullName - the full name, with or without a leading dot
      Returns:
      the type, or empty if the schema has none
    • method

      public Optional<ProtoSchema.Method> method(String path)
      The RPC method a gRPC request path names.
      Parameters:
      path - the request path, /<package>.<Service>/<Method>
      Returns:
      the method, or empty if the schema has none
    • messageFor

      public Optional<ProtoSchema.MessageType> messageFor(String path, boolean request)
      The message type of a gRPC call's requests or responses.
      Parameters:
      path - the request path, /<package>.<Service>/<Method>
      request - whether the messages are requests (the method's input type) or responses
      Returns:
      the type, or empty if the schema does not know the method or its types
    • messages

      public Map<String,ProtoSchema.MessageType> messages()
      Returns the message types, by full name.
      Returns:
      the message types, by full name
    • enums

      public Map<String,ProtoSchema.EnumType> enums()
      Returns the enum types, by full name.
      Returns:
      the enum types, by full name
    • methods

      public Collection<ProtoSchema.Method> methods()
      Returns the RPC methods, by request path.
      Returns:
      the RPC methods, by request path
    • toString

      public String toString()
      Overrides:
      toString in class Object