sseClientTemplateSections returns section templates for SSE client endpoints.
(data *ServiceData)
| 57 | |
| 58 | // sseClientTemplateSections returns section templates for SSE client endpoints. |
| 59 | func sseClientTemplateSections(data *ServiceData) []*codegen.SectionTemplate { |
| 60 | sections := make([]*codegen.SectionTemplate, 0) |
| 61 | for _, ed := range data.Endpoints { |
| 62 | if ed.SSE == nil { |
| 63 | continue |
| 64 | } |
| 65 | // Create a map of template functions needed for the SSE template |
| 66 | funcs := map[string]any{ |
| 67 | "dict": func(values ...any) (map[string]any, error) { |
| 68 | if len(values)%2 != 0 { |
| 69 | return nil, fmt.Errorf("odd number of arguments") |
| 70 | } |
| 71 | dict := make(map[string]any, len(values)/2) |
| 72 | for i := 0; i < len(values); i += 2 { |
| 73 | key, ok := values[i].(string) |
| 74 | if !ok { |
| 75 | return nil, fmt.Errorf("dict keys must be strings") |
| 76 | } |
| 77 | dict[key] = values[i+1] |
| 78 | } |
| 79 | return dict, nil |
| 80 | }, |
| 81 | "deref": func(ref string) string { |
| 82 | return strings.TrimPrefix(ref, "*") |
| 83 | }, |
| 84 | } |
| 85 | sections = append(sections, &codegen.SectionTemplate{ |
| 86 | Name: "client-sse", |
| 87 | Source: httpTemplates.Read(clientSseT, sseParseP), |
| 88 | Data: ed, |
| 89 | FuncMap: funcs, |
| 90 | }) |
| 91 | } |
| 92 | return sections |
| 93 | } |