Skip to content

Commit 49593c3

Browse files
Respect JSON tags when marshaling nested structs with dynamorm:"json"
Fixed marshalComplexValue to parse and use json struct tags when marshaling nested structs to DynamoDB AttributeValues. Previously, it used Go field names (PascalCase like DID, IsInstructionallyFunded) which caused inconsistent casing in stored data. Now properly extracts field names from json tags, handling both simple tags ("fieldname") and tags with options ("fieldname,omitempty"). This ensures that nested structs with dynamorm:"json" respect the json tag naming conventions (camelCase) when stored in DynamoDB. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 86c394f commit 49593c3

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

pkg/marshal/marshaler.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -426,8 +426,26 @@ func (m *Marshaler) marshalComplexValue(v reflect.Value) (types.AttributeValue,
426426
return nil, fmt.Errorf("struct field %s: %w", field.Name, err)
427427
}
428428

429-
// Use field name as key (could be enhanced with tag parsing)
430-
structMap[field.Name] = av
429+
// Parse JSON tag to get the field name
430+
fieldName := field.Name
431+
if jsonTag := field.Tag.Get("json"); jsonTag != "" && jsonTag != "-" {
432+
// Handle json tag with options like "fieldname,omitempty"
433+
if commaIdx := 0; commaIdx < len(jsonTag) {
434+
for j, c := range jsonTag {
435+
if c == ',' {
436+
commaIdx = j
437+
break
438+
}
439+
}
440+
if commaIdx > 0 {
441+
fieldName = jsonTag[:commaIdx]
442+
} else if jsonTag != "" {
443+
fieldName = jsonTag
444+
}
445+
}
446+
}
447+
448+
structMap[fieldName] = av
431449
}
432450
return &types.AttributeValueMemberM{Value: structMap}, nil
433451

0 commit comments

Comments
 (0)