Skip to content

Add inline depth feature#746

Open
daibva wants to merge 6 commits intogoccy:masterfrom
daibva:add-inline-depth-feature
Open

Add inline depth feature#746
daibva wants to merge 6 commits intogoccy:masterfrom
daibva:add-inline-depth-feature

Conversation

@daibva
Copy link
Copy Markdown

@daibva daibva commented May 29, 2025

🎯 Add InlineAfterDepth Option for Depth-Based Inline Formatting

This PR introduces a new encoding option InlineAfterDepth that automatically switches to inline (flow) style formatting when YAML nesting depth exceeds a specified threshold. This feature is particularly useful for deeply nested configuration files, making them more compact while maintaining readability at higher levels.

✨ Features

  • Automatic depth detection: Tracks current nesting depth during encoding
  • Flexible configuration: Users can specify any depth threshold (0, 1, 2, etc.)
  • Backward compatible: Feature is disabled by default, no impact on existing code
  • Error handling: Validates negative depth values with proper error messages
  • Comprehensive testing: Includes unit tests for various scenarios

📝 Usage Examples

Basic Usage

// Enable inline formatting after depth 1
data := map[string]interface{}{
    "database": map[string]interface{}{
        "host": "localhost",
        "port": 5432,
    },
}

// Using MarshalWithOptions
yaml, err := yaml.MarshalWithOptions(data, yaml.InlineAfterDepth(1))

// Using Encoder
var buf bytes.Buffer
enc := yaml.NewEncoder(&buf, yaml.InlineAfterDepth(1))
err := enc.Encode(data)

Real-world Example (Clash Configuration)

// Perfect for configuration files like Clash configs
yaml.MarshalWithOptions(clashConfig, yaml.InlineAfterDepth(1))

🔄 Before/After Comparison

Standard Format:

database:
  primary:
    host: localhost
    port: 5432
    credentials:
      username: admin
      password: secret

With InlineAfterDepth(1):

database:
  primary: {host: localhost, port: 5432, credentials: {username: admin, password: secret}}

With InlineAfterDepth(2):

database:
  primary:
    host: localhost
    port: 5432
    credentials: {username: admin, password: secret}

🔧 Implementation Details

  • File Changes:

    • option.go: Added InlineAfterDepth() function with validation
    • encode.go: Enhanced Encoder struct with depth tracking fields and logic
    • inline_depth_test.go: Comprehensive test suite covering all scenarios
  • Depth Tracking: Automatically increments/decrements depth when encoding complex types (structs, maps, arrays)

  • Flow Style Switching: Temporarily enables flow style when depth exceeds threshold, restores original style afterward

🧪 Testing Coverage

  • ✅ Basic functionality tests with different depth values
  • ✅ Error handling for invalid inputs (negative depths)
  • ✅ Complex nested structure scenarios
  • ✅ Default behavior verification (feature disabled)
  • ✅ Real-world usage examples with Clash configuration files
  • ✅ All existing tests continue to pass

📊 Benefits

  • File Size Reduction: Significantly reduces file size for deeply nested structures
  • Improved Readability: Maintains readability at higher levels while compacting deep nesting
  • Practical Application: Ideal for configuration files, API responses, and data serialization

📋 Files Modified

  • option.go - New encoding option implementation
  • encode.go - Depth tracking and inline formatting logic
  • inline_depth_test.go - Comprehensive test suite
  • .gitignore - Added examples/ directory to exclude temporary test files

🚀 Ready for Production

This feature has been thoroughly tested and is ready for production use. It provides a powerful new way to control YAML formatting based on structural complexity, making it especially valuable for configuration management and data serialization scenarios.

baibai and others added 6 commits May 29, 2025 13:27
- Add README_zh.md with complete Chinese translation
- Add language switcher links in both README files
- Maintain original formatting and code examples
- Support bilingual documentation navigation
- 新增 InlineAfterDepth 编码选项,当嵌套深度超过指定级别时自动启用内联格式化

- 在 Encoder 结构体中添加深度跟踪字段

- 修改 encodeValue 函数以在适当的深度动态切换到 flow 样式

- 添加全面的测试用例验证功能正确性

- 提供示例程序演示不同深度阈值的效果

- 保持向后兼容性,不影响现有编码功能
- Add InlineAfterDepth encoding option
- Implement depth tracking in Encoder
- Add comprehensive tests
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.

1 participant