Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite Java Doc logic with JDK 17 compatible APIs #902

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ and what APIs have changed, if applicable.

## [Unreleased]

## [29.42.2] - 2023-05-11
- Fix synchronization on `RequestContext` to prevent `ConcurrentModificationException`.

## [29.42.1] - 2023-05-11
- Add support for returning location of schema elements from the PDL schema encoder.

## [29.42.0] - 2023-05-02
- Remove the overriding of content-length for HEADER requests as per HTTP Spec
More details about this issue can be found @ https://jira01.corp.linkedin.com:8443/browse/SI-31814

## [29.41.12] - 2023-04-06
- Introduce `@extension.injectedUrnParts` ER annotation.
- This will be used as the replacement for using `@extension.params` to specify injected URN parts.
Expand Down Expand Up @@ -5458,7 +5468,10 @@ patch operations can re-use these classes for generating patch messages.

## [0.14.1]

[Unreleased]: https://github.com/linkedin/rest.li/compare/v29.41.12...master
[Unreleased]: https://github.com/linkedin/rest.li/compare/v29.42.2...master
[29.42.2]: https://github.com/linkedin/rest.li/compare/v29.42.1...v29.42.2
[29.42.1]: https://github.com/linkedin/rest.li/compare/v29.42.0...v29.42.1
[29.42.0]: https://github.com/linkedin/rest.li/compare/v29.41.12...v29.42.0
[29.41.12]: https://github.com/linkedin/rest.li/compare/v29.41.11...v29.41.12
[29.41.11]: https://github.com/linkedin/rest.li/compare/v29.41.10...v29.41.11
[29.41.10]: https://github.com/linkedin/rest.li/compare/v29.41.9...v29.41.10
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ allprojects {
throw new GradleScriptException("Pegasus required Java 8 or later to build, current version: ${JavaVersion.current()}", null)
}
// for all supported versions that we test build, fail the build if any compilation warnings are reported
compile.options.compilerArgs = ['-Xlint', '-Xlint:-path', '-Xlint:-static', '-Werror']
compile.options.compilerArgs = ['-Xlint', '-Xlint:-path', '-Xlint:-static']
}

tasks.withType(Javadoc)
Expand Down
2 changes: 1 addition & 1 deletion build_script/restModel.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ project.sourceSets.all { SourceSet sourceSet ->
project.tasks[sourceSet.compileJavaTaskName].dependsOn(rootProject.ext.build.restModelGenerateTasks[sourceSet])
}

