Skip to content

feat(mock): Add support for repeated scalar fields in mock generator #29

@SebastienMelki

Description

@SebastienMelki

Feature Description

The mock generator needs to handle repeated (list) scalar fields like repeated string, repeated int32, repeated bool, etc. Currently, it only handles single scalar values.

Current State

Scalar field cases in generateMockFieldAssignments don't check field.Desc.IsList(), so repeated scalar fields are treated as single values, causing compilation errors in generated code.

Proposed Solution

1. Modify Scalar Field Cases

Update each scalar case to check for repeated fields:

case protoreflect.StringKind:
    if field.Desc.IsList() {
        gf.P(varName, ".", fieldName, " = []string{")
        for i := 0; i < defaultListSize; i++ {
            gf.P("selectStringExample(\"", fieldPath, "\", ", g.getDefaultGenerator(field), "),")
        }
        gf.P("}")
    } else {
        // Existing single value logic
        gf.P(varName, ".", fieldName, " = selectStringExample(\"", fieldPath, "\", ", g.getDefaultGenerator(field), ")")
    }

2. Apply Pattern to All Scalar Types

  • protoreflect.StringKind[]string
  • protoreflect.Int32Kind[]int32
  • protoreflect.Int64Kind[]int64
  • protoreflect.BoolKind[]bool
  • protoreflect.FloatKind[]float32
  • protoreflect.DoubleKind[]float64

3. Ensure Variety in Generated Data

For repeated fields, generate different values for each element when possible:

  • Use index-based variations (e.g., "example_0", "example_1")
  • Cycle through available examples from fieldExamples
  • Apply slight variations to numeric values

Implementation Details

// Helper to get varied example for index
func getVariedExample(baseValue string, index int) string {
    return fmt.Sprintf("%s_%d", baseValue, index)
}

// In generateMockFieldAssignments
if field.Desc.IsList() {
    gf.P(varName, ".", fieldName, " = []string{")
    examples := getFieldExamples(field)
    for i := 0; i < defaultListSize; i++ {
        if i < len(examples) {
            gf.P("\"", examples[i], "\",")
        } else {
            gf.P("selectStringExample(\"", fieldPath, "\", ", g.getDefaultGenerator(field), "),")
        }
    }
    gf.P("}")
}

Testing Requirements

  • Proto files with repeated scalar fields of all types
  • Verify correct slice types in generated Go code
  • Test empty repeated fields
  • Test large repeated fields (e.g., 100+ elements)

Files Affected

  • internal/httpgen/mock_generator.go - Update generateMockFieldAssignments
  • internal/httpgen/testdata/proto/*.proto - Add test cases
  • internal/httpgen/testdata/golden/*_http_mock.pb.go - Update golden files

Success Criteria

  • All scalar types support repeated fields
  • Generated slices have correct Go types
  • Mock data has appropriate variety
  • Generated code compiles without errors
  • Handles edge cases (empty lists, single element)

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions