Skip to content

Commit

Permalink
chore(spotless): apply spotless fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
wax911 committed May 30, 2024
1 parent c98242f commit 64c82c0
Show file tree
Hide file tree
Showing 27 changed files with 299 additions and 171 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ internal class GraphClientInterceptor: Interceptor {
.method(original.method, original.body)

if (original.header(CONTENT_TYPE).isNullOrEmpty())
requestBuilder.header(CONTENT_TYPE, GraphConverter.MimeType)
requestBuilder.header(CONTENT_TYPE, GraphConverter.MIME_TYPE)

if (original.url.host == EndpointType.GITHUB.url.host)
requestBuilder.header("Authorization", "bearer ${BuildConfig.token}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ import java.io.IOException
class GraphProcessor(
discoveryPlugin: AbstractDiscoveryPlugin<*>,
override val logger: AbstractLogger = DefaultGraphLogger(),
override val fragmentPatcher: FragmentPatcher = FragmentPatcher(
defaultExtension = discoveryPlugin.targetExtension,
logger = logger
)
override val fragmentPatcher: FragmentPatcher =
FragmentPatcher(
defaultExtension = discoveryPlugin.targetExtension,
logger = logger,
),
) : AbstractGraphProcessor() {
override val defaultExtension: String = discoveryPlugin.targetExtension
override val defaultDirectory: String = discoveryPlugin.targetPath
Expand All @@ -61,8 +62,9 @@ class GraphProcessor(
createGraphQLMap(discoveryPlugin)
logger.v(TAG, "$currentThread: has completed initializing all graphql files")
logger.v(TAG, "$currentThread: discovered `${_graphFiles.size}` graphql files")
} else
} else {
logger.v(TAG, "$currentThread: skipping discovery | cache is not empty -> size: ${_graphFiles.size}")
}
}
}

