api file format

Overview

After the api file is written, the content of our api is uneven, as follows

  1. syntax= "v1"
  2. info(
  3. key1:value1
  4. key2:value2
  5. longkey:longvalue
  6. )
  7. type Foo {
  8. Bar string
  9. Baz int
  10. }

This api file is not read. We can use goctl api format to format api files.

Task Targets

  1. familiar with the use of goctl api format
  2. Learn about the function of the goctl api format command

Preparing

  1. Complete goctl installation

Format api file

  1. Create an api file and copy the following content to the demo.api file

    1. syntax = "v1"
    2. type User {
    3. Id int64 `json:"id"`
    4. Name string `json:"name"`
    5. Age int `json:"age"`
    6. Description string `json:"description"`
    7. }
    8. type Student {
    9. Id int64 `json:"id"`
    10. No int64 `json:"no"`
    11. Name string `json:"name"`
    12. Age int `json:"age"`
    13. Description string `json:"description"`
    14. }
    15. service User {
    16. @handler ping
    17. get /ping
    18. }
  2. Create workspace and directory

    1. $ mkdir -p ~/workspace/api/format
  3. Copy the above demo.api files to ~/workspace/api/format under directory

  4. Format api file

    1. $ cd ~/workspace/api/format
    2. $ goctl api format --dir demo.api
  5. View formatted api files

    1. syntax = "v1"
    2. type User {
    3. Id int64 `json:"id"`
    4. Name string `json:"name"`
    5. Age int `json:"age"`
    6. Description string `json:"description"`
    7. }
    8. type Student {
    9. Id int64 `json:"id"`
    10. No int64 `json:"no"`
    11. Name string `json:"name"`
    12. Age int `json:"age"`
    13. Description string `json:"description"`
    14. }
    15. service User {
    16. @handler ping
    17. get /ping
    18. }