#!/usr/bin/env bash set -euo pipefail # Validate that config.schema.json stays in sync with Go struct JSON tags # Extracts json:"..." tags from Go structs and compares against schema properties echo "🔍 Validating Go struct fields vs config.schema.json..." echo "========================================================" # Get the repository root if command -v readlink >/dev/null 2>&1 && readlink -f "$0" >/dev/null 2>&1; then SCRIPT_DIR="$(dirname "$(readlink -f "$0")")" else SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd -P)" fi REPO_ROOT="$(cd "$SCRIPT_DIR/../../.." && pwd)" CONFIG_SCHEMA="$REPO_ROOT/transports/config.schema.json" # Color codes RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' CYAN='\033[0;36m' NC='\033[0m' ERRORS=0 WARNINGS=0 # Check prerequisites if [ ! -f "$CONFIG_SCHEMA" ]; then echo "❌ Config schema not found: $CONFIG_SCHEMA" exit 1 fi if ! command -v jq &> /dev/null; then echo "❌ jq is required for Go-to-schema validation" exit 1 fi # Extract JSON tags from a Go struct # Usage: extract_go_json_tags # Returns sorted list of json tag names (excluding "-" and ",omitempty" suffixes) extract_go_json_tags() { local file=$1 local struct_name=$2 awk "/^type ${struct_name} struct/,/^}/" "$file" \ | grep -oE 'json:"([^"]+)"' \ | sed 's/json:"//;s/"//' \ | sed 's/,.*//' \ | grep -v '^-$' \ | sort } # Extract property keys from config.schema.json at a given jq path # Usage: extract_schema_keys extract_schema_keys() { local jq_path=$1 jq -r "${jq_path} | keys[]" "$CONFIG_SCHEMA" 2>/dev/null | sort } # Compare Go struct tags against schema properties # Usage: compare_struct_to_schema