mirror of
https://github.com/googleapis/genai-toolbox.git
synced 2026-01-30 09:48:27 -05:00
This commit introduces a new `orderedmap` package to preserve the column order of SQL query results when they are marshaled to JSON. The default Go `json.Marshal` function sorts map keys, which was causing the column order to be lost in the output of the database tools. This commit updates the following tools to use the new `orderedmap` package: - `mysqlexecutesql` - `mssqlexecutesql` - `postgresexecutesql` - `spannerexecutesql` - `sqliteexecutesql` - `bigqueryexecutesql` A new test has been added to the `mysqlexecutesql` tool to verify that the column order is preserved. ## Description > Should include a concise description of the changes (bug or feature), it's > impact, along with a summary of the solution ## PR Checklist > Thank you for opening a Pull Request! Before submitting your PR, there are a > few things you can do to make sure it goes smoothly: - [ ] Make sure you reviewed [CONTRIBUTING.md](https://github.com/googleapis/genai-toolbox/blob/main/CONTRIBUTING.md) - [ ] Make sure to open an issue as a [bug/issue](https://github.com/googleapis/genai-toolbox/issues/new/choose) before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea - [ ] Ensure the tests and linter pass - [ ] Code coverage does not decrease (if any source code was changed) - [ ] Appropriate docs were updated (if necessary) - [ ] Make sure to add `!` if this involve a breaking change 🛠️ Fixes #1492 --------- Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> Co-authored-by: Yuan Teoh <yuanteoh@google.com> Co-authored-by: Yuan Teoh <45984206+Yuan325@users.noreply.github.com>
63 lines
1.6 KiB
Go
63 lines
1.6 KiB
Go
// Copyright 2025 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 orderedmap
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
)
|
|
|
|
// Column represents a single column in a row.
|
|
type Column struct {
|
|
Name string
|
|
Value any
|
|
}
|
|
|
|
// Row represents a row of data with columns in a specific order.
|
|
type Row struct {
|
|
Columns []Column
|
|
}
|
|
|
|
// Add adds a new column to the row.
|
|
func (r *Row) Add(name string, value any) {
|
|
r.Columns = append(r.Columns, Column{Name: name, Value: value})
|
|
}
|
|
|
|
// MarshalJSON implements the json.Marshaler interface for the Row struct.
|
|
// It marshals the row into a JSON object, preserving the order of the columns.
|
|
func (r Row) MarshalJSON() ([]byte, error) {
|
|
var buf bytes.Buffer
|
|
buf.WriteString("{")
|
|
for i, col := range r.Columns {
|
|
if i > 0 {
|
|
buf.WriteString(",")
|
|
}
|
|
// Marshal the key
|
|
key, err := json.Marshal(col.Name)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
buf.Write(key)
|
|
buf.WriteString(":")
|
|
// Marshal the value
|
|
val, err := json.Marshal(col.Value)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
buf.Write(val)
|
|
}
|
|
buf.WriteString("}")
|
|
return buf.Bytes(), nil
|
|
}
|