Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions firestore/pipeline_constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ func ConstantOf(value any) Expression {
return &constant{baseExpression: &baseExpression{err: err}}
}
return &constant{baseExpression: &baseExpression{pbVal: pbVal}}
}

// Safely fall back to arrays/slices
switch reflect.TypeOf(value).Kind() {
case reflect.Slice, reflect.Array:
pbVal, _, err := toProtoValue(reflect.ValueOf(value))
if err != nil {
return &constant{baseExpression: &baseExpression{err: err}}
}
return &constant{baseExpression: &baseExpression{pbVal: pbVal}}
default:
return &constant{baseExpression: &baseExpression{err: fmt.Errorf("firestore: unknown constant type: %T", value)}}
}
Expand Down
55 changes: 55 additions & 0 deletions firestore/pipeline_constant_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package firestore

import (
"testing"
)

func TestConstantOf_SlicesAndArrays(t *testing.T) {
tests := []struct {
name string
input any
}{
{
name: "slice of ints",
input: []int{1, 2, 3},
},
{
name: "array of ints",
input: [3]int{1, 2, 3},
},
{
name: "slice of strings",
input: []string{"a", "b", "c"},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
expr := ConstantOf(tc.input)
if expr == nil {
t.Fatalf("ConstantOf returned nil")
}

pbVal, err := expr.toProto()
if err != nil {
t.Fatalf("toProto() failed with error: %v", err)
}
if pbVal == nil {
t.Fatalf("expected non-nil pb.Value")
}
})
}
}
6 changes: 6 additions & 0 deletions firestore/to_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ func toProtoValue(v reflect.Value) (pbv *pb.Value, sawTransform bool, err error)
case Expression:
pbVal, err := exprToProtoValue(x)
return pbVal, false, err
case AggregateFunction:
if x == nil || (reflect.ValueOf(x).Kind() == reflect.Ptr && reflect.ValueOf(x).IsNil()) {
return nullValue, false, nil
}
pbVal, err := x.toProto()
return pbVal, false, err
case Vector64:
return vectorToProtoValue(x), false, nil
case *latlng.LatLng:
Expand Down
19 changes: 19 additions & 0 deletions firestore/to_value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,25 @@ func TestToProtoValue_Conversions(t *testing.T) {
in: (*DocumentRef)(nil),
want: nullValue,
},
{
desc: "nil AggregateFunction",
in: AggregateFunction(nil),
want: nullValue,
},
{
desc: "nil pointer to AggregateFunction",
in: (*baseAggregateFunction)(nil),
want: nullValue,
},
{
desc: "AggregateFunction",
in: CountAll(),
want: &pb.Value{ValueType: &pb.Value_FunctionValue{
FunctionValue: &pb.Function{
Name: "count",
},
}},
},
{
desc: "bool",
in: true,
Expand Down
Loading