final Task jarTask = project.tasks[sourceSet.getTaskName('', 'jar')]
final Task jarTask = project.tasks[sourceSet.getName().endsWith('11') ? 'jar' : sourceSet.getTaskName('', 'jar')]
jarTask.from(inputParentDirPath) {
include "${pegasusDirName}${File.separatorChar}**${File.separatorChar}*.pdsc"
include "${pegasusDirName}${File.separatorChar}**${File.separatorChar}*.pdl"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@

import com.linkedin.data.DataList;
import com.linkedin.data.DataMap;
import com.linkedin.data.schema.grammar.PdlSchemaParser;
import com.linkedin.util.LineColumnNumberWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Collections;
import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -107,15 +110,45 @@ PdlBuilder newBuilderInstance(Writer writer)
private String _namespace = "";
private String _package = "";

private final boolean _trackWriteLocations;

private final Map<Object, PdlSchemaParser.ParseLocation> _writeLocations;

/**
* Construct a .pdl source code encoder.
* The encoding style defaults to {@link EncodingStyle#INDENTED} but may be changed by calling
* {@link #setEncodingStyle(EncodingStyle)}.
*
* @param out provides the encoded .pdl destination.
*/
public SchemaToPdlEncoder(Writer out)
{
_writer = out;
_encodingStyle = EncodingStyle.INDENTED;
this(out, false);
}

/**
* Construct a .pdl source code encoder with the option to track line/column of schema elements during writing.
* The encoding style defaults to {@link EncodingStyle#INDENTED} but may be changed by calling
* {@link #setEncodingStyle(EncodingStyle)}.
*
* @param out provides the encoded .pdl destination.
* @param returnContextLocations Enable recording the context locations of schema elements during parsing. The
* locations can be retrieved using {@link #getWriteLocations()} after parsing.
*/
public SchemaToPdlEncoder(Writer out, boolean returnContextLocations)
{
if (returnContextLocations)
{
_writeLocations = new IdentityHashMap<>();
// Wrap the Writer to track line/column numbers to report to elementWriteListener
_writer = new LineColumnNumberWriter(out);
} else
{
_writer = out;
_writeLocations = Collections.emptyMap();
}
setEncodingStyle(EncodingStyle.INDENTED);
_trackWriteLocations = returnContextLocations;
}

/**
Expand All @@ -126,6 +159,18 @@ public SchemaToPdlEncoder(Writer out)
public void setEncodingStyle(EncodingStyle encodingStyle)
{
_encodingStyle = encodingStyle;

// When counting column numbers, CompactPDLBuilder treats ',' as whitespace
if (_writer instanceof LineColumnNumberWriter)
{
if (_encodingStyle == EncodingStyle.COMPACT)
{
((LineColumnNumberWriter) _writer).setIsWhitespaceFunction(c -> Character.isWhitespace(c) || c == ',');
} else
{
((LineColumnNumberWriter) _writer).setIsWhitespaceFunction(Character::isWhitespace);
}
}
}

/**
Expand All @@ -150,10 +195,12 @@ public void encode(DataSchema schema) throws IOException
{
if (hasNamespace)
{
markSchemaElementStartLocation();
_builder.write("namespace")
.writeSpace()
.writeIdentifier(namedSchema.getNamespace())
.newline();
recordSchemaElementLocation(namedSchema.getNamespace());
_namespace = namedSchema.getNamespace();
}
if (hasPackage)
Expand Down Expand Up @@ -220,12 +267,14 @@ private void writeInlineSchema(DataSchema schema) throws IOException
.increaseIndent();
if (hasNamespaceOverride)
{
markSchemaElementStartLocation();
_builder
.indent()
.write("namespace")
.writeSpace()
.writeIdentifier(namedSchema.getNamespace())
.newline();
recordSchemaElementLocation(namedSchema.getNamespace());
_namespace = namedSchema.getNamespace();
}
if (hasPackageOverride)
Expand Down Expand Up @@ -291,8 +340,14 @@ private void writeInlineSchema(DataSchema schema) throws IOException
}
}

public Map<Object, PdlSchemaParser.ParseLocation> getWriteLocations()
{
return _writeLocations;
}

private void writeRecord(RecordDataSchema schema) throws IOException
{
markSchemaElementStartLocation();
writeDocAndProperties(schema);
_builder.write("record")
.writeSpace()
Expand Down Expand Up @@ -327,6 +382,7 @@ private void writeRecord(RecordDataSchema schema) throws IOException
{
writeIncludes(schema, includes);
}
recordSchemaElementLocation(schema);
}

/**
Expand All @@ -335,6 +391,7 @@ private void writeRecord(RecordDataSchema schema) throws IOException
*/
private void writeField(RecordDataSchema.Field field) throws IOException
{
markSchemaElementStartLocation();
writeDocAndProperties(field);
_builder.indent()
.writeIdentifier(field.getName())
Expand All @@ -353,6 +410,7 @@ private void writeField(RecordDataSchema.Field field) throws IOException
.writeSpace()
.writeJson(field.getDefault(), field.getType());
}
recordSchemaElementLocation(field);
_builder.newline();
}

Expand Down Expand Up @@ -382,6 +440,7 @@ private void writeEnum(EnumDataSchema schema) throws IOException
DataSchemaConstants.DEPRECATED_SYMBOLS_KEY,
properties.get(DataSchemaConstants.DEPRECATED_SYMBOLS_KEY));

markSchemaElementStartLocation();
writeDocAndProperties(schema);
_builder.write("enum")
.writeSpace()
Expand All @@ -395,6 +454,7 @@ private void writeEnum(EnumDataSchema schema) throws IOException

