Skip to content

Conversation

@Bencheng21
Copy link
Contributor

@Bencheng21 Bencheng21 commented Dec 1, 2025

Today, we sort the fields alphabetically implicitly in c1.
We add more flexibilities to config schema and we allow specifying order in field schema.

Test.

image image

Summary by CodeRabbit

  • New Features

    • Introduced configurable field ordering (new option to set field order) so fields can be displayed in a custom sequence
    • Schema output is now deterministically sorted using the configured order
  • Bug Fixes

    • Validation added to ensure positive order values are unique; duplicate orders now produce validation errors

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link

coderabbitai bot commented Dec 1, 2025

Walkthrough

Adds an Order int to SchemaField, enforces uniqueness for positive Order values during field validation, updates CLI schema output to sort fields by Order before marshaling, and adds an inline proto-side comment noting no validation rules for Order.

Changes

Cohort / File(s) Change Summary
Proto validation note
pb/c1/config/v1/config.pb.validate.go
Added an inline comment "// no validation rules for Order" inside Field.validate (no behavioral change).
Schema field shape
pkg/field/fields.go
Added exported field Order int to SchemaField.
CLI schema output
pkg/cli/commands.go
MakeConfigSchemaCommand now sorts schema fields by Field.Order before marshaling to produce deterministic output.
Order validation
pkg/field/validation.go
Introduced orderMap and a uniqueness check that returns an error when a positive Order value is duplicated; consolidated local var declarations.
Field option helper
pkg/field/field_options.go
Added exported helper WithOrder(order int) fieldOption to set SchemaField.Order during construction.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

  • Inspect pkg/field/validation.go for edge cases (zero/negative Order handling, error text).
  • Verify pkg/cli/commands.go sorting stability and interaction with any secondary sorting (e.g., by name).
  • Check callers of SchemaField and usages of WithOrder for initialization expectations.

Possibly related PRs

Suggested reviewers

  • laurenleach

Poem

🐇 I hopped through fields with numbers bright,
Each one placed in tidy sight.
Duplicates shooed, order now true,
A schema parade — lined up anew. 🥕✨

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title directly describes the main change: adding the ability to specify order in fields, which matches the PR's primary objective of allowing explicit field ordering.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch ben/DUCT12852

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
proto/c1/config/v1/config.proto (1)

57-58: Consider adding inline documentation for the order field.

While the field definition is correct, adding a brief comment would help users understand its purpose and usage. For example:

  bool is_secret = 7;
-  int64 order = 8;
+  int64 order = 8; // Display order for the field in UI; 0 = unspecified, falls back to alphabetical

This is especially helpful since some other fields in the proto have explanatory comments (e.g., lines 36, 37, 42, 50).

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3f6cd4f and 7630628.

⛔ Files ignored due to path filters (2)
  • pb/c1/config/v1/config.pb.go is excluded by !**/*.pb.go
  • pb/c1/config/v1/config_protoopaque.pb.go is excluded by !**/*.pb.go
