Skip to content

Simplify GenerateOutputSchema to use idiomatic For[T]() pattern#7046

Merged
pelikhan merged 3 commits intomainfrom
copilot/refactor-generate-output-schema
Dec 20, 2025
Merged

Simplify GenerateOutputSchema to use idiomatic For[T]() pattern#7046
pelikhan merged 3 commits intomainfrom
copilot/refactor-generate-output-schema

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Dec 20, 2025

The GenerateOutputSchema function used manual reflection indirection when the jsonschema-go library provides a cleaner generic For[T]() function.

Changes

  • Replace reflect.TypeOf() indirection with direct jsonschema.For[T](nil) call
  • Remove unused fmt and reflect imports
  • Reduce function from 11 lines to 1 line

Before/After

// Before: manual reflection
func GenerateOutputSchema[T any]() (*jsonschema.Schema, error) {
	var zero T
	typ := reflect.TypeOf(zero)
	schema, err := jsonschema.ForType(typ, &jsonschema.ForOptions{})
	if err != nil {
		return nil, fmt.Errorf("failed to generate schema: %w", err)
	}
	return schema, nil
}

// After: idiomatic generic pattern
func GenerateOutputSchema[T any]() (*jsonschema.Schema, error) {
	return jsonschema.For[T](nil)
}

The For[T]() function internally uses reflect.TypeFor[T]() which is more efficient than instantiating a zero value and calling reflect.TypeOf().

Original prompt

This section details on the original issue you should resolve

<issue_title>[plan] Simplify GenerateOutputSchema to use idiomatic ForT pattern</issue_title>
<issue_description>## Objective

Refactor GenerateOutputSchema function to use the more idiomatic For[T]() pattern instead of manual reflection.

Context

The current implementation uses reflect.TypeOf() indirection, but the library provides a cleaner generic For[T]() function.

Current Implementation

func GenerateOutputSchema[T any]() (*jsonschema.Schema, error) {
    var zero T
    typ := reflect.TypeOf(zero)
    return jsonschema.ForType(typ, &jsonschema.ForOptions{})
}

Proposed Implementation

func GenerateOutputSchema[T any]() (*jsonschema.Schema, error) {
    return jsonschema.For[T](nil)
}

Benefits

  • Cleaner code (3 lines → 1 line)
  • No reflect.TypeOf indirection
  • More idiomatic library usage
  • Better error messages with type names

Files to Modify

  • pkg/cli/mcp_schema.go - Update GenerateOutputSchema function

Acceptance Criteria

AI generated by Plan Command for discussion #6818

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


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

Copilot AI and others added 2 commits December 20, 2025 11:57
…tern

Co-authored-by: mnkiefer <8320933+mnkiefer@users.noreply.github.com>
Co-authored-by: mnkiefer <8320933+mnkiefer@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor GenerateOutputSchema to use For[T]() pattern Simplify GenerateOutputSchema to use idiomatic For[T]() pattern Dec 20, 2025
Copilot AI requested a review from mnkiefer December 20, 2025 12:10
@pelikhan pelikhan marked this pull request as ready for review December 20, 2025 13:17
@pelikhan pelikhan merged commit b42bd9e into main Dec 20, 2025
4 checks passed
@pelikhan pelikhan deleted the copilot/refactor-generate-output-schema branch December 20, 2025 13:18
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.

[plan] Simplify GenerateOutputSchema to use idiomatic For[T]() pattern

3 participants