for (String symbol : schema.getSymbols())
{
markSchemaElementStartLocation();
String docString = docs.get(symbol);
DataMap symbolProperties = coercePropertyToDataMapOrFail(schema,
DataSchemaConstants.SYMBOL_PROPERTIES_KEY + "." + symbol,
Expand All @@ -414,24 +474,29 @@ private void writeEnum(EnumDataSchema schema) throws IOException
_builder.indent()
.writeIdentifier(symbol)
.newline();
recordSchemaElementLocation(symbol);
}
_builder.decreaseIndent()
.indent()
.write("}");
recordSchemaElementLocation(schema);
}

private void writeFixed(FixedDataSchema schema) throws IOException
{
markSchemaElementStartLocation();
writeDocAndProperties(schema);
_builder.write("fixed")
.writeSpace()
.writeIdentifier(schema.getName())
.writeSpace()
.write(String.valueOf(schema.getSize()));
recordSchemaElementLocation(schema);
}

private void writeTyperef(TyperefDataSchema schema) throws IOException
{
markSchemaElementStartLocation();
writeDocAndProperties(schema);
_builder.write("typeref")
.writeSpace()
Expand All @@ -441,24 +506,29 @@ private void writeTyperef(TyperefDataSchema schema) throws IOException
.writeSpace();
DataSchema ref = schema.getRef();
writeReferenceOrInline(ref, schema.isRefDeclaredInline());
recordSchemaElementLocation(schema);
}

private void writeMap(MapDataSchema schema) throws IOException
{
markSchemaElementStartLocation();
writeProperties(schema.getProperties());
_builder.write("map[string")
.writeComma()
.writeSpace();
writeReferenceOrInline(schema.getValues(), schema.isValuesDeclaredInline());
_builder.write("]");
recordSchemaElementLocation(schema);
}

private void writeArray(ArrayDataSchema schema) throws IOException
{
markSchemaElementStartLocation();
writeProperties(schema.getProperties());
_builder.write("array[");
writeReferenceOrInline(schema.getItems(), schema.isItemsDeclaredInline());
_builder.write("]");
recordSchemaElementLocation(schema);
}

/**
Expand All @@ -467,6 +537,7 @@ private void writeArray(ArrayDataSchema schema) throws IOException
*/
private void writeUnion(UnionDataSchema schema) throws IOException
{
markSchemaElementStartLocation();
writeProperties(schema.getProperties());
_builder.write("union[");
final boolean useMultilineFormat = schema.areMembersAliased() || schema.getMembers().size() >= UNION_MULTILINE_THRESHOLD;
Expand Down Expand Up @@ -496,6 +567,7 @@ private void writeUnion(UnionDataSchema schema) throws IOException
.indent();
}
_builder.write("]");
recordSchemaElementLocation(schema);
}

/**
Expand All @@ -505,6 +577,7 @@ private void writeUnion(UnionDataSchema schema) throws IOException
*/
private void writeUnionMember(UnionDataSchema.Member member, boolean useMultilineFormat) throws IOException
{
markSchemaElementStartLocation();
if (member.hasAlias())
{
if (StringUtils.isNotBlank(member.getDoc()) || !member.getProperties().isEmpty() || member.isDeclaredInline())
Expand All @@ -524,6 +597,7 @@ else if (useMultilineFormat)
_builder.indent();
}
writeReferenceOrInline(member.getType(), member.isDeclaredInline());
recordSchemaElementLocation(member);
}

private void writePrimitive(PrimitiveDataSchema schema) throws IOException
Expand Down Expand Up @@ -865,4 +939,25 @@ else if (_namespace.equals(schema.getNamespace()) && !_importsByLocalName.contai
_builder.writeIdentifier(schema.getFullName());
}
}

void markSchemaElementStartLocation()
{
if (_trackWriteLocations)
{
((LineColumnNumberWriter) _writer).saveCurrentPosition();
}
}

private void recordSchemaElementLocation(Object schemaElement)
{
if (_trackWriteLocations)
{
LineColumnNumberWriter.CharacterPosition startPosition = ((LineColumnNumberWriter) _writer).popSavedPosition();
LineColumnNumberWriter.CharacterPosition endPosition =
((LineColumnNumberWriter) _writer).getLastNonWhitespacePosition();
_writeLocations.put(schemaElement,
new PdlSchemaParser.ParseLocation(startPosition.getLine(), startPosition.getColumn(), endPosition.getLine(),
endPosition.getColumn()));
}
}
}
Loading