MCPcopy Index your code
hub / github.com/coder/coder / DiscriminatedChatMessagePart

Function DiscriminatedChatMessagePart

scripts/apitypings/main.go:157–245  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

155// ChatMessagePart fields in codersdk/chats.go. This function
156// reads those tags via reflect and builds the union from them.
157func 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)

Callers

nothing calls this directly

Calls 3

parseVariantTagsFunction · 0.85
chatMessagePartTSNameFunction · 0.85
NodeMethod · 0.65

Tested by

no test coverage detected