Services can resolve legacy aliases and expose the catalog over CLI and HTTP instead of ad-hoc error strings. Co-authored-by: Cursor <cursoragent@cursor.com>
36 lines
862 B
Go
36 lines
862 B
Go
package domains
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
|
|
"error-code-system/errcode"
|
|
)
|
|
|
|
// ExportDomain 导出单个域的定义 JSON(字典序)。
|
|
func ExportDomain(r *errcode.Registry, domain string) ([]byte, error) {
|
|
buf := &bytes.Buffer{}
|
|
enc := json.NewEncoder(buf)
|
|
enc.SetEscapeHTML(false)
|
|
enc.SetIndent("", " ")
|
|
if err := enc.Encode(r.List(domain)); err != nil {
|
|
return nil, err
|
|
}
|
|
return buf.Bytes(), nil
|
|
}
|
|
|
|
// DomainOf 返回定义切片中的域;混合域或空切片返回错误。
|
|
func DomainOf(defs []errcode.Definition) (string, error) {
|
|
if len(defs) == 0 {
|
|
return "", fmt.Errorf("domains: empty catalog")
|
|
}
|
|
domain := defs[0].Code.Domain()
|
|
for _, def := range defs[1:] {
|
|
if def.Code.Domain() != domain {
|
|
return "", fmt.Errorf("domains: mixed domains %s and %s", domain, def.Code.Domain())
|
|
}
|
|
}
|
|
return domain, nil
|
|
}
|