Expand All @@ -75,15 +77,17 @@ class GraphProcessor(
* annotated with [GraphQuery] or if the no such file could be found
*/
@Synchronized override fun getQuery(annotations: Array<out Annotation>): String? {
val graphQuery = annotations.filterIsInstance(
GraphQuery::class.java
).firstOrNull()
val graphQuery =
annotations.filterIsInstance(
GraphQuery::class.java,
).firstOrNull()

if (graphQuery != null) {
val fileName = "${graphQuery.value}$defaultExtension"
logger.d(TAG, "Looking up `$defaultExtension` file matching: $fileName")
if (_graphFiles.containsKey(fileName))
if (_graphFiles.containsKey(fileName)) {
return _graphFiles[fileName]
}
logger.e(TAG, "The requested query annotated with value: ${graphQuery.value} could not be found!")
logger.e(TAG, "Discovered GraphQL files with the size of -> ${_graphFiles.size}")
}
Expand All @@ -100,11 +104,15 @@ class GraphProcessor(
GraphRegexUtil.containsAnOperation(entry.value)
}
.forEach { entry ->
val patch = fragmentPatcher.includeMissingFragments(
entry.key, entry.value, _graphFiles
)
if (patch.isNotEmpty())
val patch =
fragmentPatcher.includeMissingFragments(
entry.key,
entry.value,
_graphFiles,
)
if (patch.isNotEmpty()) {
entry.setValue("${entry.value}$patch")
}
}
}

Expand All @@ -117,8 +125,9 @@ class GraphProcessor(
@Synchronized private fun createGraphQLMap(discoveryPlugin: AbstractDiscoveryPlugin<*>) {
try {
val graphs = discoveryPlugin.startDiscovery(logger)
if (_graphFiles.isNotEmpty())
if (_graphFiles.isNotEmpty()) {
_graphFiles.clear()
}
_graphFiles.putAll(graphs)
patchQueries()
} catch (e: IOException) {
Expand All @@ -127,7 +136,6 @@ class GraphProcessor(
}

companion object {

private val TAG = GraphProcessor::class.java.simpleName

@Volatile
Expand All @@ -141,34 +149,39 @@ class GraphProcessor(
* @param logger Custom logger facade
*/
@Deprecated(
message = "Consider managing the singleton lifecycle of GraphProcessor on your " +
"own, with something like Dagger, Hilt or even Koin",
replaceWith = ReplaceWith(
"GraphProcessor(assetManager)",
"io.github.wax911.library.annotation.processor.GraphProcessor"
),
level = DeprecationLevel.ERROR
message =
"Consider managing the singleton lifecycle of GraphProcessor on your " +
"own, with something like Dagger, Hilt or even Koin",
replaceWith =
ReplaceWith(
"GraphProcessor(assetManager)",
"io.github.wax911.library.annotation.processor.GraphProcessor",
),
level = DeprecationLevel.ERROR,
)
@JvmOverloads
fun getInstance(
assetManager: AssetManager,
logger: AbstractLogger = DefaultGraphLogger()
logger: AbstractLogger = DefaultGraphLogger(),
): GraphProcessor {
val singleton = instance
if (singleton != null)
if (singleton != null) {
return singleton
}

return synchronized(lock) {
val init = instance
if (init != null)
if (init != null) {
init
else {
val created = GraphProcessor(
discoveryPlugin = AssetManagerDiscoveryPlugin(
assetManager
),
logger = logger
)
} else {
val created =
GraphProcessor(
discoveryPlugin =
AssetManagerDiscoveryPlugin(
assetManager,
),
logger = logger,
)
instance = created
created
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import io.github.wax911.library.logger.core.AbstractLogger
* @see AbstractLogger
*/
abstract class AbstractGraphProcessor {

internal abstract val defaultExtension: String
internal abstract val defaultDirectory: String
internal abstract val logger: AbstractLogger
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ package io.github.wax911.library.annotation.processor.fragment
*/
data class FragmentAnalysis(
val fragmentReference: String,
val isDefined: Boolean
val isDefined: Boolean,
)
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,19 @@ import io.github.wax911.library.logger.core.AbstractLogger
class FragmentPatcher(
private val defaultExtension: String,
private val fragmentAnalyzer: FragmentAnalyzer = RegexFragmentAnalyzer(),
private val logger: AbstractLogger
private val logger: AbstractLogger,
) {
fun includeMissingFragments(
graphFile: String,
graphContent: String,
availableGraphFiles: Map<String, String>,
aggregation: StringBuilder = StringBuilder()
aggregation: StringBuilder = StringBuilder(),
): String {
// Look for any missing fragment definitions in the current graph content.
val missingFragments = fragmentAnalyzer
.analyzeFragments(graphContent)
.filter { !it.isDefined }
val missingFragments =
fragmentAnalyzer
.analyzeFragments(graphContent)
.filter { !it.isDefined }

if (missingFragments.isEmpty()) {
// Nothing to do. We can short circuit and return early.
Expand Down Expand Up @@ -72,7 +73,7 @@ class FragmentPatcher(
// This fragment is nowhere to be found.
logger.w(
TAG,
"$graphFile references $missingFragment, but it could not be located."
"$graphFile references $missingFragment, but it could not be located.",
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ object GraphRegexUtil {
private const val REGEX_FRAGMENT_REFERENCE = "\\.\\.\\.(\\s+)?($REGEX_VALID_NAME)"
private const val REGEX_FRAGMENT_DEFINITION = "fragment\\s{1,}($REGEX_VALID_NAME)\\s{1,}on"
private const val REGEX_OPERATION_DEFINITION = "(query|mutation|subscription)\\s+($REGEX_VALID_NAME)"

// The fragment name will be found in group 2 of the reference regex match result.
private const val GROUP_FRAGMENT_REFERENCE = 2

// The fragment name will be found in group 1 of the definition regex match result.
private const val GROUP_FRAGMENT_DEFINITION = 1

Expand Down Expand Up @@ -66,10 +68,15 @@ object GraphRegexUtil {
* Finds all strings defined by "regexStr" in the provided "graphqlContent". The regex will return multiple match
* groups, so the "groupIndex" indicating the position of the expecting string should be specified.
*/
private fun extractFragmentNames(graphqlContent: String, regexStr: String, groupIndex: Int): Set<String> {
val regexMatches = regexStr
.toRegex(regexOptions)
.findAll(graphqlContent)
private fun extractFragmentNames(
graphqlContent: String,
regexStr: String,
groupIndex: Int,
): Set<String> {
val regexMatches =
regexStr
.toRegex(regexOptions)
.findAll(graphqlContent)

return regexMatches.filter {
it.groupValues.size >= (groupIndex + 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class RegexFragmentAnalyzer : FragmentAnalyzer {
return fragmentReferences.map {
FragmentAnalysis(
fragmentReference = it,
isDefined = fragmentDefinitions.contains(it)
isDefined = fragmentDefinitions.contains(it),
)
}.toSet()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,32 +32,36 @@ import java.io.InputStreamReader
class AssetManagerDiscoveryPlugin(
assetManager: AssetManager,
override val targetPath: String = "graphql",
override val targetExtension: String = ".graphql"
override val targetExtension: String = ".graphql",
) : AbstractDiscoveryPlugin<AssetManager>(assetManager) {

private val temporaryMap = HashMap<String, String>()

private fun AssetManager.findRecursively(
path: String,
logger: AbstractLogger
logger: AbstractLogger,
) {
val paths = list(path)
if (paths.isNullOrEmpty())
if (paths.isNullOrEmpty()) {
logger.w(TAG, "Assets do not contain any files/folders in the path -> $path")
}

for (item in paths!!) {
val absolute = "$path/$item"
if (item.endsWith(targetExtension))
if (item.endsWith(targetExtension)) {
temporaryMap[item] = resolveContents(open(absolute), logger)
else
} else {
findRecursively(absolute, logger)
}
}
}

/**
* Reads the file contents for a given [inputStream]
*/
override fun resolveContents(inputStream: InputStream, logger: AbstractLogger): String {
override fun resolveContents(
inputStream: InputStream,
logger: AbstractLogger,
): String {
val queryBuffer = StringBuilder()
try {
InputStreamReader(inputStream).useLines { sequence ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,19 @@ import java.io.InputStream
* A discovery plugin that defines a contract to handle multiple sources
*/
abstract class AbstractDiscoveryPlugin<S : Any>(
protected val source: S
protected val source: S,
) {

internal abstract val targetPath: String
internal abstract val targetExtension: String

/**
* Reads the file contents for a given [inputStream]
*/
@VisibleForTesting(otherwise = VisibleForTesting.PROTECTED)
protected abstract fun resolveContents(inputStream: InputStream, logger: AbstractLogger): String
protected abstract fun resolveContents(
inputStream: InputStream,
logger: AbstractLogger,
): String

/**
* Invokes initial discovery using [source] to produce a map
Expand Down
Loading

0 comments on commit 64c82c0

Please sign in to comment.