|
| 1 | +#!/usr/bin/env ruby |
| 2 | +# typed: true |
| 3 | +# frozen_string_literal: true |
| 4 | + |
| 5 | +require "json" |
| 6 | +require "fileutils" |
| 7 | + |
| 8 | +root = File.expand_path("..", __dir__) |
| 9 | +site_gen = File.join(root, "site", "lib", "log-generation") |
| 10 | +enums_path = File.join(site_gen, "sorbet-enums.json") |
| 11 | +structs_path = File.join(site_gen, "sorbet-log-structs.json") |
| 12 | +keys_path = File.join(site_gen, "log-keys.json") |
| 13 | + |
| 14 | +unless File.exist?(enums_path) && File.exist?(structs_path) && File.exist?(keys_path) |
| 15 | + abort "Missing generated files in #{site_gen}. Run scripts/all_check.sh first." |
| 16 | +end |
| 17 | + |
| 18 | +enums = JSON.parse(File.read(enums_path)) |
| 19 | +structs = JSON.parse(File.read(structs_path)) |
| 20 | +keys = JSON.parse(File.read(keys_path)) |
| 21 | + |
| 22 | +# Build lookup maps for enum serialized values |
| 23 | +event_values = {} |
| 24 | +(enums["LogStruct::Event"] || []).each { |e| event_values[e["name"]] = e["value"] } |
| 25 | +source_values = {} |
| 26 | +(enums["LogStruct::Source"] || []).each { |e| source_values[e["name"]] = e["value"] } |
| 27 | + |
| 28 | +catalog = {"keys" => keys, "structs" => {}} |
| 29 | + |
| 30 | +structs.each do |fq_name, info| |
| 31 | + name = info["name"] || fq_name.split("::").last |
| 32 | + fields = info["fields"] || {} |
| 33 | + s = {"name" => name} |
| 34 | + |
| 35 | + # Fixed source if enum_single |
| 36 | + if fields["source"] && fields["source"]["type"] == "enum_single" && fields["source"]["enum_value"] |
| 37 | + src_name = fields["source"]["enum_value"] |
| 38 | + s["fixed_source"] = source_values[src_name] || src_name.downcase |
| 39 | + else |
| 40 | + s["fixed_source"] = nil |
| 41 | + end |
| 42 | + |
| 43 | + # Allowed events (serialized) |
| 44 | + allowed = [] |
| 45 | + if fields["event"] |
| 46 | + ef = fields["event"] |
| 47 | + case ef["type"] |
| 48 | + when "enum_single" |
| 49 | + if ef["enum_value"] |
| 50 | + nm = ef["enum_value"] |
| 51 | + allowed << (event_values[nm] || nm.downcase) |
| 52 | + end |
| 53 | + when "enum_union" |
| 54 | + (ef["enum_values"] || []).each do |nm| |
| 55 | + allowed << (event_values[nm] || nm.downcase) |
| 56 | + end |
| 57 | + end |
| 58 | + end |
| 59 | + s["allowed_events"] = allowed.uniq |
| 60 | + |
| 61 | + catalog["structs"][name] = s |
| 62 | +end |
| 63 | + |
| 64 | +provider_dir = File.join(root, "terraform-provider-logstruct") |
| 65 | +data_dir = File.join(provider_dir, "pkg", "data") |
| 66 | +FileUtils.mkdir_p(data_dir) |
| 67 | + |
| 68 | +# Always write JSON (useful for inspection) |
| 69 | +catalog_path = File.join(data_dir, "catalog.json") |
| 70 | +File.write(catalog_path, JSON.pretty_generate(catalog)) |
| 71 | + |
| 72 | +# Also generate a Go source file with embedded types and data for zero runtime parsing |
| 73 | +gen_path = File.join(data_dir, "catalog_gen.go") |
| 74 | + |
| 75 | +def go_string(str) |
| 76 | + # Escape backslashes and quotes for Go string literals |
| 77 | + str.to_s.gsub("\\", "\\\\").gsub('"', '\\"') |
| 78 | +end |
| 79 | + |
| 80 | +go = +"// Code generated by scripts/export_provider_catalog.rb; DO NOT EDIT.\n" |
| 81 | +go << "package data\n\n" |
| 82 | +go << "func ptr[T any](v T) *T { return &v }\n\n" |
| 83 | +go << "type StructCatalog struct {\n\tName string\n\tFixedSource *string\n\tAllowedEvents []string\n}\n\n" |
| 84 | +go << "type Catalog struct {\n\tKeys map[string]string\n\tStructs map[string]StructCatalog\n}\n\n" |
| 85 | +go << "var CatalogData = Catalog{\n\tKeys: map[string]string{\n" |
| 86 | +keys.each do |k, v| |
| 87 | + go << "\t\t\"#{go_string(k)}\": \"#{go_string(v)}\",\n" |
| 88 | +end |
| 89 | +go << "\t},\n\tStructs: map[string]StructCatalog{\n" |
| 90 | +catalog["structs"].each do |name, s| |
| 91 | + go << "\t\t\"#{go_string(name)}\": {Name: \"#{go_string(s["name"])}\"," |
| 92 | + go << if s["fixed_source"] |
| 93 | + " FixedSource: ptr(\"#{go_string(s["fixed_source"])}\")," |
| 94 | + else |
| 95 | + " FixedSource: nil," |
| 96 | + end |
| 97 | + go << " AllowedEvents: []string{" |
| 98 | + go << s["allowed_events"].map { |ev| "\"#{go_string(ev)}\"" }.join(", ") |
| 99 | + go << "}},\n" |
| 100 | +end |
| 101 | +go << "\t},\n}\n" |
| 102 | + |
| 103 | +File.write(gen_path, go) |
| 104 | + |
| 105 | +puts "Wrote provider catalog to #{catalog_path} and #{gen_path}" |
0 commit comments