DiscriminatedChatMessagePart splits the flat ChatMessagePart interface into a discriminated union of per-type sub-interfaces. Each sub-interface narrows the `type` field to a string literal and includes only the fields relevant to that part type. Variant membership is declared via `variants` struct
(ts *guts.Typescript)
| 155 | // ChatMessagePart fields in codersdk/chats.go. This function |
| 156 | // reads those tags via reflect and builds the union from them. |
| 157 | func DiscriminatedChatMessagePart(ts *guts.Typescript) { |
| 158 | node, ok := ts.Node("ChatMessagePart") |
| 159 | if !ok { |
| 160 | return |
| 161 | } |
| 162 | iface, ok := node.(*bindings.Interface) |
| 163 | if !ok { |
| 164 | return |
| 165 | } |
| 166 | |
| 167 | // Build a lookup from field name to its PropertySignature so |
| 168 | // we can copy type information from the original interface. |
| 169 | fieldMap := make(map[string]*bindings.PropertySignature, len(iface.Fields)) |
| 170 | for _, f := range iface.Fields { |
| 171 | fieldMap[f.Name] = f |
| 172 | } |
| 173 | |
| 174 | // copyField copies a field from the original interface into a |
| 175 | // sub-interface, setting QuestionToken based on whether the |
| 176 | // field is required for that variant. |
| 177 | copyField := func(name string, required bool) *bindings.PropertySignature { |
| 178 | orig, exists := fieldMap[name] |
| 179 | if !exists { |
| 180 | return nil |
| 181 | } |
| 182 | return &bindings.PropertySignature{ |
| 183 | Name: orig.Name, |
| 184 | Modifiers: orig.Modifiers, |
| 185 | QuestionToken: !required, |
| 186 | Type: orig.Type, |
| 187 | SupportComments: orig.SupportComments, |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | variants := parseVariantTags() |
| 192 | unionMembers := make([]bindings.ExpressionType, 0, len(variants)) |
| 193 | |
| 194 | for _, v := range variants { |
| 195 | fields := make([]*bindings.PropertySignature, 0, 1+len(v.required)+len(v.optional)) |
| 196 | |
| 197 | // Discriminant field: type narrowed to a string literal. |
| 198 | fields = append(fields, &bindings.PropertySignature{ |
| 199 | Name: "type", |
| 200 | Type: &bindings.LiteralType{Value: string(v.typeLiteral)}, |
| 201 | }) |
| 202 | |
| 203 | for _, name := range v.required { |
| 204 | if f := copyField(name, true); f != nil { |
| 205 | fields = append(fields, f) |
| 206 | } |
| 207 | } |
| 208 | for _, name := range v.optional { |
| 209 | if f := copyField(name, false); f != nil { |
| 210 | fields = append(fields, f) |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | tsName := chatMessagePartTSName(v.typeLiteral) |
nothing calls this directly
no test coverage detected