chenrui333 / rules_openapi

🍃 bazel rules for generating code from openapi specifications
MIT License
53 stars 25 forks source link

How does one use GoLang-generated code as dependency? #56

Open juhwan-jeong-hub opened 2 years ago

juhwan-jeong-hub commented 2 years ago

I have the following code:

load("@io_bazel_rules_openapi//openapi:openapi.bzl", "openapi_gen")
load("@io_bazel_rules_go//go:def.bzl", "go_library")

openapi_gen(
    name="sample-go-src",
    language="go",
    spec="sample.yaml",
    api_package = "com.shingo.swagger.api",
    model_package = "com.shingo.swagger.model",
)

go_library(
  name = "sample-go",
  srcs = ["//bazel-bin/model/sample_go_codegen.srcjar"], # produced by sample-go-src
)

If I build sample-go-src, I can see that it builds successfully and outputs a .srcjar file.

bazel build -c opt model:sample-go-src                                       1 err | 16:51:20
INFO: Analyzed target //model:sample-go-src (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
Target //model:sample-go-src up-to-date:
  bazel-bin/model/sample-go-src_codegen.srcjar
INFO: Elapsed time: 0.634s, Critical Path: 0.56s
INFO: 2 processes: 1 internal, 1 linux-sandbox.
INFO: Build completed successfully, 2 total actions

However, if I build sample-go, I get the below error:

 bazel build -c opt model:sample-go                                           2 err | 16:54:00
ERROR:<workspace-dir>/model/BUILD:14:11: no such package 'bazel-bin/model/sample_go_codegen.srcjar': BUILD file not found in any of the following directories. Add a BUILD file to a directory to mark it as a package.
 -<workspace-dir>/bazel-bin/model/sample_go_codegen.srcjar and referenced by '//model:sample-go'
ERROR: Analysis of target '//model:sample-go' failed; build aborted: no such package 'bazel-bin/model/sample_go_codegen.srcjar': BUILD file not found in any of the following directories. Add a BUILD file to a directory to mark it as a package.
 - <workspace-dir>/bazel-bin/model/sample_go_codegen.srcjar
INFO: Elapsed time: 0.062s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (1 packages loaded, 0 targets configured)

Consequently, I cannot declare dependency on the go_library to use in my Go code.

loeffel-io commented 1 year ago

+1

i think this rules do not support other languages then java/scala - this should be mentioned in the readme

chenrui333 commented 1 year ago

can you file a PR to add support for it?

loeffel-io commented 7 months ago

this works perfectly

load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("@io_bazel_rules_openapi//openapi:openapi.bzl", "openapi_gen")
load("@aspect_bazel_lib//lib:write_source_files.bzl", "write_source_files")

openapi_gen(
    name = "revenuecat_openapi_v1",
    additional_properties = {
        "packageName": "revenuecat",
        "hideGenerationTimestamp": "true",
        "withGoMod": "false",
    },
    language = "go",
    spec = "@com_revenuecat_openapi_v1//file",
)

genrule(
    name = "revenuecat_openapi_v1_unzip",
    srcs = [":revenuecat_openapi_v1"],
    outs = [
        "unzip/api_customers.go",
        "unzip/api_entitlements.go",
        "unzip/api_offerings.go",
        "unzip/api_transactions.go",
        "unzip/client.go",
        "unzip/configuration.go",
        "unzip/model_defer_a_google_subscription_request.go",
        "unzip/model_delete_subscriber_200_response.go",
        "unzip/model_error.go",
        "unzip/model_get_offerings_403_response.go",
        "unzip/model_grant_a_promotional_entitlement_request.go",
        "unzip/model_offerings.go",
        "unzip/model_offerings_offerings_inner.go",
        "unzip/model_offerings_offerings_inner_packages_inner.go",
        "unzip/model_receipts_request.go",
        "unzip/model_receipts_request_attributes_value.go",
        "unzip/model_subscriber.go",
        "unzip/model_subscriber_subscriber.go",
        "unzip/model_subscriber_subscriber_entitlements_value.go",
        "unzip/model_subscriber_subscriber_non_subscriptions_value_inner.go",
        "unzip/model_subscriber_subscriber_subscriber_attributes_value.go",
        "unzip/model_subscriber_subscriber_subscriptions_value.go",
        "unzip/model_subscribersattribution_request.go",
        "unzip/model_subscribersattribution_request_data.go",
        "unzip/model_update_subscriber_attributes_400_response.go",
        "unzip/model_update_subscriber_attributes_400_response_attribute_errors_inner.go",
        "unzip/model_update_subscriber_attributes_request.go",
        "unzip/model_update_subscriber_attributes_request_attributes.go",
        "unzip/model_update_subscriber_attributes_request_attributes_key_name.go",
        "unzip/response.go",
        "unzip/utils.go",
    ],
    cmd = "unzip $(location :revenuecat_openapi_v1) -d $(@D)/unzip",
)

