lang.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Copyright 2017 The Xorm Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package main
  5. import (
  6. "io/ioutil"
  7. "strings"
  8. "text/template"
  9. "github.com/go-xorm/core"
  10. )
  11. type LangTmpl struct {
  12. Funcs template.FuncMap
  13. Formater func(string) (string, error)
  14. GenImports func([]*core.Table) map[string]string
  15. }
  16. var (
  17. mapper = &core.SnakeMapper{}
  18. langTmpls = map[string]LangTmpl{
  19. "go": GoLangTmpl,
  20. "c++": CPlusTmpl,
  21. "objc": ObjcTmpl,
  22. }
  23. )
  24. func loadConfig(f string) map[string]string {
  25. bts, err := ioutil.ReadFile(f)
  26. if err != nil {
  27. return nil
  28. }
  29. configs := make(map[string]string)
  30. lines := strings.Split(string(bts), "\n")
  31. for _, line := range lines {
  32. line = strings.TrimRight(line, "\r")
  33. vs := strings.Split(line, "=")
  34. if len(vs) == 2 {
  35. configs[strings.TrimSpace(vs[0])] = strings.TrimSpace(vs[1])
  36. }
  37. }
  38. return configs
  39. }
  40. func unTitle(src string) string {
  41. if src == "" {
  42. return ""
  43. }
  44. if len(src) == 1 {
  45. return strings.ToLower(string(src[0]))
  46. } else {
  47. return strings.ToLower(string(src[0])) + src[1:]
  48. }
  49. }
  50. func upTitle(src string) string {
  51. if src == "" {
  52. return ""
  53. }
  54. return strings.ToUpper(src)
  55. }