Files
Cyrene_v2/internal/services/gateway/meta.go
T
AskaEth 22e8398110 feat(contract,gateway): B4 枚举权威表 — GET /api/v1/meta/enums + 未知值回落约定
- 新增 internal/contract/enums 叶子包:platform/mood/mode 三表 + UnknownFallback="display-raw";每次调用返回新切片,无包级可变全局
- GET /api/v1/meta/enums(anyAuth 只读、纯投影、无 store/无错误路径)
- 反漂移 4 处:真构造内置适配器断言 Name() 与表双向相等、真工厂 buildAccountAdapter 逐项可建、mood 对齐 emotion 常量 + 默认转移表、mode 对齐前端 MODES(跨语言,文件缺失则 skip)
- Docs/api-client §1.1 枚举权威表 + 行15 + v0.38;api-reference §3.7 + 行12
2026-10-03 16:55:22 +08:00

43 lines
1.7 KiB
Go

package gateway
import (
"net/http"
"git.yeij.top/AskaEth/Cyrene_v2/internal/contract/enums"
)
// This file owns the read-only client metadata surface: GET /api/v1/meta/enums,
// which publishes the authoritative platform/mood/mode tables (Android
// collaboration B4, design/07 §6). Clients (Web, Android, an IM bridge) read the
// tables instead of hardcoding their own id → label mapping, so a source badge or
// a mood label cannot drift per client. The tables themselves live in
// internal/contract/enums; this handler only serves them, so there is one source
// of truth rather than a copy per consumer.
// metaEnums is the GET /api/v1/meta/enums payload. `unknown` carries the
// machine-readable fallback policy (enums.UnknownFallback) so a client reads the
// tables and the rule for an unlisted value from the same response.
type metaEnums struct {
Platforms []enums.Item `json:"platforms"`
Moods []enums.Item `json:"moods"`
Modes []enums.Item `json:"modes"`
Unknown string `json:"unknown"`
}
// handleListEnums serves the shared client enum tables. It is a pure projection
// of the Go-side authority (no store, no error path) and contains nothing
// sensitive, so it is gated like GET /api/v1/sessions: any authenticated role may
// read it.
//
// Adding a value to a table is a non-breaking change for existing clients, which
// must display an unlisted value raw (see enums.UnknownFallback) rather than
// dropping it or erroring.
func (s *Server) handleListEnums(w http.ResponseWriter, _ *http.Request) {
writeJSON(w, http.StatusOK, metaEnums{
Platforms: enums.Platforms(),
Moods: enums.Moods(),
Modes: enums.Modes(),
Unknown: enums.UnknownFallback,
})
}