Нет описания
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

example.go 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. // Package configure 使用示例
  2. //
  3. // 以下示例演示如何使用配置中心客户端SDK
  4. package test
  5. import (
  6. "context"
  7. "fmt"
  8. "log"
  9. "time"
  10. "git.x2erp.com/qdy/go-base/model/request/queryreq"
  11. "git.x2erp.com/qdy/go-base/sdk/configure"
  12. )
  13. // ExampleUsage 演示SDK的基本用法
  14. func ExampleUsage() {
  15. // 示例1:创建客户端
  16. exampleCreateClient()
  17. // 示例2:查询列表
  18. exampleListTables()
  19. // 示例3:创建表
  20. exampleSaveTable()
  21. // 示例4:查询详情
  22. exampleGetTable()
  23. // 示例5:删除表
  24. exampleDeleteTable()
  25. // 示例6:别名字典管理
  26. exampleAliasManagement()
  27. }
  28. func exampleCreateClient() {
  29. fmt.Println("=== 示例1:创建客户端 ===")
  30. // 方法1:使用默认配置
  31. client1, err := configure.NewClient()
  32. if err != nil {
  33. log.Printf("Failed to create default client: %v", err)
  34. } else {
  35. fmt.Printf("Default client created: URL=%s\n", client1.GetConfig().BaseURL)
  36. }
  37. // 方法2:使用Basic认证
  38. if _, err := configure.NewBasicAuthClient("http://localhost:8080", "admin", "123"); err != nil {
  39. log.Printf("Failed to create basic auth client: %v", err)
  40. } else {
  41. fmt.Println("Basic auth client created")
  42. }
  43. // 方法3:使用Token认证
  44. if _, err := configure.NewTokenAuthClient("http://localhost:8080", "your-token-here"); err != nil {
  45. log.Printf("Failed to create token auth client: %v", err)
  46. } else {
  47. fmt.Println("Token auth client created")
  48. }
  49. fmt.Println()
  50. }
  51. func exampleListTables() {
  52. fmt.Println("=== 示例2:查询数据库表字典列表 ===")
  53. // 创建客户端
  54. client, err := configure.NewBasicAuthClient("http://localhost:8080", "admin", "123")
  55. if err != nil {
  56. log.Printf("Failed to create client: %v", err)
  57. return
  58. }
  59. // 创建查询请求
  60. query := &configure.DicTableQueryRequest{
  61. QueryRequest: queryreq.QueryRequest{
  62. Page: 0,
  63. PageSize: 10,
  64. },
  65. }
  66. // 执行查询
  67. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  68. defer cancel()
  69. result, err := client.ListTables(ctx, query)
  70. if err != nil {
  71. log.Printf("Error listing tables: %v", err)
  72. return
  73. }
  74. fmt.Printf("Total tables: %d\n", result.TotalCount)
  75. fmt.Printf("Last page: %d\n", result.LastPage)
  76. fmt.Println()
  77. }
  78. func exampleSaveTable() {
  79. fmt.Println("=== 示例3:创建数据库表字典 ===")
  80. // 创建客户端
  81. client, err := configure.NewBasicAuthClient("http://localhost:8080", "admin", "123")
  82. if err != nil {
  83. log.Printf("Failed to create client: %v", err)
  84. return
  85. }
  86. // 创建请求
  87. req := &configure.DicTableRequest{
  88. TableID: "example_table_001",
  89. TableType: "实体表",
  90. Name: "示例表001",
  91. Description: "这是一个示例表",
  92. Creator: "admin",
  93. CreatedAt: time.Now(),
  94. UpdatedAt: time.Now(),
  95. Fields: []configure.DicTableFieldRequest{
  96. {
  97. FieldID: "example_table_001.id",
  98. TableID: "example_table_001",
  99. FiledType: "实际字段",
  100. DataType: "数值型",
  101. FieldName: "id",
  102. FieldNameCN: "主键ID",
  103. Description: "主键字段",
  104. Creator: "admin",
  105. CreatedAt: time.Now(),
  106. UpdatedAt: time.Now(),
  107. },
  108. {
  109. FieldID: "example_table_001.name",
  110. TableID: "example_table_001",
  111. FiledType: "实际字段",
  112. DataType: "字符型",
  113. FieldName: "name",
  114. FieldNameCN: "名称",
  115. Description: "名称字段",
  116. Creator: "admin",
  117. CreatedAt: time.Now(),
  118. UpdatedAt: time.Now(),
  119. },
  120. },
  121. }
  122. // 执行保存
  123. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  124. defer cancel()
  125. detail, err := client.SaveTable(ctx, req)
  126. if err != nil {
  127. log.Printf("Error saving table: %v", err)
  128. return
  129. }
  130. fmt.Printf("Table saved successfully: %s\n", detail.Table.TableID)
  131. fmt.Printf("Table name: %s\n", detail.Table.Name)
  132. fmt.Printf("Number of fields: %d\n", len(detail.Fields))
  133. fmt.Println()
  134. }
  135. func exampleGetTable() {
  136. fmt.Println("=== 示例4:查询数据库表字典详情 ===")
  137. // 创建客户端
  138. client, err := configure.NewBasicAuthClient("http://localhost:8080", "admin", "123")
  139. if err != nil {
  140. log.Printf("Failed to create client: %v", err)
  141. return
  142. }
  143. // 查询表详情
  144. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  145. defer cancel()
  146. tableID := "example_table_001"
  147. detail, err := client.GetTable(ctx, tableID)
  148. if err != nil {
  149. if err == configure.ErrNotFound {
  150. fmt.Printf("Table %s not found\n", tableID)
  151. } else {
  152. log.Printf("Error getting table: %v", err)
  153. }
  154. return
  155. }
  156. fmt.Printf("Table ID: %s\n", detail.Table.TableID)
  157. fmt.Printf("Table Type: %s\n", detail.Table.TableType)
  158. fmt.Printf("Name: %s\n", detail.Table.Name)
  159. fmt.Printf("Description: %s\n", detail.Table.Description)
  160. fmt.Printf("Creator: %s\n", detail.Table.Creator)
  161. fmt.Printf("Fields count: %d\n", len(detail.Fields))
  162. fmt.Println()
  163. }
  164. func exampleDeleteTable() {
  165. fmt.Println("=== 示例5:删除数据库表字典 ===")
  166. // 创建客户端
  167. client, err := configure.NewBasicAuthClient("http://localhost:8080", "admin", "123")
  168. if err != nil {
  169. log.Printf("Failed to create client: %v", err)
  170. return
  171. }
  172. // 删除表
  173. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  174. defer cancel()
  175. tableID := "example_table_001"
  176. if err := client.DeleteTable(ctx, tableID); err != nil {
  177. if err == configure.ErrNotFound {
  178. fmt.Printf("Table %s not found\n", tableID)
  179. } else {
  180. log.Printf("Error deleting table: %v", err)
  181. }
  182. return
  183. }
  184. fmt.Printf("Table %s deleted successfully\n", tableID)
  185. fmt.Println()
  186. }
  187. func exampleAliasManagement() {
  188. fmt.Println("=== 示例6:别名字典管理 ===")
  189. // 创建客户端
  190. client, err := configure.NewBasicAuthClient("http://localhost:8080", "admin", "123")
  191. if err != nil {
  192. log.Printf("Failed to create client: %v", err)
  193. return
  194. }
  195. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  196. defer cancel()
  197. // 1. 创建表别名字典
  198. tableAliasReq := &configure.TableAliasRequest{
  199. TableID: "example_table_001",
  200. TableAlias: "示例表别名",
  201. }
  202. tableAliasDetail, err := client.SaveTableAlias(ctx, tableAliasReq)
  203. if err != nil {
  204. log.Printf("Error saving table alias: %v", err)
  205. return
  206. }
  207. fmt.Printf("Table alias saved: ID=%s, TableID=%s, Alias=%s\n",
  208. tableAliasDetail.TableAlias.ID,
  209. tableAliasDetail.TableAlias.TableID,
  210. tableAliasDetail.TableAlias.TableAlias)
  211. // 2. 查询表别名字典列表
  212. query := &configure.TableAliasQueryRequest{
  213. QueryRequest: queryreq.QueryRequest{
  214. Page: 0,
  215. PageSize: 10,
  216. },
  217. TableID: "example_table_001",
  218. }
  219. tableAliasList, err := client.ListTableAliases(ctx, query)
  220. if err != nil {
  221. log.Printf("Error listing table aliases: %v", err)
  222. return
  223. }
  224. fmt.Printf("Total table aliases: %d\n", tableAliasList.TotalCount)
  225. // 3. 创建字段别名字典
  226. fieldAliasReq := &configure.TableFieldAliasRequest{
  227. FieldID: "example_table_001.id",
  228. TableID: "example_table_001",
  229. FieldName: "id",
  230. FieldAlias: "主键别名",
  231. Description: "主键字段别名",
  232. WhereCondition: "查询条件描述",
  233. }
  234. fieldAliasDetail, err := client.SaveTableFieldAlias(ctx, fieldAliasReq)
  235. if err != nil {
  236. log.Printf("Error saving field alias: %v", err)
  237. return
  238. }
  239. fmt.Printf("Field alias saved: ID=%s, FieldID=%s, Alias=%s\n",
  240. fieldAliasDetail.TableFieldAlias.ID,
  241. fieldAliasDetail.TableFieldAlias.FieldID,
  242. fieldAliasDetail.TableFieldAlias.FieldAlias)
  243. // 4. 查询字段别名字典列表
  244. fieldQuery := &configure.TableFieldAliasQueryRequest{
  245. QueryRequest: queryreq.QueryRequest{
  246. Page: 0,
  247. PageSize: 10,
  248. },
  249. TableID: "example_table_001",
  250. }
  251. fieldAliasList, err := client.ListTableFieldAliases(ctx, fieldQuery)
  252. if err != nil {
  253. log.Printf("Error listing field aliases: %v", err)
  254. return
  255. }
  256. fmt.Printf("Total field aliases: %d\n", fieldAliasList.TotalCount)
  257. fmt.Println()
  258. }