| 1 |
package xmlgen |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
import ( |
| 6 |
"encoding/xml" |
| 7 |
"fmt" |
| 8 |
"log" |
| 9 |
"regexp" |
| 10 |
"strings" |
| 11 |
|
| 12 |
"github.com/iancoleman/strcase" |
| 13 |
) |
| 14 |
|
| 15 |
type AgiDocModel struct { |
| 16 |
Name string `xml:"name,attr" json:"name,omitempty"` |
| 17 |
Language string `xml:"language,attr" json:"language,omitempty"` |
| 18 |
Synopsis string `xml:"synopsis" json:"synopsis,omitempty"` |
| 19 |
Syntax *SyntaxDocModel `xml:"syntax" json:"syntax,omitempty"` |
| 20 |
SeeAlso *SeeAlsoModel `xml:"see-also" json:"seeAlso,omitempty"` |
| 21 |
Description *DescriptionModel `xml:"description" json:"description,omitempty"` |
| 22 |
} |
| 23 |
|
| 24 |
type ManagerResponseListElementModel struct { |
| 25 |
Text string `xml:",chardata" json:"text,omitempty"` |
| 26 |
ManagerEvent []*ManagerEventModel `xml:"managerEvent" json:"managerEvent,omitempty"` |
| 27 |
} |
| 28 |
type ManagerResponseModel struct { |
| 29 |
Text string `xml:",chardata" json:"text,omitempty"` |
| 30 |
ListElements *ManagerResponseListElementModel `xml:"list-elements" json:"listElements,omitempty"` |
| 31 |
ManagerEvent *ManagerEventModel `xml:"managerEvent" json:"managerEvent,omitempty"` |
| 32 |
} |
| 33 |
|
| 34 |
func (m ManagerResponseModel) Events() []*ManagerEventModel { |
| 35 |
var o []*ManagerEventModel |
| 36 |
if m.ManagerEvent != nil { |
| 37 |
o = append(o, m.ManagerEvent) |
| 38 |
} |
| 39 |
if m.ListElements != nil { |
| 40 |
o = append(o, m.ListElements.ManagerEvent...) |
| 41 |
} |
| 42 |
return o |
| 43 |
} |
| 44 |
|
| 45 |
type ManagerActionModel struct { |
| 46 |
Name string `xml:"name,attr" json:"name,omitempty"` |
| 47 |
Language string `xml:"language,attr" json:"language,omitempty"` |
| 48 |
Synopsis string `xml:"synopsis" json:"synopsis,omitempty"` |
| 49 |
Syntax *SyntaxDocModel `xml:"syntax" json:"syntax,omitempty"` |
| 50 |
SeeAlso *SeeAlsoModel `xml:"see-also" json:"seeAlso,omitempty"` |
| 51 |
Description *DescriptionModel `xml:"description" json:"description,omitempty"` |
| 52 |
Responses ManagerResponseModel `xml:"responses" json:"responses,omitempty"` |
| 53 |
} |
| 54 |
type ManagerEventInstanceModel struct { |
| 55 |
Text string `xml:",chardata"` |
| 56 |
Class string `xml:"class,attr"` |
| 57 |
Synopsis string `xml:"synopsis"` |
| 58 |
Language string `xml:"language,attr" json:"language,omitempty"` |
| 59 |
Syntax *SyntaxDocModel `xml:"syntax" json:"syntax,omitempty"` |
| 60 |
SeeAlso *SeeAlsoModel `xml:"see-also" json:"seeAlso,omitempty"` |
| 61 |
Description *DescriptionModel `xml:"description" json:"description,omitempty"` |
| 62 |
} |
| 63 |
type ManagerEventModel struct { |
| 64 |
Name string `xml:"name,attr" json:"name,omitempty"` |
| 65 |
Instance ManagerEventInstanceModel `xml:"managerEventInstance"` |
| 66 |
} |
| 67 |
|
| 68 |
type SyntaxDocModel struct { |
| 69 |
Parameter []SyntaxParameterDocModel `xml:"parameter" json:"parameter,omitempty"` |
| 70 |
ChannelSnapshot []struct { |
| 71 |
Text string `xml:",chardata"` |
| 72 |
Prefix string `xml:"prefix,attr"` |
| 73 |
} `xml:"channel_snapshot"` |
| 74 |
BridgeSnapshot []struct { |
| 75 |
Text string `xml:",chardata"` |
| 76 |
Prefix string `xml:"prefix,attr"` |
| 77 |
} `xml:"bridge_snapshot"` |
| 78 |
} |
| 79 |
type SyntaxParameterDocModel struct { |
| 80 |
Name string `xml:"name,attr" json:"name,omitempty"` |
| 81 |
Required bool `xml:"required,attr" json:"required,omitempty"` |
| 82 |
Para ParaRaw `xml:"para" json:"para,omitempty"` |
| 83 |
EnumList *EnumListModel `xml:"enumlist" json:"enumList,omitempty"` |
| 84 |
|
| 85 |
Note *struct { |
| 86 |
Text string `xml:",chardata"` |
| 87 |
Para struct { |
| 88 |
Text string `xml:",chardata"` |
| 89 |
Literal []string `xml:"literal"` |
| 90 |
Filename string `xml:"filename"` |
| 91 |
Replaceable string `xml:"replaceable"` |
| 92 |
} `xml:"para"` |
| 93 |
} `xml:"note"` |
| 94 |
} |
| 95 |
type DocModel struct { |
| 96 |
XMLName xml.Name `xml:"docs" json:"xmlName,omitempty"` |
| 97 |
Agi []*AgiDocModel `xml:"agi" json:"agi,omitempty"` |
| 98 |
Manager []*ManagerActionModel `xml:"manager" json:"manager,omitempty"` |
| 99 |
ManagerEvent []*ManagerEventModel `xml:"managerEvent" json:"managerEvent,omitempty"` |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
type ParaRaw struct { |
| 104 |
Data string `xml:",innerxml" json:"data,omitempty"` |
| 105 |
} |
| 106 |
type DescriptionModel struct { |
| 107 |
|
| 108 |
Para []ParaRaw `xml:"para" json:"para,omitempty"` |
| 109 |
EnumList *EnumListModel `xml:"enumlist" json:"enumList,omitempty"` |
| 110 |
VariableList *VariableListModel `xml:"variablelist" json:"variableList,omitempty"` |
| 111 |
Note *struct { |
| 112 |
Text string `xml:",chardata"` |
| 113 |
Para struct { |
| 114 |
Text string `xml:",chardata"` |
| 115 |
Filename string `xml:"filename"` |
| 116 |
} `xml:"para"` |
| 117 |
} `xml:"note"` |
| 118 |
} |
| 119 |
|
| 120 |
type VariableListVariableValueModel struct { |
| 121 |
Text string `xml:",chardata" json:"text,omitempty"` |
| 122 |
Name string `xml:"name,attr" json:"name,omitempty"` |
| 123 |
} |
| 124 |
type VariableListVariableModel struct { |
| 125 |
Name string `xml:"name,attr" json:"name,omitempty"` |
| 126 |
Para ParaRaw `xml:"para" json:"para,omitempty"` |
| 127 |
Value []VariableListVariableValueModel `xml:"value" json:"value,omitempty"` |
| 128 |
} |
| 129 |
type VariableListModel struct { |
| 130 |
Variable []VariableListVariableModel `xml:"variable" json:"variable,omitempty"` |
| 131 |
} |
| 132 |
type SeeAlsoModel struct { |
| 133 |
Ref []SeeAlsoRefModel `json:"ref,omitempty"` |
| 134 |
} |
| 135 |
type SeeAlsoRefModel struct { |
| 136 |
Text string `xml:",chardata" json:"text,omitempty"` |
| 137 |
Type string `xml:"type,attr" json:"type,omitempty"` |
| 138 |
} |
| 139 |
|
| 140 |
type EnumModel struct { |
| 141 |
|
| 142 |
Name string `xml:"name,attr" json:"name,omitempty"` |
| 143 |
Para ParaRaw `xml:"para" json:"para,omitempty"` |
| 144 |
} |
| 145 |
type EnumListModel struct { |
| 146 |
|
| 147 |
Enum []EnumModel `xml:"enum" json:"enum,omitempty"` |
| 148 |
} |
| 149 |
|
| 150 |
func (d *DocModel) Synopsis(in string) string { |
| 151 |
s := strings.TrimSpace(in) |
| 152 |
s = deindent(s) |
| 153 |
return s |
| 154 |
} |
| 155 |
func (d *DocModel) ManagerModel(in *ManagerActionModel) *ManagerAction { |
| 156 |
o := &ManagerAction{ |
| 157 |
Name: in.Name, |
| 158 |
Synopsis: d.Synopsis(in.Synopsis), |
| 159 |
Syntax: d.Syntax(in.Syntax), |
| 160 |
SeeAlso: d.SeeAlso(in.SeeAlso), |
| 161 |
Description: d.Description(in.Description), |
| 162 |
} |
| 163 |
resp := in.Responses.Events() |
| 164 |
|
| 165 |
for _, v := range resp { |
| 166 |
o.Responses = append(o.Responses, d.ManagerEventModel(v)) |
| 167 |
} |
| 168 |
|
| 169 |
if len(resp) > 1 { |
| 170 |
fmt.Println("Multi response", len(resp), o.Name) |
| 171 |
for _, v := range resp { |
| 172 |
fmt.Print(" ", v.Name) |
| 173 |
} |
| 174 |
fmt.Println() |
| 175 |
} |
| 176 |
return o |
| 177 |
} |
| 178 |
|
| 179 |
func (d *DocModel) ManagerEventModel(in *ManagerEventModel) *ManagerEvent { |
| 180 |
o := &ManagerEvent{ |
| 181 |
Name: in.Name, |
| 182 |
Synopsis: d.Synopsis(in.Instance.Synopsis), |
| 183 |
Syntax: d.Syntax(in.Instance.Syntax), |
| 184 |
SeeAlso: d.SeeAlso(in.Instance.SeeAlso), |
| 185 |
} |
| 186 |
return o |
| 187 |
} |
| 188 |
func (d *DocModel) Description(in *DescriptionModel) string { |
| 189 |
if in == nil { |
| 190 |
return "" |
| 191 |
} |
| 192 |
var p []string |
| 193 |
for _, v := range in.Para { |
| 194 |
p = append(p, v.Data) |
| 195 |
} |
| 196 |
|
| 197 |
return formatParas(p) |
| 198 |
} |
| 199 |
func (d *DocModel) AGIModel(in *AgiDocModel) *AGICommand { |
| 200 |
o := &AGICommand{ |
| 201 |
Name: in.Name, |
| 202 |
Synopsis: d.Synopsis(in.Synopsis), |
| 203 |
Syntax: d.Syntax(in.Syntax), |
| 204 |
SeeAlso: d.SeeAlso(in.SeeAlso), |
| 205 |
Description: d.Description(in.Description), |
| 206 |
} |
| 207 |
return o |
| 208 |
} |
| 209 |
func (d *DocModel) SeeAlso(in *SeeAlsoModel) (o []*SeeAlso) { |
| 210 |
if in == nil { |
| 211 |
return |
| 212 |
} |
| 213 |
for _, v := range in.Ref { |
| 214 |
o = append(o, &SeeAlso{ |
| 215 |
Name: v.Text, |
| 216 |
Type: v.Type, |
| 217 |
}) |
| 218 |
} |
| 219 |
return |
| 220 |
} |
| 221 |
func (d *DocModel) Syntax(in *SyntaxDocModel) *Syntax { |
| 222 |
o := &Syntax{} |
| 223 |
|
| 224 |
if in == nil { |
| 225 |
return o |
| 226 |
} |
| 227 |
for _, v := range in.Parameter { |
| 228 |
parameter := d.Parameter(&v) |
| 229 |
if parameter.Name == "" { |
| 230 |
log.Println("drop param", in) |
| 231 |
o.Missing = true |
| 232 |
continue |
| 233 |
} |
| 234 |
o.Params = append(o.Params, parameter) |
| 235 |
} |
| 236 |
return o |
| 237 |
} |
| 238 |
|
| 239 |
var regDefaultTo = regexp.MustCompile(`Defaults to\s*<literal>(.*?)</literal>`) |
| 240 |
|
| 241 |
func parseParaDefaultTo(s string) string { |
| 242 |
sub := regDefaultTo.FindStringSubmatch(s) |
| 243 |
if len(sub) > 1 { |
| 244 |
return sub[1] |
| 245 |
} |
| 246 |
return "" |
| 247 |
} |
| 248 |
|
| 249 |
var regIndent = regexp.MustCompile(`(?m)^\s+`) |
| 250 |
|
| 251 |
func deindent(s string) string { |
| 252 |
return regIndent.ReplaceAllString(s, "") |
| 253 |
} |
| 254 |
|
| 255 |
var paraTagReplace = strings.NewReplacer( |
| 256 |
"<literal>", "`", "</literal>", "`", |
| 257 |
"<variable>", "`", "</variable>", "`", |
| 258 |
"<directory>", "`", "</directory>", "`", |
| 259 |
"<filename>", " ", "</filename>", " ", |
| 260 |
"<replaceable>", " ", "</replaceable>", " ", |
| 261 |
"\n", " ", |
| 262 |
) |
| 263 |
|
| 264 |
func formatParas(s []string) string { |
| 265 |
sb := strings.Builder{} |
| 266 |
for _, v := range s { |
| 267 |
sb.WriteString(formatPara(v)) |
| 268 |
sb.WriteString("\n\n") |
| 269 |
} |
| 270 |
return strings.TrimSpace(sb.String()) |
| 271 |
} |
| 272 |
func formatPara(s string) string { |
| 273 |
s = deindent(s) |
| 274 |
return paraTagReplace.Replace(s) |
| 275 |
} |
| 276 |
func (*DocModel) Parameter(in *SyntaxParameterDocModel) *Parameter { |
| 277 |
o := &Parameter{ |
| 278 |
RawName: in.Name, |
| 279 |
Required: in.Required, |
| 280 |
} |
| 281 |
para := strings.TrimSpace(in.Para.Data) |
| 282 |
if para != "" { |
| 283 |
o.Default = parseParaDefaultTo(para) |
| 284 |
if o.Default == "" { |
| 285 |
|
| 286 |
o.Description = formatPara(para) |
| 287 |
} |
| 288 |
} |
| 289 |
processParameter(o) |
| 290 |
return o |
| 291 |
} |
| 292 |
|
| 293 |
func processParameter(p *Parameter) { |
| 294 |
rawName := p.RawName |
| 295 |
var name string |
| 296 |
typ := "string" |
| 297 |
switch rawName { |
| 298 |
case "skipms": |
| 299 |
name = "SkipMS" |
| 300 |
case "offsetms": |
| 301 |
name = "OffsetMS" |
| 302 |
case "keytree": |
| 303 |
name = "KeyTree" |
| 304 |
} |
| 305 |
if name == "" { |
| 306 |
name = rawName |
| 307 |
|
| 308 |
name = strings.ReplaceAll(name, "=", "-") |
| 309 |
if len(name) > 4 { |
| 310 |
name = strings.ReplaceAll(name, "name", "Name") |
| 311 |
} |
| 312 |
|
| 313 |
name = strcase.ToCamel(name) |
| 314 |
} |
| 315 |
switch name { |
| 316 |
case "SkipMS", "OffsetMS", "SampleOffset", "Priority", "Timeout": |
| 317 |
typ = "int" |
| 318 |
case "Time": |
| 319 |
typ = "float64" |
| 320 |
case "": |
| 321 |
default: |
| 322 |
if name[0] >= '0' && name[0] <= '9' { |
| 323 |
name = "Field" + name |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
p.Name = name |
| 328 |
p.Type = typ |
| 329 |
} |