-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
work on new code-gen - generate SCHEMA$ for enum classes (#310)
- Loading branch information
1 parent
a21320d
commit 2c4f0da
Showing
10 changed files
with
213 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
avro-codegen/src/test/resources/schemas/SimpleEnumWithHugeDoc.avsc
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
helper/helper-common/src/main/java/com/linkedin/avroutil1/compatibility/SourceCodeUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright 2022 LinkedIn Corp. | ||
* Licensed under the BSD 2-Clause License (the "License"). | ||
* See License in the project root for license information. | ||
*/ | ||
|
||
package com.linkedin.avroutil1.compatibility; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class SourceCodeUtils { | ||
|
||
private SourceCodeUtils() { | ||
//util class | ||
} | ||
|
||
/** | ||
* splits a large java string literal into smaller pieces in a safe way. | ||
* by safe we mean avoids splitting anywhere near an escape sequence | ||
* @param javaStringLiteral large string literal | ||
* @param maxChunkSize max chunk size in characters | ||
* @return smaller string literals that can be joined to reform the argument | ||
* TODO - change this method to calculate chunk sizes in utf-8 bytes | ||
*/ | ||
public static List<String> safeSplit(String javaStringLiteral, int maxChunkSize) { | ||
String remainder = javaStringLiteral; | ||
List<String> results = new ArrayList<>(remainder.length() / maxChunkSize); | ||
while (remainder.length() > maxChunkSize) { | ||
int cutIndex = maxChunkSize; | ||
while (cutIndex > 0 && escapesNear(remainder, cutIndex)) { | ||
cutIndex--; | ||
} | ||
if (cutIndex <= 0) { | ||
//should never happen ... | ||
throw new IllegalStateException("unable to split " + javaStringLiteral); | ||
} | ||
String piece = remainder.substring(0, cutIndex); | ||
results.add(piece); | ||
remainder = remainder.substring(cutIndex); | ||
} | ||
if (!remainder.isEmpty()) { | ||
results.add(remainder); | ||
} | ||
return results; | ||
} | ||
|
||
/** | ||
* returns true is there's a string escape sequence starting anywhere | ||
* near a given index in a given string literal. since the longest escape | ||
* sequences in java are ~5-6 characters (unicode escapes) a safety margin | ||
* of 10 characters is used. | ||
* @param literal string literal to look for escape sequences in | ||
* @param index index around (before) which to look for escapes | ||
* @return true if any escape sequence found | ||
*/ | ||
static boolean escapesNear(String literal, int index) { | ||
//we start at index because we dont want the char at the start of the next fragment | ||
//to be an "interesting" character either | ||
for (int i = index; i > Math.max(0, index - 6); i--) { | ||
char c = literal.charAt(i); | ||
if (c == '\\' || c == '"' || c == '\'') { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
...helper-common/src/test/java/com/linkedin/avroutil1/compatibility/SourceCodeUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright 2022 LinkedIn Corp. | ||
* Licensed under the BSD 2-Clause License (the "License"). | ||
* See License in the project root for license information. | ||
*/ | ||
|
||
package com.linkedin.avroutil1.compatibility; | ||
|
||
import org.testng.Assert; | ||
import org.testng.annotations.Test; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
|
||
public class SourceCodeUtilsTest { | ||
|
||
@Test | ||
public void testSafeSplit() { | ||
Assert.assertEquals( | ||
Arrays.asList("1234567890", "abcdefghij"), | ||
SourceCodeUtils.safeSplit("1234567890abcdefghij", 10)); | ||
Assert.assertEquals( | ||
Arrays.asList("1234567890", "abcdefghij", "AB"), | ||
SourceCodeUtils.safeSplit("1234567890abcdefghijAB", 10)); | ||
Assert.assertEquals(Collections.singletonList("1234567890"), | ||
SourceCodeUtils.safeSplit("1234567890", 10)); | ||
//dont chop at ' | ||
Assert.assertEquals( | ||
Arrays.asList("12345678", "9'abcdefgh", "ij"), | ||
SourceCodeUtils.safeSplit("123456789'abcdefghij", 10)); | ||
//unicode escapes not on the boundary | ||
Assert.assertEquals( | ||
Arrays.asList("xx\\u1234xx", "xxxxxxxxxx"), | ||
SourceCodeUtils.safeSplit("xx\\u1234xxxxxxxxxxxx", 10)); | ||
Assert.assertEquals( | ||
Arrays.asList("xxxx\\u1234", "xxxxxxxxxx"), | ||
SourceCodeUtils.safeSplit("xxxx\\u1234xxxxxxxxxx", 10)); | ||
//unicode escapes cross the boundary | ||
Assert.assertEquals( | ||
Arrays.asList("xxxx","x\\u1234xxx", "xxxxxx"), | ||
SourceCodeUtils.safeSplit("xxxxx\\u1234xxxxxxxxx", 10)); | ||
Assert.assertEquals( | ||
Arrays.asList("xxxxx","x\\u1234xxx", "xxxxx"), | ||
SourceCodeUtils.safeSplit("xxxxxx\\u1234xxxxxxxx", 10)); | ||
Assert.assertEquals( | ||
Arrays.asList("xxxxxx","x\\u1234xxx", "xxxx"), | ||
SourceCodeUtils.safeSplit("xxxxxxx\\u1234xxxxxxx", 10)); | ||
Assert.assertEquals( | ||
Arrays.asList("xxxxxxx","x\\u1234xxx", "xxx"), | ||
SourceCodeUtils.safeSplit("xxxxxxxx\\u1234xxxxxx", 10)); | ||
Assert.assertEquals( | ||
Arrays.asList("xxxxxxxx","x\\u1234xxx", "xx"), | ||
SourceCodeUtils.safeSplit("xxxxxxxxx\\u1234xxxxx", 10)); | ||
Assert.assertEquals( | ||
Arrays.asList("xxxxxxxxx","x\\u1234xxx", "x"), | ||
SourceCodeUtils.safeSplit("xxxxxxxxxx\\u1234xxxx", 10)); | ||
Assert.assertEquals( | ||
Arrays.asList("xxxxxxxxx","x\\u1234xxx", "x"), | ||
SourceCodeUtils.safeSplit("xxxxxxxxxx\\u1234xxxx", 10)); | ||
Assert.assertEquals( | ||
Arrays.asList("xxxxxxxxxx","x\\u1234xxx"), | ||
SourceCodeUtils.safeSplit("xxxxxxxxxxx\\u1234xxx", 10)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters