feat: improve gas station management workflow
This commit is contained in:
@@ -386,9 +386,8 @@ func ResourceResponse(value any) any {
|
||||
return stripInternalIDs(decoded)
|
||||
}
|
||||
|
||||
// PublicResourceResponse additionally resolves persisted relation keys into
|
||||
// their public identities. It is used by list/detail endpoints so an edit form
|
||||
// can round-trip the relation without ever receiving a surrogate database ID.
|
||||
// PublicResourceResponse preserves a resource's own ID for administrative
|
||||
// display while resolving and removing persistence-only relation IDs.
|
||||
func PublicResourceResponse(value any) (any, error) {
|
||||
encoded, err := json.Marshal(value)
|
||||
if err != nil {
|
||||
@@ -450,7 +449,7 @@ type relationIdentityRecord struct {
|
||||
|
||||
func projectRelationIdentities(value any) (any, error) {
|
||||
groups := map[string]*relationIdentityGroup{}
|
||||
collectRelationIdentityReferences(value, groups)
|
||||
collectRelationIdentityReferences(value, groups, true)
|
||||
groupKeys := make([]string, 0, len(groups))
|
||||
for key := range groups {
|
||||
groupKeys = append(groupKeys, key)
|
||||
@@ -477,12 +476,14 @@ func projectRelationIdentities(value any) (any, error) {
|
||||
return value, nil
|
||||
}
|
||||
|
||||
func collectRelationIdentityReferences(value any, groups map[string]*relationIdentityGroup) {
|
||||
func collectRelationIdentityReferences(value any, groups map[string]*relationIdentityGroup, preserveRecordID bool) {
|
||||
switch data := value.(type) {
|
||||
case map[string]any:
|
||||
for key, item := range data {
|
||||
if key == "id" {
|
||||
delete(data, key)
|
||||
if !preserveRecordID {
|
||||
delete(data, key)
|
||||
}
|
||||
continue
|
||||
}
|
||||
if strings.HasSuffix(key, "_id") {
|
||||
@@ -515,11 +516,11 @@ func collectRelationIdentityReferences(value any, groups map[string]*relationIde
|
||||
delete(data, key)
|
||||
continue
|
||||
}
|
||||
collectRelationIdentityReferences(item, groups)
|
||||
collectRelationIdentityReferences(item, groups, false)
|
||||
}
|
||||
case []any:
|
||||
for _, item := range data {
|
||||
collectRelationIdentityReferences(item, groups)
|
||||
collectRelationIdentityReferences(item, groups, preserveRecordID)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,6 +50,38 @@ func TestResourceResponseStripsInternalIDsRecursively(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPublicResourceResponsePreservesOnlyRecordID(t *testing.T) {
|
||||
got, err := PublicResourceResponse(map[string]any{
|
||||
"id": uint64(7), "identity": "record",
|
||||
"child": map[string]any{"id": uint64(8), "identity": "child"},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
record := got.(map[string]any)
|
||||
if record["id"] != float64(7) {
|
||||
t.Fatalf("record ID = %#v, want 7", record["id"])
|
||||
}
|
||||
if _, exists := record["child"].(map[string]any)["id"]; exists {
|
||||
t.Fatal("nested database ID was exposed")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPublicResourceResponsePreservesListRecordIDs(t *testing.T) {
|
||||
got, err := PublicResourceResponse([]map[string]any{
|
||||
{"id": uint64(11), "identity": "first"},
|
||||
{"id": uint64(12), "identity": "second"},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
list := got.([]any)
|
||||
if list[0].(map[string]any)["id"] != float64(11) ||
|
||||
list[1].(map[string]any)["id"] != float64(12) {
|
||||
t.Fatalf("list record IDs were not preserved: %#v", list)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPublicFieldProtectionMasksGasorderContacts(t *testing.T) {
|
||||
value := map[string]any{"contact_name": "张三", "contact_phone": "13800138000"}
|
||||
ProtectPublicFields(value, false, false, false)
|
||||
|
||||
Reference in New Issue
Block a user