Skip to content

Refactor: eliminate redundant JSON unmarshals in produce-policy validators; use slices.Contains in env_sources#906

Merged
rspurgeon merged 2 commits intomainfrom
copilot/simplifier-code-simplification
Apr 27, 2026
Merged

Refactor: eliminate redundant JSON unmarshals in produce-policy validators; use slices.Contains in env_sources#906
rspurgeon merged 2 commits intomainfrom
copilot/simplifier-code-simplification

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 26, 2026

Two minor simplifications to align recently added code with existing codebase patterns.

Changes

  • event_gateway_produce_policy.goUnmarshalJSON was calling json.Unmarshal 4× (meta struct + 2 validators + SDK union type). Parse data into map[string]any once after the meta extraction and pass it to both validators, matching the pattern in event_gateway_consume_policy.go. Also eliminates 3 intermediate string variables (modifyHeadersType, schemaValidationType, encryptType) in favor of direct validTypes slice indexing.

    // Before: each validator parsed data independently
    func validateProducePolicyTypeField(data []byte) error {
        var raw map[string]any
        if err := json.Unmarshal(data, &raw); err != nil { ... }
        ...
    }
    
    // After: parse once, pass map
    var raw map[string]any
    if err := json.Unmarshal(data, &raw); err != nil { return err }
    if err := validateProducePolicyTypeField(raw); err != nil { return err }
    if err := validateProducePolicyConfigField(raw); err != nil { return err }
  • env_sources.go — Replace 3-line manual loop in parseStructuredFieldTag with slices.Contains(parts[1:], "inline"), consistent with Go 1.26 idioms used throughout the codebase.

Copilot AI linked an issue Apr 26, 2026 that may be closed by this pull request
…n env_sources

Agent-Logs-Url: https://github.com/Kong/kongctl/sessions/66acfdaf-3733-4ea7-a0bd-4f72a7ac1b52

Co-authored-by: rspurgeon <10521262+rspurgeon@users.noreply.github.com>
Copilot AI changed the title [WIP] Simplify code in event_gateway_produce_policy.go and env_sources.go refactor: eliminate redundant JSON unmarshals in produce-policy validators; use slices.Contains in env_sources Apr 26, 2026
Copilot AI requested a review from rspurgeon April 26, 2026 21:36
@rspurgeon rspurgeon marked this pull request as ready for review April 26, 2026 22:07
@rspurgeon rspurgeon requested review from a team as code owners April 26, 2026 22:07
Copilot AI review requested due to automatic review settings April 26, 2026 22:07
Copy link
Copy Markdown
Contributor

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 streamlines declarative parsing by reducing redundant JSON unmarshalling in the Event Gateway produce policy resource and simplifying struct-tag parsing logic in the loader using standard library slice helpers.

Changes:

  • Unmarshal produce policy JSON into a map[string]any once and reuse it across validators (aligning with the existing consume policy pattern).
  • Simplify produce policy type validation by centralizing allowed types in a single slice and using slices.Contains.
  • Replace a manual "inline" tag scan loop with slices.Contains in structured tag parsing.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
internal/declarative/resources/event_gateway_produce_policy.go Parse JSON once for validators and simplify allowed-type handling in produce policy unmarshalling/validation.
internal/declarative/loader/env_sources.go Use slices.Contains to detect "inline" in structured field tags, reducing boilerplate.

@rspurgeon
Copy link
Copy Markdown
Collaborator

@harshadixit12 please review

Copilot AI temporarily deployed to kongctl-acceptance-5 April 27, 2026 01:41 Inactive
Copilot AI temporarily deployed to kongctl-acceptance April 27, 2026 01:41 Inactive
Copilot AI temporarily deployed to kongctl-acceptance-3 April 27, 2026 01:41 Inactive
Copilot AI temporarily deployed to kongctl-acceptance-2 April 27, 2026 01:41 Inactive
Copilot AI temporarily deployed to kongctl-acceptance-4 April 27, 2026 01:41 Inactive
@rspurgeon rspurgeon changed the title refactor: eliminate redundant JSON unmarshals in produce-policy validators; use slices.Contains in env_sources Refactor: eliminate redundant JSON unmarshals in produce-policy validators; use slices.Contains in env_sources Apr 27, 2026
@rspurgeon rspurgeon merged commit bb76fc5 into main Apr 27, 2026
26 checks passed
@rspurgeon rspurgeon deleted the copilot/simplifier-code-simplification branch April 27, 2026 20:22
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.

[simplifier] Code Simplification - 2026-04-26

4 participants