-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
V3 casl 561 query tags #370
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9979460
CASL-561 tags querying
Amaneusz 575e106
CASL-561 tag normalization
Amaneusz 0e50edd
CASL-561 minor cleanup
Amaneusz 62131b9
CASL-561 review remarks
Amaneusz c971dee
CASL-561 further review remarks
Amaneusz 516c8e5
CASL-561 placeholders instead of inlined query values
Amaneusz 2d92fc5
CASL-561 wrapping tags selection in placeholder
Amaneusz 53f53c4
CASL-561 splitting internal & JsExport annotation on TagNormalizer
Amaneusz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
86 changes: 0 additions & 86 deletions
86
here-naksha-lib-model/src/commonMain/kotlin/naksha/model/Tag.kt
This file was deleted.
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
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
131 changes: 131 additions & 0 deletions
131
here-naksha-lib-model/src/commonMain/kotlin/naksha/model/TagNormalizer.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,131 @@ | ||
package naksha.model | ||
|
||
import naksha.base.NormalizerForm | ||
import naksha.base.NormalizerForm.NFD | ||
import naksha.base.NormalizerForm.NFKC | ||
import naksha.base.Platform | ||
import naksha.model.TagNormalizer.TagNormalizer_C.normalizeTag | ||
import naksha.model.TagNormalizer.TagNormalizer_C.splitNormalizedTag | ||
import kotlin.js.JsExport | ||
|
||
/** | ||
* An object used for Tag normalization and splitting. | ||
* | ||
* Process of normalization happens in [normalizeTag] method and includes following steps: | ||
* 1) Always: apply normalization form (see [NormalizerForm]) | ||
* 2) Conditional: lowercase the whole tag | ||
* 3) Conditional: remove all non-ASCII characters | ||
* | ||
* Normalization form used in step #1 and subsequent conditional steps depend on tag prefix. | ||
* | ||
* Process of splitting happens in [splitNormalizedTag] method. | ||
* Note that not all tags can be split, it depends on their prefix. | ||
* | ||
* Summarised per-prefix behavior: | ||
* +----------+------------+-----------+----------+-------+ | ||
* | prefix | norm. form | lowercase | no ASCII | split | | ||
* +----------+------------+-----------+----------+-------+ | ||
* | @ | NFKC | false | false | true | | ||
* | ref_ | NFKC | false | false | false | | ||
* | ~ | NFD | false | true | true | | ||
* | # | NFD | false | true | true | | ||
* | sourceID | NFKC | false | false | false | | ||
* | < ELSE > | NFD | true | true | true | | ||
* +----------+------------+-----------+----------+-------+ | ||
* | ||
* By default, (if no special prefix is found) tag is normalized with NFD, lowercased, cleaned of non-ASCII and splittable. | ||
*/ | ||
@JsExport | ||
class TagNormalizer private constructor() { | ||
|
||
companion object TagNormalizer_C { | ||
private val DEFAULT_POLICY = TagProcessingPolicy(NFD, removeNonAscii = true, lowercase = true, split = true) | ||
private val PREFIX_TO_POLICY = mapOf( | ||
"@" to TagProcessingPolicy(NFKC, removeNonAscii = false, lowercase = false, split = true), | ||
"ref_" to TagProcessingPolicy(NFKC, removeNonAscii = false, lowercase = false, split = false), | ||
"sourceID" to TagProcessingPolicy(NFKC, removeNonAscii = false, lowercase = false, split = false), | ||
"~" to TagProcessingPolicy(NFD, removeNonAscii = true, lowercase = false, split = true), | ||
"#" to TagProcessingPolicy(NFD, removeNonAscii = true, lowercase = false, split = true) | ||
) | ||
|
||
private val AS_IS: CharArray = CharArray(128 - 32) { (it + 32).toChar() } | ||
private val TO_LOWER: CharArray = CharArray(128 - 32) { (it + 32).toChar().lowercaseChar() } | ||
|
||
/** | ||
* Main method for raw tag normalization. See[TagNormalizer] doc for more | ||
*/ | ||
fun normalizeTag(tag: String): String { | ||
val policy = policyFor(tag) | ||
val normalized = Platform.normalize(tag, policy.normalizerForm) | ||
return if (policy.lowercase) { | ||
if (policy.removeNonAscii) { | ||
removeNonAscii(normalized, TO_LOWER) | ||
} else { | ||
normalized.lowercase() | ||
} | ||
} else if (policy.removeNonAscii){ | ||
removeNonAscii(normalized, AS_IS) | ||
} else { | ||
normalized | ||
} | ||
} | ||
|
||
private fun removeNonAscii(input: String, outputCharacterSet: CharArray): String { | ||
val sb = StringBuilder() | ||
for (element in input) { | ||
val c = (element.code - 32).toChar() | ||
if (c.code < outputCharacterSet.size) { | ||
sb.append(outputCharacterSet[c.code]) | ||
} | ||
} | ||
return sb.toString() | ||
} | ||
|
||
|
||
/** | ||
* Main method for normalized tag splitting. See[TagNormalizer] doc for more | ||
*/ | ||
internal fun splitNormalizedTag(normalizedTag: String): Pair<String, Any?> { | ||
if (!policyFor(normalizedTag).split) { | ||
return normalizedTag to null | ||
} | ||
val i = normalizedTag.indexOf('=') | ||
val key: String | ||
val value: Any? | ||
if (i >= 1) { | ||
if (normalizedTag[i - 1] == ':') { // := | ||
key = normalizedTag.substring(0, i - 1).trim() | ||
val raw = normalizedTag.substring(i + 1).trim() | ||
value = if ("true".equals(raw, ignoreCase = true)) { | ||
true | ||
} else if ("false".equals(raw, ignoreCase = true)) { | ||
false | ||
} else { | ||
raw.toDouble() | ||
} | ||
} else { | ||
key = normalizedTag.substring(0, i).trim() | ||
value = normalizedTag.substring(i + 1).trim() | ||
} | ||
} else { | ||
key = normalizedTag | ||
value = null | ||
} | ||
return key to value | ||
} | ||
|
||
private fun policyFor(tag: String): TagProcessingPolicy { | ||
for ((prefix, policy) in PREFIX_TO_POLICY) { | ||
if (tag.startsWith(prefix)) return policy | ||
} | ||
return DEFAULT_POLICY | ||
} | ||
} | ||
Amaneusz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
private data class TagProcessingPolicy( | ||
val normalizerForm: NormalizerForm, | ||
val removeNonAscii: Boolean, | ||
val lowercase: Boolean, | ||
val split: Boolean | ||
) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As this class is part of
lib-models
, and it is a public class, it must be exposed to javascript via@JsExport
annotation.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done