From c5e2b77ab5d614a2cb56de2abbbf705541111f45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CNaeel=E2=80=9D?= Date: Thu, 16 Jul 2026 14:33:53 +0400 Subject: [PATCH] =?UTF-8?q?docs:=20=D0=BF=D0=BE=D0=B4=D0=B2=D0=BE=D0=B4?= =?UTF-8?q?=D0=BD=D1=8B=D0=B5=20=D0=BA=D0=B0=D0=BC=D0=BD=D0=B8=20=D0=BE?= =?UTF-8?q?=D1=82=20=D0=A1=D0=BE=D0=BD=D0=BD=D0=B5=D1=82=D0=B0=20(Required?= =?UTF-8?q?+Default,=20json-=D1=82=D0=B5=D0=B3=D0=B8,=20value=5Flist)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/TODO/resource_generator_refactor_plan.md | 72 ++++++++++++++++--- 1 file changed, 64 insertions(+), 8 deletions(-) diff --git a/docs/TODO/resource_generator_refactor_plan.md b/docs/TODO/resource_generator_refactor_plan.md index d81ec0a..400bcad 100644 --- a/docs/TODO/resource_generator_refactor_plan.md +++ b/docs/TODO/resource_generator_refactor_plan.md @@ -83,13 +83,15 @@ if v == "map-fixed" || v == "array-map-fixed" { **map-fixed** (один объект) → `schema.SingleNestedAttribute`: ```go +// ⚠️ Подполя с default → Optional+Computed+Default (см. 4.1) +// Подполя без default → Required "clusterConfiguration": schema.SingleNestedAttribute{ Required: true, Attributes: map[string]schema.Attribute{ - "cpu": schema.Int64Attribute{Required: true, ...}, - "memory": schema.Int64Attribute{Required: true, Default: int64default.StaticInt64(512)}, - "replicas": schema.Int64Attribute{Required: true, Default: int64default.StaticInt64(1)}, - "disk": schema.Int64Attribute{Required: true, Default: int64default.StaticInt64(10)}, + "cpu": schema.Int64Attribute{Optional: true, Computed: true, Default: int64default.StaticInt64(500)}, + "memory": schema.Int64Attribute{Optional: true, Computed: true, Default: int64default.StaticInt64(512)}, + "replicas": schema.Int64Attribute{Optional: true, Computed: true, Default: int64default.StaticInt64(1)}, + "disk": schema.Int64Attribute{Optional: true, Computed: true, Default: int64default.StaticInt64(10)}, }, }, ``` @@ -113,11 +115,12 @@ if v == "map-fixed" || v == "array-map-fixed" { Для `clusterConfiguration` → `PostgresClusterConfigurationModel`: ```go +// ⚠️ json-теги = оригинальный code (camelCase), tfsdk-теги = snake_case (см. 4.2) type PostgresClusterConfigurationModel struct { - Cpu types.Int64 `tfsdk:"cpu"` - Memory types.Int64 `tfsdk:"memory"` - Replicas types.Int64 `tfsdk:"replicas"` - Disk types.Int64 `tfsdk:"disk"` + Cpu types.Int64 `tfsdk:"cpu" json:"cpu"` + Memory types.Int64 `tfsdk:"memory" json:"memory"` + Replicas types.Int64 `tfsdk:"replicas" json:"replicas"` + Disk types.Int64 `tfsdk:"disk" json:"disk"` } ``` @@ -223,3 +226,56 @@ map — обратная конвертация из JSON в nested struct не - В Schema: `SingleNestedAttribute` / `ListNestedAttribute` - В Create/Modify: сериализация в JSON по родительскому ID 6. Протестировать на postgres (самый сложный — 8 map-fixed + 1 array-map-fixed) + +--- + +## Блок 4. Подводные камни (найдены Соннетом при сверке с terraform-plugin-framework) + +### 4.1 — Конфликт Required + Default (БЛОКИРУЮЩИЙ) + +Во фреймворке атрибут не может быть одновременно `Required` и иметь `Default`. +`Default` работает только с `Optional + Computed`. + +А sub_params имеют `required: true` И `default` одновременно: +```yaml +sub_params: + - code: cpu + required: true + default: 500 + - code: memory + required: true + default: 512 +``` + +**Решение**: для подполей с default → `Optional + Computed + Default`. +Для подполей без default → `Required` без Default. + +```go +// Есть default: +schema.Int64Attribute{Optional: true, Computed: true, Default: int64default.StaticInt64(500)} +// Нет default: +schema.Int64Attribute{Required: true} +``` + +### 4.2 — JSON-ключи для API (БЛОКИРУЮЩИЙ) + +`tfsdk`-теги генерятся в snake_case (`cluster_configuration`), но API ждёт +оригинальный camelCase code (`cpu`, `memory`, `replicas`). + +`json.Marshal` struct'а с tfsdk-тегами даст неверные ключи при сериализации +map-fixed → JSON для отправки в API. + +**Решение**: генерировать отдельные `json`-теги = оригинальный `code`: +```go +type PostgresClusterConfigurationModel struct { + Cpu types.Int64 `tfsdk:"cpu" json:"cpu"` + Memory types.Int64 `tfsdk:"memory" json:"memory"` + Replicas types.Int64 `tfsdk:"replicas" json:"replicas"` + Disk types.Int64 `tfsdk:"disk" json:"disk"` +} +``` + +### 4.3 — value_list подполей (out of scope) + +Подполя имеют `value_list` (например `replicas: [1,3,5,7]`, `version: ["17","16"]`). +Генерация enum-валидаторов — на этом этапе не делать. Оставить на будущее.