funcmap.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright 2017 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package main
  15. import (
  16. "strings"
  17. "text/template"
  18. )
  19. // This file contains support functions that are passed into template
  20. // evaluation for use within templates.
  21. func hasFieldNamedOK(s *ServiceType) bool {
  22. return s.hasFieldNamed("OK")
  23. }
  24. func hasFieldNamedDefault(s *ServiceType) bool {
  25. return s.hasFieldNamed("Default")
  26. }
  27. func hasParameters(m *ServiceMethod) bool {
  28. return m.ParametersType != nil
  29. }
  30. func hasResponses(m *ServiceMethod) bool {
  31. return m.ResponsesType != nil
  32. }
  33. func hasPathParameters(m *ServiceMethod) bool {
  34. for _, field := range m.ParametersType.Fields {
  35. if field.Position == "path" {
  36. return true
  37. }
  38. }
  39. return false
  40. }
  41. func hasFormParameters(m *ServiceMethod) bool {
  42. for _, field := range m.ParametersType.Fields {
  43. if field.Position == "formdata" {
  44. return true
  45. }
  46. }
  47. return false
  48. }
  49. func goType(openapiType string) string {
  50. switch openapiType {
  51. case "number":
  52. return "int"
  53. default:
  54. return openapiType
  55. }
  56. }
  57. func parameterList(m *ServiceMethod) string {
  58. result := ""
  59. if m.ParametersType != nil {
  60. for i, field := range m.ParametersType.Fields {
  61. if i > 0 {
  62. result += ", "
  63. }
  64. result += field.ParameterName + " " + field.NativeType
  65. }
  66. }
  67. return result
  68. }
  69. func bodyParameterName(m *ServiceMethod) string {
  70. for _, field := range m.ParametersType.Fields {
  71. if field.Position == "body" {
  72. return field.JSONName
  73. }
  74. }
  75. return ""
  76. }
  77. func bodyParameterFieldName(m *ServiceMethod) string {
  78. for _, field := range m.ParametersType.Fields {
  79. if field.Position == "body" {
  80. return field.Name
  81. }
  82. }
  83. return ""
  84. }
  85. func commentForText(text string) string {
  86. result := ""
  87. lines := strings.Split(text, "\n")
  88. for i, line := range lines {
  89. if i > 0 {
  90. result += "\n"
  91. }
  92. result += "// " + line
  93. }
  94. return result
  95. }
  96. func templateHelpers() template.FuncMap {
  97. return template.FuncMap{
  98. "hasFieldNamedOK": hasFieldNamedOK,
  99. "hasFieldNamedDefault": hasFieldNamedDefault,
  100. "hasParameters": hasParameters,
  101. "hasPathParameters": hasPathParameters,
  102. "hasFormParameters": hasFormParameters,
  103. "hasResponses": hasResponses,
  104. "goType": goType,
  105. "parameterList": parameterList,
  106. "bodyParameterName": bodyParameterName,
  107. "bodyParameterFieldName": bodyParameterFieldName,
  108. "commentForText": commentForText,
  109. }
  110. }