Skip to content

Commit

Permalink
snakecase nested model
Browse files Browse the repository at this point in the history
  • Loading branch information
bryzettler committed Nov 1, 2023
1 parent c84fbaf commit db6b85e
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions packages/metadata-service/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@ function generateAssetJson(record: KeyToAsset, keyStr: string) {
key_to_asset_key: record.address,
image,
hotspot_infos: {
iot: record?.iotHotspotInfo,
mobile: record?.mobileHotspotInfo,
iot: snakeCaseKeys(record?.iotHotspotInfo?.dataValues),
mobile: snakeCaseKeys(record?.mobileHotspotInfo?.dataValues),
},
entity_key_b64: record?.entityKey.toString("base64"),
key_serialization: record?.keySerialization,
Expand Down Expand Up @@ -269,6 +269,25 @@ server.get<{ Params: { eccCompact: string } }>(
}
);

function snakeCaseKeys(obj: any): any {
if (obj instanceof Array) {
return obj.map((item) => snakeCaseKeys(item));
} else if (obj instanceof Object) {
const snakeCasedObject: { [key: string]: any } = {};
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
const snakeCasedKey = key
.replace(/([A-Z])/g, (match) => `_${match.toLowerCase()}`)
.replace(/^_/, ""); // Remove leading underscore
snakeCasedObject[snakeCasedKey] = snakeCaseKeys(obj[key]);
}
}
return snakeCasedObject;
} else {
return obj;
}
}

function locationAttributes(
name: string,
info: MobileHotspotInfo | IotHotspotInfo | undefined
Expand Down

0 comments on commit db6b85e

Please sign in to comment.