Skip to content

Commit

Permalink
Merge remote-tracking branch 'hairyhum/publish-to-multiple-projects' …
Browse files Browse the repository at this point in the history
…into master
  • Loading branch information
cjab committed Aug 23, 2020
2 parents 25ca4c3 + 33b779d commit fbaea38
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/weddell/client/publisher.ex
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ defmodule Weddell.Client.Publisher do
def topics(client, opts \\ []) do
max_topics = Keyword.get(opts, :max, @default_list_max)
cursor = Keyword.get(opts, :cursor, "")
request = ListTopicsRequest.new(project: "projects/#{client.project}",
request = ListTopicsRequest.new(project: Util.full_project(client.project),
page_size: max_topics,
page_token: cursor)
client.channel
Expand Down
2 changes: 1 addition & 1 deletion lib/weddell/client/subscriber.ex
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ defmodule Weddell.Client.Subscriber do
def subscriptions(client, opts \\ []) do
max_topics = Keyword.get(opts, :max, @default_list_max)
cursor = Keyword.get(opts, :cursor, "")
request = ListSubscriptionsRequest.new(project: "projects/#{client.project}",
request = ListSubscriptionsRequest.new(project: Util.full_project(client.project),
page_size: max_topics,
page_token: cursor)
client.channel
Expand Down
23 changes: 23 additions & 0 deletions lib/weddell/client/util.ex
Original file line number Diff line number Diff line change
@@ -1,13 +1,36 @@
defmodule Weddell.Client.Util do
@moduledoc false

@subscription_regex ~r/projects\/.+\/subscriptions\/.+/
@topics_regex ~r/projects\/.+\/topics\/.+/

@spec full_subscription(project :: String.t, subscription :: String.t) :: String.t
def full_subscription(project, "projects/" <> _ = name) do
case Regex.match?(@subscription_regex, name) do
true -> name
false -> make_full_subscription(project, name)
end
end
def full_subscription(project, name) do
make_full_subscription(project, name)
end

defp make_full_subscription(project, name) do
"#{full_project(project)}/subscriptions/#{name}"
end

@spec full_topic(project :: String.t, topic :: String.t) :: String.t
def full_topic(project, "projects/" <> _ = name) do
case Regex.match?(@topics_regex, name) do
true -> name
false -> make_full_topic(project, name)
end
end
def full_topic(project, name) do
make_full_topic(project, name)
end

defp make_full_topic(project, name) do
"#{full_project(project)}/topics/#{name}"
end

Expand Down
32 changes: 32 additions & 0 deletions test/weddell/client/util_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,45 @@ defmodule Weddell.Client.UtilTest do
assert Util.full_subscription(@project, @subscription) ==
"projects/#{@project}/subscriptions/#{@subscription}"
end
test "returns original string if it's a fully qualified subscription" do
full_subscription = "projects/myproject/subscriptions/mysubscription"
assert Util.full_subscription(@project, full_subscription) == full_subscription
end
test "builds a full subscription if not fully qualified" do
subscriptions = [
"projects/myproject/subscription/mysubscription", # without s in subscriptions
"projects/myproject/subscriptionsmysubscription", # missing slash
"projects//subscriptions/mysubscription", # missing project
"projects/myproject/subscriptions/", # missing subscription name
"projects/myproject/something" # missing subscriptions bit
]
Enum.each(subscriptions, fn(sub) ->
assert Util.full_subscription(@project, sub) == "projects/#{@project}/subscriptions/#{sub}"
end)
end
end

describe "Util.full_topic\2" do
test "builds a full topic string" do
assert Util.full_topic(@project, @topic) ==
"projects/#{@project}/topics/#{@topic}"
end
test "returns original string if it's a fully qualified topic" do
full_topic = "projects/myproject/topics/mytopic"
assert Util.full_topic(@project, full_topic) == full_topic
end
test "builds a full topic if not fully qualified" do
topics = [
"projects/myproject/topic/mytopic", # without s in topics
"projects/myproject/topicsmytopic", # missing slash
"projects//topics/mytopic", # missing project
"projects/myproject/topics/", # missing topic name
"projects/myproject/something" # missing topics bit
]
Enum.each(topics, fn(topic) ->
assert Util.full_topic(@project, topic) == "projects/#{@project}/topics/#{topic}"
end)
end
end

describe "Util.full_project\2" do
Expand Down

0 comments on commit fbaea38

Please sign in to comment.