Skip to content

Commit

Permalink
Name: add get a random name (#1728)
Browse files Browse the repository at this point in the history
  • Loading branch information
maximiliankaul authored Sep 25, 2024
1 parent 13f82d4 commit d2ded6a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/cpg.common-conventions.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ kotlin {

tasks.withType<KotlinCompile> {
compilerOptions {
freeCompilerArgs = listOf("-opt-in=kotlin.RequiresOptIn", "-Xcontext-receivers")
freeCompilerArgs = listOf("-opt-in=kotlin.RequiresOptIn", "-opt-in=kotlin.uuid.ExperimentalUuidApi", "-Xcontext-receivers")
}
}

Expand Down
13 changes: 13 additions & 0 deletions cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/graph/Name.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import de.fraunhofer.aisec.cpg.frontends.Handler
import de.fraunhofer.aisec.cpg.frontends.Language
import de.fraunhofer.aisec.cpg.frontends.LanguageFrontend
import java.util.*
import kotlin.uuid.Uuid

/**
* This class represents anything that can have a "Name". In the simplest case it only represents a
Expand All @@ -49,6 +50,18 @@ class Name(
language: Language<*>?
) : this(localName, parent, language?.namespaceDelimiter ?: ".")

companion object {
/**
* Creates a random name starting with a prefix plus a random UUID (version 4). The Name is
* prefixed by [prefix], followed by a separator character [separatorChar] and finalized by
* a random UUID ("-" separators also replaced with [separatorChar]).
*/
fun random(prefix: String, separatorChar: Char = '_'): Name {
val randomPart = Uuid.random().toString().replace('-', separatorChar)
return Name(localName = prefix + separatorChar + randomPart)
}
}

/**
* The full string representation of this name. Since [localName] and [parent] are immutable,
* this is basically a cache for [toString]. Otherwise, we would need to call [toString] a lot
Expand Down

0 comments on commit d2ded6a

Please sign in to comment.