| 238 | ) |
| 239 | |
| 240 | func extractPythonName(gname, gdoc string) (string, string, error) { |
| 241 | const ( |
| 242 | PythonName = "gopy:name " |
| 243 | NLPythonName = "\n" + PythonName |
| 244 | ) |
| 245 | i := -1 |
| 246 | var tag string |
| 247 | // Check for either a doc string that starts with our tag, |
| 248 | // or as the first token of a newline |
| 249 | if strings.HasPrefix(gdoc, PythonName) { |
| 250 | i = 0 |
| 251 | tag = PythonName |
| 252 | } else { |
| 253 | i = strings.Index(gdoc, NLPythonName) |
| 254 | tag = NLPythonName |
| 255 | } |
| 256 | if i < 0 { |
| 257 | return gname, gdoc, nil |
| 258 | } |
| 259 | s := gdoc[i+len(tag):] |
| 260 | if end := strings.Index(s, "\n"); end > 0 { |
| 261 | if !isValidPythonName(s[:end]) { |
| 262 | return "", "", fmt.Errorf("gopy: invalid identifier: %s", s[:end]) |
| 263 | } |
| 264 | return s[:end], gdoc[:i] + s[end:], nil |
| 265 | } |
| 266 | return gname, gdoc, nil |
| 267 | } |
| 268 | |
| 269 | // extractPythonNameFieldTag parses a struct field tag and returns |
| 270 | // a new python name. If the tag is not defined then the original |