Skip to content

Commit 3e68dcc

Browse files
committed
Inline real type resolution method
It not so big and this unifies handling of types and enums
1 parent a4706e7 commit 3e68dcc

File tree

1 file changed

+20
-24
lines changed

1 file changed

+20
-24
lines changed

shared/src/main/scala/io/kaitai/struct/precompile/ResolveTypes.scala

+20-24
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,26 @@ class ResolveTypes(specs: ClassSpecs, topClass: ClassSpec, opaqueTypes: Boolean)
4747
private def resolveUserType(curClass: ClassSpec, dataType: DataType, path: List[String]): Iterable[CompilationProblem] = {
4848
dataType match {
4949
case ut: UserType =>
50-
val (resClassSpec, problems) = resolveUserType(curClass, ut.name, path ++ List("type"))
51-
ut.classSpec = resClassSpec
52-
problems
50+
try {
51+
val resolver = new ClassTypeProvider(specs, curClass)
52+
val ty = resolver.resolveTypePath(curClass, ut.name)
53+
Log.typeResolve.info(() => s" => ${ty.nameAsStr}")
54+
ut.classSpec = Some(ty)
55+
None
56+
} catch {
57+
case _: TypeNotFoundError =>
58+
// Type definition not found
59+
if (opaqueTypes) {
60+
// Generate special "opaque placeholder" ClassSpec
61+
Log.typeResolve.info(() => " => ??? (generating opaque type)")
62+
ut.classSpec = Some(ClassSpec.opaquePlaceholder(ut.name))
63+
None
64+
} else {
65+
// Opaque types are disabled => that is an error
66+
Log.typeResolve.info(() => " => ??? (opaque type are disabled => error)")
67+
Some(TypeNotFoundErr(ut.name, curClass, path :+ "type"))
68+
}
69+
}
5370
case et: EnumType =>
5471
et.name match {
5572
case typePath :+ name =>
@@ -84,25 +101,4 @@ class ResolveTypes(specs: ClassSpecs, topClass: ClassSpec, opaqueTypes: Boolean)
84101
None
85102
}
86103
}
87-
88-
private def resolveUserType(curClass: ClassSpec, typeName: List[String], path: List[String]): (Option[ClassSpec], Option[CompilationProblem]) = {
89-
try {
90-
val resolver = new ClassTypeProvider(specs, curClass)
91-
val ty = resolver.resolveTypePath(curClass, typeName)
92-
Log.typeResolve.info(() => s" => ${ty.nameAsStr}")
93-
(Some(ty), None)
94-
} catch {
95-
case _: TypeNotFoundError =>
96-
// Type definition not found
97-
if (opaqueTypes) {
98-
// Generate special "opaque placeholder" ClassSpec
99-
Log.typeResolve.info(() => " => ??? (generating opaque type)")
100-
(Some(ClassSpec.opaquePlaceholder(typeName)), None)
101-
} else {
102-
// Opaque types are disabled => that is an error
103-
Log.typeResolve.info(() => " => ??? (opaque type are disabled => error)")
104-
(None, Some(TypeNotFoundErr(typeName, curClass, path)))
105-
}
106-
}
107-
}
108104
}

0 commit comments

Comments
 (0)