Go Huma でのフィルター クエリ パラメーターの追加
私が調べたところによると、Huma は残念ながら次のような配列クエリ フィルターをサポートしていません: filters[]=filter1&filters[]=filter2 (括弧も省略しません。例: filter=filter1&filter=filter2)。フィルターをカンマで区切る例を示す Github の issue https://github.com/danielgtaylor/huma/issues/325 を見つけたので、最終的には次のようになりました。 filters=postcode:eq:RM7(EX,作成日:gt:2024-01-01
フィルタの文書化
ボディパラメータは構造体として指定するだけで検証され、ドキュメント内で生成されますが、フィルタのドキュメント化と検証は個別に行う必要があります。
ドキュメントは、Huma.Param オブジェクト (Operation の下) の description 属性の下に簡単に追加できます。
Parameters: []*huma.Param{{ Name: "filters", In: "query", Description: "Filter properties by various fields. Separate filters by comma.\n\n" + "Format: field:operator:value\n\n" + "Supported fields:\n" + "- postcode (operator: eq)\n" + "- created (operators: gt, lt, gte, lte)\n", Schema: &huma.Schema{ Type: "string", Items: &huma.Schema{ Type: "string", Pattern: "^[a-zA-Z_]+:(eq|neq|gt|lt|gte|lte):[a-zA-Z0-9-:.]+$", }, Examples: []any{ "postcode:eq:RM7 8EX", "created:gt:2024-01-01", }, }, Required: false, }},
これで、検証用に PropertyFilterParams 構造体を定義できるようになりました。
type FilterParam struct { Field string Operator string Value interface{} } type PropertyFilterParams struct { Items []FilterParam } func (s *PropertyFilterParams) UnmarshalText(text []byte) error { equalityFields := []string{"postcode"} greaterSmallerFields := []string{} dateFields := []string{"created"} for _, item := range strings.Split(string(text), ",") { filterParam, err := parseAndValidateFilterItem(item, equalityFields, greaterSmallerFields, dateFields) if err != nil { return err } s.Items = append(s.Items, filterParam) } return nil } func (s *PropertyFilterParams) Schema(registry huma.Registry) *huma.Schema { return &huma.Schema{ Type: huma.TypeString, } } func parseAndValidateFilterItem(item string, equalityFields []string, greaterSmallerFields []string, dateFields []string) (FilterParam, error) { parts := strings.SplitN(item, ":", 3) field := parts[0] operator := parts[1] value := parts[2] if contains(equalityFields, field) { if operator != "eq" && operator != "neq" { return FilterParam{}, fmt.Errorf("Unsupported operator %s for field %s. Only 'eq' and 'neq' are supported.", operator, field) } } else if contains(greaterSmallerFields, field) { if !validation.IsValidCompareGreaterSmallerOperator(operator) { return FilterParam{}, fmt.Errorf("Unsupported operator %s for field %s. Supported operators: eq, neq, gt, lt, gte, lte.", operator, field) } } else if contains(dateFields, field) { if !validation.IsValidCompareGreaterSmallerOperator(operator) { return FilterParam{}, fmt.Errorf("Unsupported operator %s for field %s. Supported operators: eq, neq, gt, lt, gte, lte.", operator, field) } if !validation.IsValidDate(value) { return FilterParam{}, fmt.Errorf("Invalid date format: %s. Expected: YYYY-MM-DD", value) } } else { return FilterParam{}, fmt.Errorf("Unsupported filter field: %s", field) } return FilterParam{Field: field, Operator: operator, Value: value}, nil }
PropertyQueryParams 構造体に PropertyFilterParams を追加しました。
type PropertyQueryParams struct { PaginationParams Filter PropertyFilterParams `query:"filters" doc:"Filter properties by various fields"` Sort PropertySortParams `query:"sorts" doc:"Sort properties by various fields"` }
これは、PropertyQueryParams をルートに追加する様子です (フィルターの説明を含むオペレーション コード自体は getAllPropertyOperation の下にあることに注意してください。完全なコードは貼り付けていませんが、要点は理解していただければ幸いです) 。検証が失敗した場合は、422 応答がスローされます。また、渡されたフィルター値をループする方法も追加しました:
huma.Register(api, getAllPropertyOperation(schema, "get-properties", "/properties", []string{"Properties"}), func(ctx context.Context, input *struct { models.Headers models.PropertyQueryParams }) (*models.MultiplePropertyOutput, error) { for _, filter := range input.Filter.Items { fmt.Println(filter) } return mockMultiplePropertyResponse(), err }) }
これが誰かのお役に立てば幸いです。より良い解決策を見つけた場合は、コメントでお知らせください。
以上がGo Huma でのフィルター クエリ パラメーターの追加の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック











Golangは、パフォーマンスとスケーラビリティの点でPythonよりも優れています。 1)Golangのコンピレーションタイプの特性と効率的な並行性モデルにより、高い並行性シナリオでうまく機能します。 2)Pythonは解釈された言語として、ゆっくりと実行されますが、Cythonなどのツールを介してパフォーマンスを最適化できます。

Golangは並行性がCよりも優れていますが、Cは生の速度ではGolangよりも優れています。 1)Golangは、GoroutineとChannelを通じて効率的な並行性を達成します。これは、多数の同時タスクの処理に適しています。 2)Cコンパイラの最適化と標準ライブラリを介して、極端な最適化を必要とするアプリケーションに適したハードウェアに近い高性能を提供します。

goisidealforforbeginnersandsutable forcloudnetworkservicesduetoitssimplicity、andconcurrencyfeatures.1)installgofromtheofficialwebsiteandverify with'goversion'.2)

Golangは迅速な発展と同時シナリオに適しており、Cは極端なパフォーマンスと低レベルの制御が必要なシナリオに適しています。 1)Golangは、ごみ収集と並行機関のメカニズムを通じてパフォーマンスを向上させ、高配列Webサービス開発に適しています。 2)Cは、手動のメモリ管理とコンパイラの最適化を通じて究極のパフォーマンスを実現し、埋め込みシステム開発に適しています。

GolangとPythonにはそれぞれ独自の利点があります。Golangは高性能と同時プログラミングに適していますが、PythonはデータサイエンスとWeb開発に適しています。 Golangは同時性モデルと効率的なパフォーマンスで知られていますが、Pythonは簡潔な構文とリッチライブラリエコシステムで知られています。

GolangとCのパフォーマンスの違いは、主にメモリ管理、コンピレーションの最適化、ランタイム効率に反映されています。 1)Golangのゴミ収集メカニズムは便利ですが、パフォーマンスに影響を与える可能性があります。

GolangとCにはそれぞれパフォーマンス競争において独自の利点があります。1)Golangは、高い並行性と迅速な発展に適しており、2)Cはより高いパフォーマンスと微細な制御を提供します。選択は、プロジェクトの要件とチームテクノロジースタックに基づいている必要があります。

GolangisidealforBuildingsCalables Systemsduetoitsefficiency andConcurrency、Whilepythonexcelsinquickscriptinganddataanalysisduetoitssimplicityand vastecosystem.golang'ssignencouragesclean、readisinediteNeditinesinedinediseNabletinedinedinedisedisedioncourase
