(lines: string[], typeName: string, fields: GoStructField[], ctx: GoCodegenCtx)
| 1119 | } |
| 1120 | |
| 1121 | function pushGoStructUnmarshalJSON(lines: string[], typeName: string, fields: GoStructField[], ctx: GoCodegenCtx): void { |
| 1122 | const unionFields = fields |
| 1123 | .map((field) => ({ field, unionField: goDiscriminatedUnionField(field.goType, ctx) })) |
| 1124 | .filter((entry): entry is { field: GoStructField; unionField: GoDiscriminatedUnionField } => entry.unionField !== undefined); |
| 1125 | if (unionFields.length === 0) return; |
| 1126 | |
| 1127 | const blockLines: string[] = []; |
| 1128 | blockLines.push(`func (r *${typeName}) UnmarshalJSON(data []byte) error {`); |
| 1129 | blockLines.push(`\ttype raw${typeName} struct {`); |
| 1130 | for (const field of fields) { |
| 1131 | const unionField = goDiscriminatedUnionField(field.goType, ctx); |
| 1132 | let rawType = field.goType; |
| 1133 | if (unionField?.kind === "single") rawType = "json.RawMessage"; |
| 1134 | if (unionField?.kind === "slice") rawType = "[]json.RawMessage"; |
| 1135 | if (unionField?.kind === "map") rawType = "map[string]json.RawMessage"; |
| 1136 | blockLines.push(`\t\t${field.goName} ${rawType} \`${field.jsonTag}\``); |
| 1137 | } |
| 1138 | blockLines.push(`\t}`); |
| 1139 | blockLines.push(`\tvar raw raw${typeName}`); |
| 1140 | blockLines.push(`\tif err := json.Unmarshal(data, &raw); err != nil {`); |
| 1141 | blockLines.push(`\t\treturn err`); |
| 1142 | blockLines.push(`\t}`); |
| 1143 | |
| 1144 | for (const field of fields) { |
| 1145 | const unionField = goDiscriminatedUnionField(field.goType, ctx); |
| 1146 | if (!unionField) { |
| 1147 | blockLines.push(`\tr.${field.goName} = raw.${field.goName}`); |
| 1148 | continue; |
| 1149 | } |
| 1150 | |
| 1151 | if (unionField.kind === "single") { |
| 1152 | blockLines.push(`\tif raw.${field.goName} != nil {`); |
| 1153 | blockLines.push(`\t\tvalue, err := ${unionField.unionInfo.unmarshalFuncName}(raw.${field.goName})`); |
| 1154 | blockLines.push(`\t\tif err != nil {`); |
| 1155 | blockLines.push(`\t\t\treturn err`); |
| 1156 | blockLines.push(`\t\t}`); |
| 1157 | blockLines.push(`\t\tr.${field.goName} = value`); |
| 1158 | blockLines.push(`\t}`); |
| 1159 | } else if (unionField.kind === "slice") { |
| 1160 | blockLines.push(`\tif raw.${field.goName} != nil {`); |
| 1161 | blockLines.push(`\t\tr.${field.goName} = make([]${unionField.unionInfo.typeName}, 0, len(raw.${field.goName}))`); |
| 1162 | blockLines.push(`\t\tfor _, rawItem := range raw.${field.goName} {`); |
| 1163 | blockLines.push(`\t\t\tvalue, err := ${unionField.unionInfo.unmarshalFuncName}(rawItem)`); |
| 1164 | blockLines.push(`\t\t\tif err != nil {`); |
| 1165 | blockLines.push(`\t\t\t\treturn err`); |
| 1166 | blockLines.push(`\t\t\t}`); |
| 1167 | blockLines.push(`\t\t\tr.${field.goName} = append(r.${field.goName}, value)`); |
| 1168 | blockLines.push(`\t\t}`); |
| 1169 | blockLines.push(`\t}`); |
| 1170 | } else { |
| 1171 | blockLines.push(`\tif raw.${field.goName} != nil {`); |
| 1172 | blockLines.push(`\t\tr.${field.goName} = make(map[string]${unionField.unionInfo.typeName}, len(raw.${field.goName}))`); |
| 1173 | blockLines.push(`\t\tfor key, rawValue := range raw.${field.goName} {`); |
| 1174 | blockLines.push(`\t\t\tvalue, err := ${unionField.unionInfo.unmarshalFuncName}(rawValue)`); |
| 1175 | blockLines.push(`\t\t\tif err != nil {`); |
| 1176 | blockLines.push(`\t\t\t\treturn err`); |
| 1177 | blockLines.push(`\t\t\t}`); |
| 1178 | blockLines.push(`\t\t\tr.${field.goName}[key] = value`); |
no test coverage detected
searching dependent graphs…