Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 18, 2025

Overview

This PR adds support for JSONC (JSON with comments), JSON5, and plain text files to the vals eval command by introducing a --raw flag. This addresses the feature request for a "loose mode" similar to envsubst that works with any text format while keeping the powerful ref+ syntax.

Problem

Currently, vals eval requires valid YAML or JSON input. This limitation prevents users from using vals with:

  • JSONC files (JSON with comments) - commonly used in VSCode, tsconfig.json, etc.
  • JSON5 files with flexible syntax (trailing commas, unquoted keys, etc.)
  • Plain text configuration files
  • Any text format that isn't structured YAML/JSON

Users who want to use vals for simple string substitution in configuration files had to either use vals get (which only works with single strings) or ensure their files are valid YAML/JSON.

Solution

Added a --raw flag to the vals eval command that treats input as raw text instead of parsing it as YAML/JSON. When enabled:

  • Input is read as plain text without parsing
  • The existing vals.Get() function performs string substitution on the entire content
  • All ref+ expressions are replaced with their actual values
  • The original file format, comments, and structure are preserved
  • Output format flag (-o) is ignored since the format is preserved as-is

Usage Examples

Plain text configuration:

$ echo 'DATABASE_URL=ref+awsssm://prod/db/url' | vals eval -f - --raw
DATABASE_URL=postgres://prod-db:5432/myapp

JSONC with comments:

$ cat config.jsonc
{
  // Database configuration
  "database": "ref+vault://secret/data/db#/url",
  /* API Keys */
  "api_key": "ref+awssecrets://prod/api#/key"
}

$ vals eval -f config.jsonc --raw
{
  // Database configuration
  "database": "postgres://prod-db:5432/myapp",
  /* API Keys */
  "api_key": "sk-prod-1234567890"
}

JSON5 with trailing commas:

$ vals eval -f config.json5 --raw  # Preserves JSON5 syntax

Implementation Details

  1. New RawInput() function in io.go - Reads raw text content from files or stdin, similar to Inputs() but returns a string instead of YAML nodes

  2. Updated vals eval command in cmd/vals/main.go - Added --raw flag handling that bypasses YAML/JSON parsing and uses vals.Get() for text substitution

  3. Comprehensive tests - Added unit tests for RawInput() and integration tests for raw mode with various file formats

  4. Documentation - Added "Raw Text Mode" section to README with practical examples

Benefits

  • No breaking changes - Existing functionality unchanged when --raw is not used
  • Minimal code changes - Reuses existing vals.Get() logic for substitution
  • Format preservation - Comments, whitespace, and original structure maintained
  • Flexible - Works with any text format, not just JSONC/JSON5
  • Intuitive - Behavior similar to envsubst but with vals provider power

Testing

  • ✅ All existing tests pass
  • ✅ New unit tests for RawInput() function
  • ✅ Integration tests for raw mode with various formats
  • ✅ Linter passes with 0 issues
  • ✅ CodeQL security scan passes with 0 alerts
  • ✅ Manual validation with JSONC, JSON5, and plain text files

Closes #XXX

Original prompt

This section details on the original issue you should resolve

<issue_title>Does vals tool also support JSONC , JSON5 or plain text files ?</issue_title>
<issue_description>Json configuration with comments is heavily used in a lot of ecosystems .
Is there a "loose" mode that could run on any text format ? This would be more similar envsubst but keeping the nice ref+ syntax .</issue_description>

Comments on the Issue (you are @copilot in this section)

Fixes #731


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI changed the title [WIP] Add support for JSONC and JSON5 in vals tool Add --raw flag to vals eval for JSONC, JSON5, and plain text support Oct 18, 2025
Copilot AI requested a review from yxxhero October 18, 2025 00:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Does vals tool also support JSONC , JSON5 or plain text files ?

2 participants