Skip to content

Commit c1de4ff

Browse files
Nikoloclaude
andcommitted
Fix IndexPart to inherit Conditions from parent index
IndexPart now copies Conditions from the parent index, ensuring generated code for partial indexes includes the correct WHERE clause instead of empty conditions. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 1336bf3 commit c1de4ff

4 files changed

Lines changed: 56 additions & 6 deletions

File tree

internal/app/argen_w_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -701,9 +701,10 @@ type TriggersFoo struct {
701701
FieldsMap: map[string]ds.IndexField{
702702
"Field1": {IndField: 0, Order: 0},
703703
},
704-
Primary: false,
705-
Unique: false,
706-
Partial: true,
704+
Primary: false,
705+
Unique: false,
706+
Partial: true,
707+
Conditions: map[int]ds.IndexCondition{},
707708
},
708709
},
709710
IndexMap: map[string]int{"Field1Field2": 0, "Field1Part": 1},

internal/pkg/parser/index.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ func ParseIndexPartTag(field *ast.Field, ind *ds.IndexDeclaration, indexMap map[
6161
ind.FieldsMap[fields[exInd.Fields[f]].Name] = exInd.FieldsMap[fields[exInd.Fields[f]].Name]
6262
}
6363

64+
ind.Conditions = exInd.Conditions
65+
6466
return nil
6567
}
6668

internal/pkg/parser/index_b_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,52 @@ func TestParseIndexWithConditions(t *testing.T) {
193193
}
194194
}
195195

196+
func TestParseIndexPartWithConditions(t *testing.T) {
197+
rp := ds.NewRecordPackage()
198+
_ = rp.AddField(ds.FieldDeclaration{Name: "Status", Format: "string"})
199+
_ = rp.AddField(ds.FieldDeclaration{Name: "CreatedAt", Format: "time.Time"})
200+
_ = rp.AddField(ds.FieldDeclaration{Name: "Error", Format: "string"})
201+
202+
conditions := map[int]ds.IndexCondition{
203+
0: {ConditionType: "=", Value: []string{"active"}, IsNullCheck: false},
204+
2: {ConditionType: "is null", Value: []string{}, IsNullCheck: true},
205+
}
206+
207+
_ = rp.AddIndex(ds.IndexDeclaration{
208+
Name: "StatusCreated",
209+
Num: 0,
210+
Selector: "SelectByStatusCreated",
211+
Fields: []int{0, 1},
212+
FieldsMap: map[string]ds.IndexField{
213+
"Status": {IndField: 0, Order: ds.IndexOrderAsc},
214+
"CreatedAt": {IndField: 1, Order: ds.IndexOrderAsc},
215+
},
216+
Conditions: conditions,
217+
})
218+
219+
fields := []*ast.Field{
220+
{
221+
Names: []*ast.Ident{{Name: "StatusPart"}},
222+
Type: &ast.Ident{Name: "bool"},
223+
Tag: &ast.BasicLit{Value: "`" + `ar:"index:StatusCreated;fieldnum:1;selector:SelectByStatus"` + "`"},
224+
},
225+
}
226+
227+
err := parser.ParseIndexPart(rp, fields)
228+
if err != nil {
229+
t.Fatalf("ParseIndexPart() unexpected error: %v", err)
230+
}
231+
232+
if len(rp.Indexes) != 2 {
233+
t.Fatalf("expected 2 indexes, got %d", len(rp.Indexes))
234+
}
235+
236+
partIndex := rp.Indexes[1]
237+
assert.Check(t, cmp.DeepEqual(conditions, partIndex.Conditions), "IndexPart should inherit Conditions from parent index")
238+
assert.Check(t, partIndex.Partial, "IndexPart should have Partial=true")
239+
assert.Check(t, cmp.Equal(1, len(partIndex.Fields)), "IndexPart should have 1 field")
240+
}
241+
196242
func TestParseIndexPart(t *testing.T) {
197243
type args struct {
198244
dst *ds.RecordPackage

internal/pkg/parser/parser_b_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,10 @@ type TriggersFoo struct {
107107
FieldsMap: map[string]ds.IndexField{
108108
"Field1": {IndField: 0, Order: 0},
109109
},
110-
Primary: false,
111-
Unique: false,
112-
Partial: true,
110+
Primary: false,
111+
Unique: false,
112+
Partial: true,
113+
Conditions: map[int]ds.IndexCondition{},
113114
},
114115
},
115116
IndexMap: map[string]int{"Field1Field2": 0, "Field1Part": 1},

0 commit comments

Comments
 (0)