-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve usage of the directives in the api (#137)
* Move directive implementations into an own package * Improve base structure * Improve create method * Generalise directive write to have less duplicated code * Remove constant variable * Update import usage * Update import usage
- Loading branch information
1 parent
1b5b889
commit 9f12410
Showing
14 changed files
with
190 additions
and
78 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
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
114 changes: 114 additions & 0 deletions
114
src/main/kotlin/net/theevilreaper/dartpoet/directive/DirectiveHelper.kt
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,114 @@ | ||
package net.theevilreaper.dartpoet.directive | ||
|
||
import net.theevilreaper.dartpoet.DartModifier | ||
import net.theevilreaper.dartpoet.code.CodeWriter | ||
import net.theevilreaper.dartpoet.directive.impl.DartDirective | ||
import net.theevilreaper.dartpoet.util.SEMICOLON | ||
|
||
/** | ||
* The [DirectiveHelper] is a utility class that provides functionality to write a [Directive] to a [CodeWriter] instance. | ||
* | ||
* <p>In previous versions, the implementation for writing a directive was embedded within the directive implementations themselves. | ||
* This approach resulted in duplicated code and violated the "Don't Repeat Yourself" (DRY) principle.</p> | ||
* | ||
* <p>This helper class centralizes the write process, reducing code duplication and providing a single point of maintenance for directive writing logic.</p> | ||
* | ||
* @version 1.0.0 | ||
* @since 1.0.0 | ||
* @author theEvilReaper | ||
*/ | ||
internal object DirectiveHelper { | ||
|
||
private const val IMPORT_KEY: String = "import" | ||
private const val EXPORT_KEY: String = "export" | ||
private const val PART_KEY: String = "part" | ||
|
||
/** | ||
* Writes a given [Directive] implementation to a [CodeWriter] instance. | ||
* This method raises an [UnsupportedOperationException] if the directive is a library or part directive. | ||
* @param writer the [CodeWriter] instance to append the directive | ||
* @param directive the directive to write | ||
* @param importCast the cast for the import | ||
* @param castType the type of the cast | ||
* @throws UnsupportedOperationException if the directive is a library or part directive | ||
*/ | ||
@Throws(UnsupportedOperationException::class) | ||
internal fun writeDirective(writer: CodeWriter, directive: Directive, importCast: String?, castType: CastType?) { | ||
if (directive.type() == DirectiveType.LIBRARY || directive.type() == DirectiveType.PART) { | ||
throw UnsupportedOperationException("The library and part directive should be written by the directive impl") | ||
} | ||
|
||
val importKey = when (directive.type()) { | ||
DirectiveType.EXPORT -> EXPORT_KEY | ||
else -> IMPORT_KEY | ||
} | ||
|
||
writer.emit(importKey) | ||
writer.emitSpace() | ||
|
||
val path = when (directive.type()) { | ||
DirectiveType.IMPORT -> updateImportBegin(directive.getPathWithEnding()) | ||
else -> directive.getPathWithEnding() | ||
} | ||
|
||
writer.emitCode("%C", path) | ||
|
||
if (importCast != null && castType != null) { | ||
writer.emitSpace() | ||
writer.emitCode("%L", castType.identifier) | ||
writer.emitSpace() | ||
writer.emitCode("%L", importCast) | ||
} | ||
|
||
writer.emit(SEMICOLON) | ||
} | ||
|
||
/** | ||
* Writes a given [Directive] implementation to a [CodeWriter] instance. | ||
* This method is only used to write the library and part directive. | ||
* @param writer the [CodeWriter] instance to append the directive | ||
* @param directive the directive to write | ||
* @param asPartOf the flag to check if the library is part of another library | ||
* @throws UnsupportedOperationException if the directive is not a part or library directive | ||
*/ | ||
@Throws(UnsupportedOperationException::class) | ||
internal fun writePartOrLibDirective(writer: CodeWriter, directive: Directive, asPartOf: Boolean = false) { | ||
if (!(directive.type() == DirectiveType.PART || directive.type() == DirectiveType.LIBRARY)) { | ||
throw UnsupportedOperationException("This method can only be used for part and library directive") | ||
} | ||
|
||
val importPair: Pair<String, String> = when (directive.type()) { | ||
DirectiveType.PART -> Pair(PART_KEY, "%C") | ||
else -> Pair(getLibraryImportKey(asPartOf), "%L") | ||
} | ||
|
||
writer.emitCode("%L", importPair.first) | ||
writer.emitSpace() | ||
writer.emitCode(importPair.second, directive.getRawPath()) | ||
writer.emit(SEMICOLON) | ||
} | ||
|
||
/** | ||
* Returns the key for the library import. | ||
* @param asPartOf the flag to check if the library is part of another library | ||
* @return the key for the library import | ||
*/ | ||
private fun getLibraryImportKey(asPartOf: Boolean = false): String = when (asPartOf) { | ||
true -> "part of" | ||
false -> DartModifier.LIBRARY.identifier | ||
} | ||
|
||
/** | ||
* The method updates the given import path if it doesn't start with `dart:`. | ||
* In this case, the method will add the `package:` prefix to the path. | ||
* A `package:` import is required when the [DartDirective] is used to import a file from a package. | ||
* @param path the path to update | ||
* @return the updated path | ||
*/ | ||
private fun updateImportBegin(path: String): String { | ||
return when (path.startsWith("dart:")) { | ||
true -> path | ||
false -> "package:$path" | ||
} | ||
} | ||
} |
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
16 changes: 7 additions & 9 deletions
16
...er/dartpoet/directive/LibraryDirective.kt → ...rtpoet/directive/impl/LibraryDirective.kt
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 |
---|---|---|
@@ -1,27 +1,25 @@ | ||
package net.theevilreaper.dartpoet.directive | ||
package net.theevilreaper.dartpoet.directive.impl | ||
|
||
import net.theevilreaper.dartpoet.DartModifier.LIBRARY | ||
import net.theevilreaper.dartpoet.code.CodeWriter | ||
import net.theevilreaper.dartpoet.util.SEMICOLON | ||
import net.theevilreaper.dartpoet.directive.BaseDirective | ||
import net.theevilreaper.dartpoet.directive.DirectiveHelper | ||
import net.theevilreaper.dartpoet.directive.DirectiveType | ||
|
||
/** | ||
* The [LibraryDirective] represents the library directive from dart. | ||
* @since 1.0.0 | ||
* @author theEvilReaper | ||
*/ | ||
class LibraryDirective internal constructor( | ||
private val path: String, | ||
path: String, | ||
private val asPartOf: Boolean = false | ||
) : BaseDirective(path) { | ||
) : BaseDirective(DirectiveType.LIBRARY, path) { | ||
|
||
/** | ||
* Writes the data from the [LibraryDirective] to a given instance from a [CodeWriter]. | ||
* @param writer the [CodeWriter] instance to append the directive | ||
*/ | ||
override fun write(writer: CodeWriter) { | ||
val baseString = if (asPartOf) "part of" else LIBRARY.identifier | ||
writer.emit("$baseString·") | ||
writer.emit(path) | ||
writer.emit(SEMICOLON) | ||
DirectiveHelper.writePartOrLibDirective(writer, this, asPartOf) | ||
} | ||
} |
Oops, something went wrong.