📒 Files selected for processing (3)
  • pb/c1/config/v1/config.pb.validate.go (1 hunks)
  • pkg/field/fields.go (1 hunks)
  • proto/c1/config/v1/config.proto (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: go-test (1.25.2, windows-latest)
🔇 Additional comments (2)
pb/c1/config/v1/config.pb.validate.go (1)

503-504: LGTM! Generated validation code correctly handles the new Order field.

The comment follows the established pattern for fields without validation rules, and the placement before the oneof switch is appropriate.

pkg/field/fields.go (1)

81-82: The Order field is currently unused—consider clarifying its purpose or removing if not needed.

The Order field is defined in the SchemaField struct with the comment "Displaying the SchemaField in order," but it's not accessed anywhere in the codebase. No sorting or ordering logic uses it. If this field is intended for future use to control field display order, document its semantics (e.g., how zero values are handled, sort order semantics). If it's not actively being used, consider whether it should be included or marked as reserved for future use.

FieldGroups []SchemaFieldGroup

// Displaying the SchemaField in order.
Order int
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not that it affects this PR, but how are we going to handle conflicts (two fields with same Order)?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should throw an error for this, similar to how field groups throws an error if default is set to multiple groups. @Bencheng21 what was account schema do?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point. Updated.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could check this when generating the config code, so it prevents same Order at go gen time

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds like we could call validate() while generating the config code, but I would put it in another PR if necessary.

bool is_ops = 6;
bool is_secret = 7;

int64 order = 8;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where are we going to use this order? I was expecting us to sort in baton-sdk after we make the list

confschema.Fields = append(confschema.Fields, f)

we can also sort in c1 UI but figured baton-sdk would be easier

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good. yeah, baton-sdk would be easier.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
pkg/field/validation.go (1)

355-377: Clarify positive-order uniqueness logic and make the error more actionable

The current code effectively enforces uniqueness only for f.Order > 0, but that’s not obvious on first read because the duplicate check runs for all values:

if _, ok := orderMap[f.Order]; ok {
    return fmt.Errorf("field Order should be unique")
}

if f.Order > 0 {
    orderMap[f.Order] = struct{}{}
}

This works, but:

  • It’s a bit confusing that you look up orderMap[f.Order] even when f.Order <= 0, while those values are never inserted.
  • The error message doesn’t indicate which field or order value caused the failure, which makes debugging connector schemas harder.

You can make the intent clearer and the error more useful with a small refactor:

-        if _, ok := orderMap[f.Order]; ok {
-            return fmt.Errorf("field Order should be unique")
-        }
-
-        if f.Order > 0 {
-            orderMap[f.Order] = struct{}{}
-        }
+        if f.Order > 0 {
+            if _, ok := orderMap[f.Order]; ok {
+                return fmt.Errorf("field %q has a duplicate Order value %d", f.FieldName, f.Order)
+            }
+            orderMap[f.Order] = struct{}{}
+        }

This keeps the behavior of only enforcing uniqueness for positive orders, but makes that contract explicit and surfaces enough detail to fix misconfigured schemas quickly.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7630628 and d8bc54a.

📒 Files selected for processing (2)
  • pkg/cli/commands.go (1 hunks)
  • pkg/field/validation.go (2 hunks)

Comment on lines 677 to 683
// Sort fields by Order and then FieldName.
sort.Slice(confschema.Fields, func(i, j int) bool {
if confschema.Fields[i].Order < confschema.Fields[j].Order {
return true
}
return confschema.Fields[i].FieldName < confschema.Fields[j].FieldName
})
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Explicit Order semantics are inverted when some fields keep the default Order == 0

Right now all fields start at Order == 0, and you sort purely by Order then FieldName. That means any field with a positive Order value will appear after all the default-zero fields. If the intent is that smaller explicit orders come first (e.g., Order=1 → “top of the form”) and 0 means “unspecified, use default alphabetical after ordered fields”, this behavior is reversed for partially ordered schemas.

To make Order work intuitively while preserving alphabetical ordering for unspecified fields, consider treating Order == 0 as “unspecified” and placing those after any explicitly ordered fields:

-        // Sort fields by Order and then FieldName.
-        sort.Slice(confschema.Fields, func(i, j int) bool {
-            if confschema.Fields[i].Order < confschema.Fields[j].Order {
-                return true
-            }
-            return confschema.Fields[i].FieldName < confschema.Fields[j].FieldName
-        })
+        // Sort fields by Order (explicit first) and then FieldName.
+        // Treat Order == 0 as "unspecified": they come after explicitly ordered fields,
+        // and are sorted alphabetically among themselves.
+        sort.Slice(confschema.Fields, func(i, j int) bool {
+            fi, fj := confschema.Fields[i], confschema.Fields[j]
+
+            switch {
+            case fi.Order == 0 && fj.Order == 0:
+                // Neither has explicit order: fall back to FieldName.
+                return fi.FieldName < fj.FieldName
+            case fi.Order == 0:
+                // Only j has explicit order → j comes first.
+                return false
+            case fj.Order == 0:
+                // Only i has explicit order → i comes first.
+                return true
+            case fi.Order != fj.Order:
+                // Both explicitly ordered → smaller Order first.
+                return fi.Order < fj.Order
+            default:
+                // Same explicit Order → stable, deterministic tie-breaker.
+                return fi.FieldName < fj.FieldName
+            }
+        })

This keeps the previous “alphabetical by name” behavior for fields without an explicit order, while letting connectors opt into deterministic custom ordering for selected fields without having to assign Order to every single one.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// Sort fields by Order and then FieldName.
sort.Slice(confschema.Fields, func(i, j int) bool {
if confschema.Fields[i].Order < confschema.Fields[j].Order {
return true
}
return confschema.Fields[i].FieldName < confschema.Fields[j].FieldName
})
// Sort fields by Order (explicit first) and then FieldName.
// Treat Order == 0 as "unspecified": they come after explicitly ordered fields,
// and are sorted alphabetically among themselves.
sort.Slice(confschema.Fields, func(i, j int) bool {
fi, fj := confschema.Fields[i], confschema.Fields[j]
switch {
case fi.Order == 0 && fj.Order == 0:
// Neither has explicit order: fall back to FieldName.
return fi.FieldName < fj.FieldName
case fi.Order == 0:
// Only j has explicit order → j comes first.
return false
case fj.Order == 0:
// Only i has explicit order → i comes first.
return true
case fi.Order != fj.Order:
// Both explicitly ordered → smaller Order first.
return fi.Order < fj.Order
default:
// Same explicit Order → stable, deterministic tie-breaker.
return fi.FieldName < fj.FieldName
}
})
🤖 Prompt for AI Agents
pkg/cli/commands.go around lines 677-683: the current comparator sorts purely by
Order then FieldName which causes default Order==0 fields to come before
explicitly ordered (positive) fields, inverting intended semantics; update the
comparator so fields with Order>0 come first ordered by ascending Order (and
FieldName tie-breaker), and fields with Order==0 are treated as unspecified and
placed after all explicitly ordered fields, sorted alphabetically by FieldName.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (2)
pkg/field/fields.go (1)

79-82: Consider documenting Order semantics on SchemaField

The Order int addition is straightforward, but its intended semantics (e.g., >0 means explicitly ordered and must be unique; 0/negative means “unspecified / ignore for ordering”) are only inferable from Validate. A short comment spelling that out here would make it easier for connector authors and future maintainers to use it correctly.

pkg/field/validation.go (1)

355-361: Order uniqueness logic is sound; improve error context and make intent explicit

The orderMap approach correctly enforces uniqueness of positive Order values within the selected auth group, which addresses the prior concern about conflicting Orders. Right now, though:

  • The error "field Order should be unique" gives no indication which value or fields collided.
  • The uniqueness check runs for all Order values, even though only >0 are actually tracked.

You could make this easier to debug and more self-documenting by:

  • Tracking the first field name per order value.
  • Restricting the uniqueness check to positive orders for clarity.

For example:

@@ func Validate(c Configuration, v Configurable, opts ...Option) error {
-   var (
-       present          = make(map[string]int)
-       validationErrors = &ErrConfigurationMissingFields{}
-
-       fieldGroupMap = c.FieldGroupFields(validateOpts.authGroup)
-       orderMap      = make(map[int]struct{}, len(c.Fields))
-   )
+   var (
+       present          = make(map[string]int)
+       validationErrors = &ErrConfigurationMissingFields{}
+
+       fieldGroupMap = c.FieldGroupFields(validateOpts.authGroup)
+       orderMap      = make(map[int]string, len(c.Fields)) // positive Order -> field name
+   )
@@
-       if _, ok := orderMap[f.Order]; ok {
-           return fmt.Errorf("field Order should be unique")
-       }
-
-       if f.Order > 0 {
-           orderMap[f.Order] = struct{}{}
-       }
+       if f.Order > 0 {
+           if prev, ok := orderMap[f.Order]; ok {
+               return fmt.Errorf("field Order %d is used by both %s and %s", f.Order, prev, f.FieldName)
+           }
+           orderMap[f.Order] = f.FieldName
+       }

This preserves the current behavior while making duplicate-order failures much easier to diagnose.

Also applies to: 371-377

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d8bc54a and 329b66f.

⛔ Files ignored due to path filters (2)
  • pb/c1/config/v1/config.pb.go is excluded by !**/*.pb.go
  • pb/c1/config/v1/config_protoopaque.pb.go is excluded by !**/*.pb.go
📒 Files selected for processing (4)
  • pb/c1/config/v1/config.pb.validate.go (1 hunks)
  • pkg/cli/commands.go (2 hunks)
  • pkg/field/fields.go (1 hunks)
  • pkg/field/validation.go (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • pkg/cli/commands.go
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: go-test (1.25.2, windows-latest)
🔇 Additional comments (1)
pb/c1/config/v1/config.pb.validate.go (1)

489-505: Generated validation: noting no rules for Order is appropriate

Marking // no validation rules for Order here is consistent with handling SchemaField.Order purely in our own validation (pkg/field/validation.go) rather than via PGV rules. Given this file is generated, the change looks correct as long as it comes from the updated proto.


if _, ok := orderMap[f.Order]; ok {
return fmt.Errorf("field Order should be unique")
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, there are cases where some fields have an order and some don’t. If a field doesn’t specify an order, it’ll be shown first.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems reasonable to me IMO (or they could be last).

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.

5 participants