[
https://issues.apache.org/jira/browse/AVRO-2299?focusedWorklogId=734384&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-734384
]
ASF GitHub Bot logged work on AVRO-2299:
----------------------------------------
Author: ASF GitHub Bot
Created on: 01/Mar/22 07:41
Start Date: 01/Mar/22 07:41
Worklog Time Spent: 10m
Work Description: rayokota commented on a change in pull request #805:
URL: https://github.com/apache/avro/pull/805#discussion_r816516049
##########
File path: lang/java/avro/src/main/java/org/apache/avro/SchemaNormalization.java
##########
@@ -160,14 +200,73 @@ private static Appendable build(Map<String, String> env,
Schema s, Appendable o)
else
firstTime = false;
o.append("{\"name\":\"").append(f.name()).append("\"");
- build(env, f.schema(), o.append(",\"type\":")).append("}");
+ build(env, f.schema(), o.append(",\"type\":"), ps, aps);
+ if (!ps)
+ setFieldProps(o, f, aps); // if standard canonical form then add
reserved properties
+ o.append("}");
}
o.append("]");
}
+ if (!ps) {
+ setComplexProps(o, s);
+ setSimpleProps(o, s.getObjectProps(), aps);
+ } // adding the reserved property if not parser canonical schema
return o.append("}");
}
}
+ private static Appendable writeLogicalType(Schema s, LogicalType lt,
Appendable o, LinkedHashSet<String> aps)
+ throws IOException {
+ o.append("{\"type\":\"").append(s.getType().getName()).append("\"");
+ // adding the logical property
+ setLogicalProps(o, lt);
+ // adding the reserved property
+ setSimpleProps(o, s.getObjectProps(), aps);
+ return o.append("}");
+ }
+
+ private static void setLogicalProps(Appendable o, LogicalType lt) throws
IOException {
+
o.append(",\"").append(LogicalType.LOGICAL_TYPE_PROP).append("\":\"").append(lt.getName()).append("\"");
+ if (lt.getName().equals("decimal")) {
+ LogicalTypes.Decimal dlt = (LogicalTypes.Decimal) lt;
+ o.append(",\"precision\":").append(Integer.toString(dlt.getPrecision()));
+ if (dlt.getScale() != 0)
+ o.append(",\"scale\":").append(Integer.toString(dlt.getScale()));
+ }
+ }
+
+ private static void setSimpleProps(Appendable o, Map<String, Object>
schemaProps, LinkedHashSet<String> aps)
+ throws IOException {
+ for (String propKey : aps) {
+ if (schemaProps.containsKey(propKey)) {
+ String propValue =
JacksonUtils.toJsonNode(schemaProps.get(propKey)).toString();
+ o.append(",\"").append(propKey).append("\":").append(propValue);
+ }
+ }
+ }
+
+ private static void setComplexProps(Appendable o, Schema s) throws
IOException {
+ if (s.getDoc() != null && !s.getDoc().isEmpty())
+ o.append(",\"doc\":\"").append(s.getDoc()).append("\"");
+ if (s.getAliases() != null && !s.getAliases().isEmpty())
+ o.append(",\"aliases\":").append(JacksonUtils.toJsonNode(new
TreeSet<String>(s.getAliases())).toString());
+ if (s.getType() == Schema.Type.ENUM && s.getEnumDefault() != null) {
+
o.append(",\"default\":").append(JacksonUtils.toJsonNode(s.getEnumDefault()).toString());
+ }
+ }
+
+ private static void setFieldProps(Appendable o, Schema.Field f,
LinkedHashSet<String> aps) throws IOException {
+ if (f.order() != null)
+ o.append(",\"order\":\"").append(f.order().toString()).append("\"");
+ if (f.doc() != null)
+ o.append(",\"doc\":\"").append(f.doc()).append("\"");
Review comment:
This should be
`o.append(",\"doc\":").append(toJsonNode(f.doc()).toString());`
##########
File path: lang/java/avro/src/main/java/org/apache/avro/SchemaNormalization.java
##########
@@ -160,14 +200,73 @@ private static Appendable build(Map<String, String> env,
Schema s, Appendable o)
else
firstTime = false;
o.append("{\"name\":\"").append(f.name()).append("\"");
- build(env, f.schema(), o.append(",\"type\":")).append("}");
+ build(env, f.schema(), o.append(",\"type\":"), ps, aps);
+ if (!ps)
+ setFieldProps(o, f, aps); // if standard canonical form then add
reserved properties
+ o.append("}");
}
o.append("]");
}
+ if (!ps) {
+ setComplexProps(o, s);
+ setSimpleProps(o, s.getObjectProps(), aps);
+ } // adding the reserved property if not parser canonical schema
return o.append("}");
}
}
+ private static Appendable writeLogicalType(Schema s, LogicalType lt,
Appendable o, LinkedHashSet<String> aps)
+ throws IOException {
+ o.append("{\"type\":\"").append(s.getType().getName()).append("\"");
+ // adding the logical property
+ setLogicalProps(o, lt);
+ // adding the reserved property
+ setSimpleProps(o, s.getObjectProps(), aps);
+ return o.append("}");
+ }
+
+ private static void setLogicalProps(Appendable o, LogicalType lt) throws
IOException {
+
o.append(",\"").append(LogicalType.LOGICAL_TYPE_PROP).append("\":\"").append(lt.getName()).append("\"");
+ if (lt.getName().equals("decimal")) {
+ LogicalTypes.Decimal dlt = (LogicalTypes.Decimal) lt;
+ o.append(",\"precision\":").append(Integer.toString(dlt.getPrecision()));
+ if (dlt.getScale() != 0)
+ o.append(",\"scale\":").append(Integer.toString(dlt.getScale()));
+ }
+ }
+
+ private static void setSimpleProps(Appendable o, Map<String, Object>
schemaProps, LinkedHashSet<String> aps)
+ throws IOException {
+ for (String propKey : aps) {
+ if (schemaProps.containsKey(propKey)) {
+ String propValue =
JacksonUtils.toJsonNode(schemaProps.get(propKey)).toString();
+ o.append(",\"").append(propKey).append("\":").append(propValue);
+ }
+ }
+ }
+
+ private static void setComplexProps(Appendable o, Schema s) throws
IOException {
+ if (s.getDoc() != null && !s.getDoc().isEmpty())
+ o.append(",\"doc\":\"").append(s.getDoc()).append("\"");
Review comment:
This should be
`o.append(",\"doc\":").append(toJsonNode(s.getDoc()).toString());`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
Issue Time Tracking
-------------------
Worklog Id: (was: 734384)
Remaining Estimate: 0h
Time Spent: 10m
> Get Plain Schema
> ----------------
>
> Key: AVRO-2299
> URL: https://issues.apache.org/jira/browse/AVRO-2299
> Project: Apache Avro
> Issue Type: New Feature
> Components: java
> Affects Versions: 1.9.0, 1.8.2, 1.9.1
> Reporter: Rumeshkrishnan Mohan
> Assignee: Doug Cutting
> Priority: Major
> Labels: features
> Time Spent: 10m
> Remaining Estimate: 0h
>
> {panel:title=Avro Schema Reserved Keys:}
> "doc", "fields", "items", "name", "namespace",
> "size", "symbols", "values", "type", "aliases", "default"
> {panel}
> AVRO also supports user defined properties for both Schema and Field.
> Is there way to get the schema with reserved property (key, value)?
> Input Schema:
> {code:java}
> {
> "name": "testSchema",
> "namespace": "com.avro",
> "type": "record",
> "fields": [
> {
> "name": "email",
> "type": "string",
> "doc": "email id",
> "user_field_prop": "xxxxx"
> }
> ],
> "user_schema_prop": "xxxxxx"
> }{code}
> Expected Plain Schema:
> {code:java}
> {
> "name": "testSchema",
> "namespace": "com.avro",
> "type": "record",
> "fields": [
> {
> "name": "email",
> "type": "string",
> "doc": "email id"
> }
> ]
> }
> {code}
--
This message was sent by Atlassian Jira
(v8.20.1#820001)