write_source_files(
    name = "revenuecat_openapi_v1_source",
    diff_test = True,
    files = {
        "api_customers.go": "//revenuecat/v1:unzip/api_customers.go",
        "api_entitlements.go": "//revenuecat/v1:unzip/api_entitlements.go",
        "api_offerings.go": "//revenuecat/v1:unzip/api_offerings.go",
        "api_transactions.go": "//revenuecat/v1:unzip/api_transactions.go",
        "client.go": "//revenuecat/v1:unzip/client.go",
        "configuration.go": "//revenuecat/v1:unzip/configuration.go",
        "model_defer_a_google_subscription_request.go": "//revenuecat/v1:unzip/model_defer_a_google_subscription_request.go",
        "model_delete_subscriber_200_response.go": "//revenuecat/v1:unzip/model_delete_subscriber_200_response.go",
        "model_error.go": "//revenuecat/v1:unzip/model_error.go",
        "model_get_offerings_403_response.go": "//revenuecat/v1:unzip/model_get_offerings_403_response.go",
        "model_grant_a_promotional_entitlement_request.go": "//revenuecat/v1:unzip/model_grant_a_promotional_entitlement_request.go",
        "model_offerings.go": "//revenuecat/v1:unzip/model_offerings.go",
        "model_offerings_offerings_inner.go": "//revenuecat/v1:unzip/model_offerings_offerings_inner.go",
        "model_offerings_offerings_inner_packages_inner.go": "//revenuecat/v1:unzip/model_offerings_offerings_inner_packages_inner.go",
        "model_receipts_request.go": "//revenuecat/v1:unzip/model_receipts_request.go",
        "model_receipts_request_attributes_value.go": "//revenuecat/v1:unzip/model_receipts_request_attributes_value.go",
        "model_subscriber.go": "//revenuecat/v1:unzip/model_subscriber.go",
        "model_subscriber_subscriber.go": "//revenuecat/v1:unzip/model_subscriber_subscriber.go",
        "model_subscriber_subscriber_entitlements_value.go": "//revenuecat/v1:unzip/model_subscriber_subscriber_entitlements_value.go",
        "model_subscriber_subscriber_non_subscriptions_value_inner.go": "//revenuecat/v1:unzip/model_subscriber_subscriber_non_subscriptions_value_inner.go",
        "model_subscriber_subscriber_subscriber_attributes_value.go": "//revenuecat/v1:unzip/model_subscriber_subscriber_subscriber_attributes_value.go",
        "model_subscriber_subscriber_subscriptions_value.go": "//revenuecat/v1:unzip/model_subscriber_subscriber_subscriptions_value.go",
        "model_subscribersattribution_request.go": "//revenuecat/v1:unzip/model_subscribersattribution_request.go",
        "model_subscribersattribution_request_data.go": "//revenuecat/v1:unzip/model_subscribersattribution_request_data.go",
        "model_update_subscriber_attributes_400_response.go": "//revenuecat/v1:unzip/model_update_subscriber_attributes_400_response.go",
        "model_update_subscriber_attributes_400_response_attribute_errors_inner.go": "//revenuecat/v1:unzip/model_update_subscriber_attributes_400_response_attribute_errors_inner.go",
        "model_update_subscriber_attributes_request.go": "//revenuecat/v1:unzip/model_update_subscriber_attributes_request.go",
        "model_update_subscriber_attributes_request_attributes.go": "//revenuecat/v1:unzip/model_update_subscriber_attributes_request_attributes.go",
        "model_update_subscriber_attributes_request_attributes_key_name.go": "//revenuecat/v1:unzip/model_update_subscriber_attributes_request_attributes_key_name.go",
        "response.go": "//revenuecat/v1:unzip/response.go",
        "utils.go": "//revenuecat/v1:unzip/utils.go",
    },
)

go_library(
    name = "revenuecat",
    srcs = [
        "api_customers.go",
        "api_entitlements.go",
        "api_offerings.go",
        "api_transactions.go",
        "client.go",
        "configuration.go",
        "model_defer_a_google_subscription_request.go",
        "model_delete_subscriber_200_response.go",
        "model_error.go",
        "model_get_offerings_403_response.go",
        "model_grant_a_promotional_entitlement_request.go",
        "model_offerings.go",
        "model_offerings_offerings_inner.go",
        "model_offerings_offerings_inner_packages_inner.go",
        "model_receipts_request.go",
        "model_receipts_request_attributes_value.go",
        "model_subscriber.go",
        "model_subscriber_subscriber.go",
        "model_subscriber_subscriber_entitlements_value.go",
        "model_subscriber_subscriber_non_subscriptions_value_inner.go",
        "model_subscriber_subscriber_subscriber_attributes_value.go",
        "model_subscriber_subscriber_subscriptions_value.go",
        "model_subscribersattribution_request.go",
        "model_subscribersattribution_request_data.go",
        "model_update_subscriber_attributes_400_response.go",
        "model_update_subscriber_attributes_400_response_attribute_errors_inner.go",
        "model_update_subscriber_attributes_request.go",
        "model_update_subscriber_attributes_request_attributes.go",
        "model_update_subscriber_attributes_request_attributes_key_name.go",
        "response.go",
        "utils.go",
    ],
    importpath = "github.com/mindful-hq/earth-payment-revenuecat-openapi/revenuecat/v1",
    visibility = ["//visibility:public"],
)