Skip to content

Instantly share code, notes, and snippets.

@beccasaurus
Last active May 31, 2019 03:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beccasaurus/7ac8fe0b5a025e556d6c8f732ad4b68e to your computer and use it in GitHub Desktop.
Save beccasaurus/7ac8fe0b5a025e556d6c8f732ad4b68e to your computer and use it in GitHub Desktop.
Sample Configurations in Separate Files :: POLYFILL
#! /usr/bin/env ruby
# Script for my local code sample development.
#
# Merged individual samples into speech_gapic.yaml
#
# Shim for upcoming change (samples will be in separate files)
#
# Note: the exact formatting of the individual files is subject to change
# TODO(beccasaurus) simply provide a GAPIC file and then lazily glob(s) of where to find the files (just get all .yamls)
# gapicify-samples speech_gapic.yaml samples/
# gapicify-samples speech/v1/speech_gapic.yaml my_sample.yaml speech/v1/samples_dir some/**/specific/glob
require "yaml"
VERSIONS = ["v1", "v1p1beta1"]
VERSIONS.each do |version|
puts "[#{version}]"
version_root = File.join __dir__, version
samples_root = File.join version_root, "samples"
sample_files = Dir[File.join samples_root, "*.yaml"]
gapic_config_file = File.join version_root, "speech_gapic.yaml"
samples = []
sample_files.each do |yaml_file|
begin
data = YAML.load_file yaml_file
if data.is_a?(Hash) && data.has_key?("samples") && data["samples"].is_a?(Array)
samples.concat data["samples"]
end
rescue StandardError => ex
puts "Problem loading YAML file #{yaml_file}"
puts ex.message
end
end
gapic_config = YAML.load_file gapic_config_file
# Forcefully remove all existing standalone samples from the file
# (otherwise renaming region tags / removing samples won't work)
unless ENV["KEEP_GAPIC"]
gapic_config["interfaces"].each do |interface|
interface["methods"].each do |method|
if method.has_key?("samples") && method["samples"].has_key?("standalone")
method["samples"]["standalone"].each do |standalone_sample|
if method.has_key? "sample_value_sets"
method["sample_value_sets"].delete_if { |s| s["id"] == standalone_sample["region_tag"] }
end
end
end
end
end
end
samples.each do |sample|
service_name = sample.delete "service"
rpc_name = sample.delete "rpc"
if service_config = gapic_config["interfaces"].find { |s| s["name"] == service_name }
if rpc_config = service_config["methods"].find { |m| m["name"] == rpc_name }
rpc_config["samples"] ||= {}
rpc_config["samples"]["standalone"] ||= []
unless rpc_config["samples"]["standalone"].any? { |s| s["region_tag"] == sample["id"] }
rpc_config["samples"]["standalone"].push({
"region_tag" => sample["id"],
"value_sets" => [ sample["id"] ]
})
end
rpc_config["sample_value_sets"] ||= []
rpc_config["sample_value_sets"].delete_if { |s| s["id"] == sample["id"] }
rpc_config["sample_value_sets"].push sample
puts sample["id"]
else
puts "Could not find rpc config #{rpc_name} for #{service_name} in #{gapic_config_file}"
end
else
puts "Could not find service config #{service_name} in #{gapic_config_file}"
end
end
File.write gapic_config_file, gapic_config.to_yaml.lines[1..-1].join # *
puts "Wrote #{samples.size} samples to #{gapic_config_file}"
# * removes the '---' which Ruby to_yaml adds by default as the first line
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment