Skip to content
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

Demonstrate kotlin gazelle plugin parser failure #529

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions gazelle/kotlin/tests/gcsutil/BUILD.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# gazelle:resolve kotlin com.google.cloud.storage.contrib.nio @maven//:com_google_cloud_google_cloud_nio
9 changes: 9 additions & 0 deletions gazelle/kotlin/tests/gcsutil/BUILD.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
load("@io_bazel_rules_kotlin//kotlin:jvm.bzl", "kt_jvm_library")

# gazelle:resolve kotlin com.google.cloud.storage.contrib.nio @maven//:com_google_cloud_google_cloud_nio

kt_jvm_library(
name = "gcsutil",
srcs = ["gcsutil.kt"],
deps = ["@maven//:com_google_cloud_google_cloud_nio"],
)
2 changes: 2 additions & 0 deletions gazelle/kotlin/tests/gcsutil/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# This is a Bazel workspace for the Gazelle test data.
workspace(name = "simple_file")
132 changes: 132 additions & 0 deletions gazelle/kotlin/tests/gcsutil/gcsutil.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package gazelle.kotlin.tests.gcsutil

import com.google.cloud.storage.contrib.nio.CloudStorageFileSystem
import com.google.cloud.storage.contrib.nio.CloudStoragePath
import java.net.URI

private fun quote(value: String): String = "\"$value\""

/**
* Checks that the uri is formatted like "gs://bucket/file/path.txt", the form accepted by
* gsutil, and returns a [CloudStorageUri].
*
* @throws IllegalArgumentException if the scheme is not a valid GCS path.
* @return a wrapper around [URI] that ensures the URI passed
*/
fun URI.toCloudStorageUri(): CloudStorageUri = CloudStorageUri(this)

/**
* Returns true if the URI is a valid GCS URI as accepted by [CloudStorageUri].
*/
fun URI.isCloudStorageUri(): Boolean {
return try {
this.validateGoogleCloudStorageUri()
true
} catch (e: java.lang.IllegalArgumentException) {
false
}
}

private fun URI.validateGoogleCloudStorageUri() {
if (this.scheme != "gs") {
throw IllegalArgumentException(
"URI doesn't start with gs://, got scheme ${this.scheme}",
)
}
val fragment = this.rawFragment ?: ""
if (fragment.isNotEmpty()) {
throw IllegalArgumentException(
"GCS uris must not have a fragment, got ${quote(fragment)}",
)
}
val query = this.rawQuery ?: ""
if (query.isNotEmpty()) {
throw IllegalArgumentException(
"GCS uris must not have a query part, got ${quote(query)}",
)
}
val path = this.rawPath
if (path.contains("//")) {
throw IllegalArgumentException(
"GCS URIs should not contain two adjacent slashes: ${quote(this)}",
)
}
}

/**
* A class for keeping a validated (per [validateGoogleCloudStorageUri]) URI.
*
* @throws IllegalArgumentException if the passed uri isn't a GCS URI.
*/
// TODO(reddaly): Add more checks to ensure the bucket characters comply with GCS requirements.
@JvmInline
value class CloudStorageUri(val uri: URI) {
constructor(gsUri: String) : this(URI.create(gsUri))

init {
uri.validateGoogleCloudStorageUri()
}

/**
* Returns a [CloudStoragePath] for this "gs://"-style URI.
*/
val path: CloudStoragePath get() = CloudStorageFileSystemSet.DEFAULT.pathFromGsUri(this)

/**
* Returns the GCS path without a leading gs://. The result is formatted like
* <bucket name>/<path to file or directory>
*/
val pathStringWithBucketName: String get() = this.uri.toString().removePrefix("gs://")

/**
* Returns the GCS bucket name for this path.
*/
val bucket: String get() = this.uri.authority

/**
* The path of the file within the bucket. For "gs://bucket-x/foo/bar", returns "foo/bar".
* For "gs://bucket-x", returns "".
*/
val bucketRelativePath: String get() = this.uri.path.removePrefix("/")

/**
* Returns a string like "gs://foo/bar".
*/
override fun toString(): String = this.uri.toString()

/**
* Resolves a GCS path using [URI.resolve] on the URI version of this object.
*
* CloudStorageUri("gs://foo/bar", "baz") returns
* CloudStorageUri("gs://foo/bar/baz").
*/
fun child(relativePath: String): CloudStorageUri =
CloudStorageUri(URI("$this/$relativePath"))
}

// TODO: Workaround for https://github.com/googleapis/java-storage-nio/issues/1153 -
private class CloudStorageFileSystemSet {
companion object {
/**
* A singleton instance of CloudStorageFileSystemSet.
*/
val DEFAULT = CloudStorageFileSystemSet()
}

private val fileSystemByBucketName: MutableMap<String, CloudStorageFileSystem> =
mutableMapOf()

/**
* Returns the value of a "gs://bucket/file/path.txt"-style GCP location as a [Path].
*/
fun pathFromGsUri(uri: CloudStorageUri): CloudStoragePath {
val bucket = uri.bucket
val relativePath = uri.bucketRelativePath
val fs = synchronized(this.fileSystemByBucketName) {
this.fileSystemByBucketName.getOrPut(bucket) {
CloudStorageFileSystem.forBucket(bucket)
}
}
return fs.getPath(relativePath)
}
}
1 change: 1 addition & 0 deletions gazelle/kotlin/tests/simple_file2/BUILD.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# gazelle:resolve kotlin com.google.cloud.storage.contrib.nio @maven//:com_google_cloud_google_cloud_nio
9 changes: 9 additions & 0 deletions gazelle/kotlin/tests/simple_file2/BUILD.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
load("@io_bazel_rules_kotlin//kotlin:jvm.bzl", "kt_jvm_library")

# gazelle:resolve kotlin com.google.cloud.storage.contrib.nio @maven//:com_google_cloud_google_cloud_nio

kt_jvm_library(
name = "simple_file",
srcs = ["lib.kt"],
deps = ["@maven//:com_google_cloud_google_cloud_nio"],
)
2 changes: 2 additions & 0 deletions gazelle/kotlin/tests/simple_file2/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# This is a Bazel workspace for the Gazelle test data.
workspace(name = "simple_file")
8 changes: 8 additions & 0 deletions gazelle/kotlin/tests/simple_file2/lib.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package xyz.example.gcsutil

import com.google.cloud.storage.contrib.nio.CloudStoragePath

fun hello(): CloudStoragePath? {
println("Hello world!")
return null
}
6 changes: 3 additions & 3 deletions go.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -728,10 +728,10 @@ def deps():
name = "com_github_smacker_go_tree_sitter",
build_file_proto_mode = "disable_global",
importpath = "github.com/smacker/go-tree-sitter",
sum = "h1:exZ0FwfhblsYbgfqYH+W/3sFye821WD02NjBmc+ENhE=",
version = "v0.0.0-20230501083651-a7d92773b3aa",
commit = "0e314ace747f49b293ec3a5ec9d7df8395269b53",
vcs = "git",
remote = "[email protected]:reddaly/go-tree-sitter.git",
)

go_repository(
name = "com_github_spf13_afero",
build_file_proto_mode = "disable_global",
Expand Down
Loading