Skip to content

Add support for parsing frontmatter into custom structs#119

Merged
alexec merged 4 commits into
mainfrom
copilot/enable-front-matter-in-struct
Nov 21, 2025
Merged

Add support for parsing frontmatter into custom structs#119
alexec merged 4 commits into
mainfrom
copilot/enable-front-matter-in-struct

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Nov 21, 2025

This PR enables library users to parse frontmatter from both tasks and rules into their own custom structs with strongly-typed fields.

Changes Made

  • New Method: Added Markdown.ParseFrontmatter(target any) error method that allows users to parse frontmatter into custom structs with YAML tags
  • Input Validation: Validates nil target parameter for better error messages
  • Comprehensive Tests: Added tests covering basic fields, nested selectors, array values, optional fields, and error cases
  • Example Code: Added example usage demonstrating how to parse both task and rule frontmatter
  • Documentation: Added inline godoc with usage examples

Example Usage

type TaskMetadata struct {
    TaskName    string `yaml:"task_name"`
    Resume      bool   `yaml:"resume"`
    Priority    string `yaml:"priority"`
}

result, err := cc.Run(context.Background(), "deploy")
if err != nil {
    log.Fatal(err)
}

// Parse task frontmatter
var taskMeta TaskMetadata
if err := result.Task.ParseFrontmatter(&taskMeta); err != nil {
    log.Fatal(err)
}

fmt.Printf("Task: %s, Priority: %s\n", taskMeta.TaskName, taskMeta.Priority)

// Parse rule frontmatter
for _, rule := range result.Rules {
    var ruleMeta struct {
        Language string `yaml:"language"`
    }
    if err := rule.ParseFrontmatter(&ruleMeta); err == nil {
        fmt.Printf("Language: %s\n", ruleMeta.Language)
    }
}

Implementation Details

  • Method placed on Markdown type for better reusability across tasks and rules
  • Uses existing YAML library to avoid adding new dependencies
  • Marshal-unmarshal approach leverages existing project dependencies
  • Comprehensive test coverage including edge cases

Testing

  • ✅ All tests pass
  • ✅ Code lints successfully (make lint)
  • ✅ Build completes successfully
  • ✅ Security scan completed with no issues (CodeQL)
Original prompt

The user of the library might want to have the front-matter from the task in their own struct. Can you provide a way to enable that?


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

@alexec
Copy link
Copy Markdown
Contributor

alexec commented Nov 21, 2025

@copilot try again

Copilot AI and others added 2 commits November 21, 2025 03:26
- Added Result.ParseTaskFrontmatter() method to parse task frontmatter into custom structs
- Added comprehensive tests for parsing into custom structs
- Added example code demonstrating library usage
- Updated .gitignore to exclude test binaries
- All tests pass, code lints successfully

Co-authored-by: alexec <1142830+alexec@users.noreply.github.com>
- Added validation to check for nil target parameter
- Added test case for nil target
- Added comment explaining why marshal-unmarshal approach is used
- All tests pass

Co-authored-by: alexec <1142830+alexec@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Nov 21, 2025

@copilot try again

I've implemented support for parsing task frontmatter into custom structs. The implementation includes:

  1. New Result.ParseTaskFrontmatter(target any) error method
  2. Comprehensive test coverage with multiple scenarios
  3. Example code for library users
  4. Nil validation for better error handling

Users can now parse task frontmatter like this:

type TaskMeta struct {
    TaskName string `yaml:"task_name"`
    Resume   bool   `yaml:"resume"`
    Priority string `yaml:"priority"`
}

var meta TaskMeta
err := result.ParseTaskFrontmatter(&meta)

All tests pass, code lints successfully, and no security vulnerabilities were found. (commits: 7248707, 7614126)

Copilot AI changed the title [WIP] Add support for front-matter in user struct Add support for parsing task frontmatter into custom structs Nov 21, 2025
@alexec alexec marked this pull request as ready for review November 21, 2025 03:41
Comment thread pkg/codingcontext/result.go Outdated
- Moved ParseFrontmatter method from Result to Markdown type
- Renamed from ParseTaskFrontmatter to ParseFrontmatter for generality
- Updated all tests to use markdown.ParseFrontmatter()
- Updated examples to demonstrate parsing both task and rule frontmatter
- Method now works on any Markdown instance, not just tasks
- All tests pass, code lints successfully

Co-authored-by: alexec <1142830+alexec@users.noreply.github.com>
Copilot AI changed the title Add support for parsing task frontmatter into custom structs Add support for parsing frontmatter into custom structs Nov 21, 2025
Copilot AI requested a review from alexec November 21, 2025 04:03
@alexec alexec merged commit 0bd0dc8 into main Nov 21, 2025
2 checks passed
@alexec alexec deleted the copilot/enable-front-matter-in-struct branch November 21, 2025 04:05
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