Class JsObjSpecBuilder
JsObjSpecBuilder
class is a builder for creating instances of JsObjSpec
with additional metadata.
It allows configuring the name, namespace, documentation, field orders, aliases, and default values for a JSON object
specification.
Example usage:
// Creating a simple JsObjSpec
JsObjSpec simpleSpec = JsObjSpecBuilder
.withName("Person")
.withNamespace("example.namespace")
.withDoc("Specification for representing a person")
.withFieldDocs(Map.of("name", "Full name of the person", "age", "Age of the person"))
.withFieldOrders(Map.of("name", MetaData.ORDERS.DESC))
.withFieldAliases(Map.of("firstName", List.of("name")))
.withAliases(List.of("PersonRecord"))
.withFieldsDefaults(Map.of("age", JsInt.of(30)))
.build(existingJsObjSpec);
// Creating a recursive JsObjSpec with defaults
JsObjSpec recursiveSpec = JsObjSpecBuilder.withName("person")
.withFieldsDefaults(Map.of("father", JsNull.NULL))
.build(JsObjSpec.of("name", JsSpecs.str(),
"age", JsSpecs.integer(),
"father", JsSpecs.ofNamedSpec("person").nullable()
)
.withOptKeys("father")
);
Note: When creating named specs using this builder, the specs are cached by `[namespace.]?name`. Aliases are also cached. Attempting to create two specs with the same full name will result in an error.
When dealing with recursive specifications or generating Avro schemas with [avro-spec](...), using the builder pattern is mandatory to create complex specifications. For example, the creation of a recursive JsObjSpec with defaults is shown above.
Field aliases and field defaults configured through this builder are utilized by the JsObjSpecParser
when
parsing JSON objects.
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionBuilds the final JsObjSpec by combining the specified configuration with the provided base JsObjSpec.withAliases
(List<String> aliases) Sets aliases for the JsObjSpec.Sets the documentation for the JsObjSpec.withFieldAliases
(Map<String, List<String>> fieldsAliases) Sets field aliases for the JsObjSpec.withFieldDocs
(Map<String, String> fieldsDoc) Sets the field-level documentation for the JsObjSpec.withFieldOrders
(Map<String, JsObjSpecBuilder.ORDERS> fieldsOrder) Sets the order for fields in the JsObjSpec.withFieldsDefaults
(Map<String, JsValue> fieldsDefaults) Sets default values for fields in the JsObjSpec.withMaxProperties
(int maxProperties) Sets the maximum number of properties that the JSON object must have.withMinProperties
(int minProperties) Sets the minimum number of properties that the JSON object must have.static JsObjSpecBuilder
Sets the name of the JSON object specification.withNamespace
(String nameSpace) Sets the namespace of the JSON object specification.
-
Method Details
-
withName
Sets the name of the JSON object specification. The name must follow the Avro naming conventions, adhering to the regex pattern:[A-Za-z_][A-Za-z0-9_]*
.- Parameters:
name
- The name of the JSON object specification.- Returns:
- A reference to this
JsObjSpecBuilder
instance for method chaining. - Throws:
IllegalArgumentException
- If the provided name does not follow the Avro naming conventions.
-
withNamespace
Sets the namespace of the JSON object specification. The namespace must follow the Avro naming conventions, adhering to the regex pattern:[A-Za-z_][A-Za-z0-9_.]+
. It should start with a letter or an underscore, followed by letters, numbers, underscores, or dots.- Parameters:
nameSpace
- The namespace of the JSON object specification.- Returns:
- A reference to this
JsObjSpecBuilder
instance for method chaining. - Throws:
IllegalArgumentException
- If the provided namespace does not follow the Avro naming conventions.
-
withDoc
Sets the documentation for the JsObjSpec.- Parameters:
doc
- The documentation for the JsObjSpec.- Returns:
- This JsObjSpecBuilder for method chaining.
-
withFieldDocs
Sets the field-level documentation for the JsObjSpec.- Parameters:
fieldsDoc
- A map containing field names as keys and their corresponding documentation as values.- Returns:
- This JsObjSpecBuilder for method chaining.
-
withFieldOrders
Sets the order for fields in the JsObjSpec.- Parameters:
fieldsOrder
- A map containing field names as keys and their corresponding order as values.- Returns:
- This JsObjSpecBuilder for method chaining.
-
withFieldAliases
Sets field aliases for the JsObjSpec.Field aliases are used by the `JsObjSpecParser` to replace an alias with its corresponding main field during JSON parsing. For example, if field 'b' is specified as an alias for field 'a', and the JSON input contains a field named 'b', the parser will replace 'b' with 'a'.
- Parameters:
fieldsAliases
- A map containing field names as keys and a list of their corresponding aliases as values.- Returns:
- This JsObjSpecBuilder for method chaining.
-
withMinProperties
Sets the minimum number of properties that the JSON object must have.- Parameters:
minProperties
- The minimum number of properties (inclusive).- Returns:
- This JsObjSpecBuilder instance for method chaining.
- Throws:
IllegalArgumentException
- If minProperties is negative.
-
withMaxProperties
Sets the maximum number of properties that the JSON object must have.- Parameters:
maxProperties
- The maximum number of properties (inclusive).- Returns:
- This JsObjSpecBuilder instance for method chaining.
- Throws:
IllegalArgumentException
- If maxProperties is negative.
-
withAliases
Sets aliases for the JsObjSpec. Must follow the avro naming conventions and, adhering to the regex pattern:[A-Za-z_][A-Za-z0-9_.]+
Aliases provide alternative names for the JsObjSpec, and they can be used interchangeably when referring to the same specification.
- Parameters:
aliases
- A list of alternative names (aliases) for the JsObjSpec.- Returns:
- This JsObjSpecBuilder for method chaining.
- Throws:
IllegalArgumentException
- If any of the provided aliases does not follow the naming pattern.
-
withFieldsDefaults
Sets default values for fields in the JsObjSpec.Default values are used when parsing JSON objects to replace missing fields with specified values. The provided default values must conform to the specifications of the corresponding fields in the JsObjSpec.
- Parameters:
fieldsDefaults
- A map containing field names and their corresponding default values.- Returns:
- This JsObjSpecBuilder for method chaining.
- Throws:
IllegalArgumentException
- If any default value does not conform to the field specification, or if a field with a default value is not defined in the JsObjSpec.- See Also:
-
build
Builds the final JsObjSpec by combining the specified configuration with the provided base JsObjSpec.This method validates and merges the configured metadata with the base JsObjSpec to create a new JsObjSpec. The resulting JsObjSpec is then cached for future reference.
- Parameters:
spec
- The base JsObjSpec to which the configured metadata is applied.- Returns:
- The final JsObjSpec with the specified configuration.
- Throws:
IllegalArgumentException
- If any configured metadata is invalid or conflicts with the base JsObjSpec.- See Also:
-