Skip to content

Add file embedding support#2

Merged
karngyan merged 5 commits intomainfrom
files
Oct 14, 2025
Merged

Add file embedding support#2
karngyan merged 5 commits intomainfrom
files

Conversation

@karngyan
Copy link
Member

Add file embedding support

This PR adds the ability to embed file contents directly into generated Go code using the file: prefix in TOML values.

Features

  • File embedding: Use file:path/to/file in TOML to embed file contents as []byte in generated code
  • Size limits: Configurable max file size (default 1MB) via --max-file-size flag
  • Path resolution: File paths are relative to the TOML file location
  • Validation: Files are validated at generation time for existence and size limits

Refactoring

  • Split generator.go into focused modules:
    • file_handler.go - file reading and embedding
    • struct_gen.go - struct generation logic
    • validation.go - validation helpers
    • value_writer.go - value serialization

Use Cases

  • TLS certificates and keys
  • SQL schemas
  • Template files
  • Small assets (logos, icons)
  • Configuration snippets

Files are embedded as hex byte arrays at generation time, making binaries self-contained with no runtime I/O needed.

Copilot AI review requested due to automatic review settings October 14, 2025 20:04
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds comprehensive file embedding support to the cfgx tool, allowing TOML values to reference external files using the file: prefix syntax. When generation occurs, these files are read and embedded as []byte arrays in the generated Go code, creating self-contained binaries.

Key changes:

  • File embedding using file:path/to/file syntax in TOML values
  • Configurable size limits with --max-file-size flag (default 1MB)
  • Code refactoring to split generator.go into focused modules

Reviewed Changes

Copilot reviewed 19 out of 20 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
readme.md Added comprehensive documentation for file embedding feature with examples and use cases
internal/generator/generator.go Refactored to move most functionality to focused modules, added file validation support
internal/generator/file_handler.go New module handling file reference detection and content loading with size validation
internal/generator/value_writer.go New module for Go value serialization with file embedding and duration support
internal/generator/struct_gen.go New module containing struct generation logic moved from main generator
internal/generator/validation.go New module for validation helpers including time import detection
cmd/cfgx/main.go Added --max-file-size flag with human-readable size parsing
cfgx.go Updated public API to support file embedding with new options
testdata/files/ Added test files for validating file embedding functionality
example/config/ Updated example to demonstrate file embedding with certificate

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@github-actions
Copy link

📊 Code Coverage Report

total:									(statements)			67.2%
Coverage by file
github.com/gomantics/cfgx/cfgx.go:82:					GenerateFromFile		77.1%
github.com/gomantics/cfgx/cfgx.go:168:					Generate			100.0%
github.com/gomantics/cfgx/cfgx.go:184:					GenerateWithOptions		66.7%
github.com/gomantics/cfgx/cmd/cfgx/main.go:30:				main				0.0%
github.com/gomantics/cfgx/cmd/cfgx/main.go:38:				parseFileSize			0.0%
github.com/gomantics/cfgx/cmd/cfgx/main.go:140:				init				0.0%
github.com/gomantics/cfgx/internal/envoverride/envoverride.go:13:	Apply				50.0%
github.com/gomantics/cfgx/internal/envoverride/envoverride.go:40:	applyNested			88.2%
github.com/gomantics/cfgx/internal/envoverride/envoverride.go:79:	convertValue			93.3%
github.com/gomantics/cfgx/internal/envoverride/envoverride.go:111:	convertArray			88.9%
github.com/gomantics/cfgx/internal/generator/file_handler.go:11:	isFileReference			100.0%
github.com/gomantics/cfgx/internal/generator/file_handler.go:18:	loadFileContent			81.2%
github.com/gomantics/cfgx/internal/generator/generator.go:25:		WithPackageName			100.0%
github.com/gomantics/cfgx/internal/generator/generator.go:32:		WithEnvOverride			0.0%
github.com/gomantics/cfgx/internal/generator/generator.go:39:		WithInputDir			100.0%
github.com/gomantics/cfgx/internal/generator/generator.go:46:		WithMaxFileSize			100.0%
github.com/gomantics/cfgx/internal/generator/generator.go:53:		New				100.0%
github.com/gomantics/cfgx/internal/generator/generator.go:68:		stripSuffix			40.0%
github.com/gomantics/cfgx/internal/generator/generator.go:79:		Generate			88.2%
github.com/gomantics/cfgx/internal/generator/struct_gen.go:24:		generateStructsAndVars		63.8%
github.com/gomantics/cfgx/internal/generator/struct_gen.go:128:		collectNestedStructs		57.1%
github.com/gomantics/cfgx/internal/generator/struct_gen.go:166:		generateStruct			89.5%
github.com/gomantics/cfgx/internal/generator/struct_gen.go:208:		generateStructInit		85.2%
github.com/gomantics/cfgx/internal/generator/struct_gen.go:272:		writeArrayOfTablesInit		58.8%
github.com/gomantics/cfgx/internal/generator/struct_gen.go:317:		writeArrayOfStructs		0.0%
github.com/gomantics/cfgx/internal/generator/validation.go:10:		validateFileReferences		100.0%
github.com/gomantics/cfgx/internal/generator/validation.go:20:		validateFileReferencesValue	84.6%
github.com/gomantics/cfgx/internal/generator/validation.go:51:		needsTimeImport			100.0%
github.com/gomantics/cfgx/internal/generator/validation.go:60:		needsTimeImportValue		88.9%
github.com/gomantics/cfgx/internal/generator/validation.go:82:		isDurationString		100.0%
github.com/gomantics/cfgx/internal/generator/value_writer.go:17:	toGoType			94.1%
github.com/gomantics/cfgx/internal/generator/value_writer.go:61:	writeValue			100.0%
github.com/gomantics/cfgx/internal/generator/value_writer.go:66:	writeValueWithIndent		76.5%
github.com/gomantics/cfgx/internal/generator/value_writer.go:105:	writeByteArrayLiteral		88.2%
github.com/gomantics/cfgx/internal/generator/value_writer.go:141:	writeDurationLiteral		80.0%
github.com/gomantics/cfgx/internal/generator/value_writer.go:197:	writeArray			80.0%
github.com/gomantics/cfgx/internal/pkgutil/pkgutil.go:11:		InferName			63.6%
total:									(statements)			67.2%

@karngyan karngyan requested a review from Copilot October 14, 2025 20:10
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 19 out of 20 changed files in this pull request and generated no new comments.

Comments suppressed due to low confidence (3)

internal/generator/value_writer.go:1

  • The writeValueWithIndent call should use the correct value parameter - it's passing 'value' instead of 'val' which is the []any slice being processed.
package generator

internal/generator/value_writer.go:1

  • The writeValueWithIndent call should use the correct value parameter - it's passing 'value' instead of 'val' which is the []any slice being processed.
package generator

internal/generator/value_writer.go:1

  • Type mismatch: writeArrayOfStructs expects []any or []map[string]any, but val is []map[string]any. The function should be called with the correct type.
package generator

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@karngyan karngyan merged commit de1bd46 into main Oct 14, 2025
2 checks passed
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.

2 participants