Skip to content

Commit a9f213c

Browse files
committed
Java codegen: cope with anonymous types
1 parent 9fa9171 commit a9f213c

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

schema_salad/codegen_base.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
import collections
33
from typing import Any, Dict, List, MutableSequence, Optional, Set, Union
44

5-
from . import schema
6-
75

86
class TypeDef: # pylint: disable=too-few-public-methods
97
"""Schema Salad type description."""
@@ -66,7 +64,7 @@ def prologue(self) -> None:
6664
@staticmethod
6765
def safe_name(name: str) -> str:
6866
"""Generate a safe version of the given name."""
69-
return schema.avro_field_name(name)
67+
raise NotImplementedError()
7068

7169
def begin_class(
7270
self, # pylint: disable=too-many-arguments

schema_salad/java_codegen.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ def safe_name(name: str) -> str:
160160
if avn in ("class", "extends", "abstract", "default", "package"):
161161
# reserved words
162162
avn = avn + "_"
163+
if avn and avn.startswith("anon."):
164+
avn = avn[5:]
163165
return avn
164166

165167
def interface_name(self, n: str) -> str:
@@ -685,14 +687,19 @@ def declare_id_field(
685687
self.declare_field(name, fieldtype, doc, True)
686688
if optional:
687689
set_uri = """
690+
Boolean __original_is_null = {safename} == null;
688691
if ({safename} == null) {{
689692
if (__docRoot != null) {{
690693
{safename} = java.util.Optional.of(__docRoot);
691694
}} else {{
692695
{safename} = java.util.Optional.of("_:" + java.util.UUID.randomUUID().toString());
693696
}}
694697
}}
695-
__baseUri = (String) {safename}.orElse(null);
698+
if (__original_is_null) {{
699+
__baseUri = __baseUri_;
700+
}} else {{
701+
__baseUri = (String) {safename}.orElse(null);
702+
}}
696703
"""
697704
else:
698705
set_uri = """

0 commit comments

Comments
 (0)