Generate Codes From API Definition

Once the api definition is completed, we generate the controller code through the make ctrl command (or gf gen ctrl).

  1. $ make ctrl
  2. generated: /Users/john/Temp/demo/api/user/user.go
  3. generated: /Users/john/Temp/demo/internal/controller/user/user.go
  4. generated: /Users/john/Temp/demo/internal/controller/user/user_new.go
  5. generated: /Users/john/Temp/demo/internal/controller/user/user_v1_create.go
  6. generated: /Users/john/Temp/demo/internal/controller/user/user_v1_update.go
  7. generated: /Users/john/Temp/demo/internal/controller/user/user_v1_delete.go
  8. generated: /Users/john/Temp/demo/internal/controller/user/user_v1_get_one.go
  9. generated: /Users/john/Temp/demo/internal/controller/user/user_v1_get_list.go
  10. done!

goframe api API controller

The generated codes mainly include 3 types of files.

Abstraction API For API

Defines the api API to ensure the completeness of the controller’s API implementation, avoiding issues of missing API implementations in controller. Since GoFrame is a rigorous development framework, it controls such details well. Whether or not this feature is used by developers can be decided based on specific scenarios and needs.

  1. /Users/john/Temp/demo/api/user/user.go

This file is maintained by the development tool, and developers do not need to worry about it.

Content is as follows:

api/user/user.go

  1. // =================================================================================
  2. // Code generated and maintained by GoFrame CLI tool. DO NOT EDIT.
  3. // =================================================================================
  4. package user
  5. import (
  6. "context"
  7. "demo/api/user/v1"
  8. )
  9. type IUserV1 interface {
  10. Create(ctx context.Context, req *v1.CreateReq) (res *v1.CreateRes, err error)
  11. Update(ctx context.Context, req *v1.UpdateReq) (res *v1.UpdateRes, err error)
  12. Delete(ctx context.Context, req *v1.DeleteReq) (res *v1.DeleteRes, err error)
  13. GetOne(ctx context.Context, req *v1.GetOneReq) (res *v1.GetOneRes, err error)
  14. GetList(ctx context.Context, req *v1.GetListReq) (res *v1.GetListRes, err error)
  15. }

Controller Router Object

Used to manage the initialization of the controller, as well as data structures and constant definitions used internally by the control.

  1. generated: /Users/john/Temp/demo/internal/controller/user/user.go
  2. generated: /Users/john/Temp/demo/internal/controller/user/user_new.go

Among them, internal/controller/user/user.go is an empty source file, which can be used to define data structures, constants, and other content used by the controller internally.

internal/controller/user/user.go

  1. // =================================================================================
  2. // This is auto-generated by GoFrame CLI tool only once. Fill this file as you wish.
  3. // =================================================================================
  4. package user

Another file internal/controller/user/user_new.go is an auto-generated route object creation file.

internal/controller/user/user_new.go

  1. // =================================================================================
  2. // This is auto-generated by GoFrame CLI tool only once. Fill this file as you wish.
  3. // =================================================================================
  4. package user
  5. import (
  6. "demo/api/user"
  7. )
  8. type ControllerV1 struct{}
  9. func NewV1() user.IUserV1 {
  10. return &ControllerV1{}
  11. }

Both of these files will only be generated once, after which developers can freely modify and extend them.

Step4 - Generate Controller - 图2tip

If later we need to define a v2 API, the make ctrl command will similarly generate a type ControllerV2 struct{} structure definition and a func NewV2() user.IUserV2 initialization method.

Controller Router Implementation

Used for the implementation code files of specific api APIs. By default, codes are generated in the form of one source file per api API. Of course, it is also possible to control the aggregation of APIs defined in api files into a corresponding single source file. For specific command introductions and configurations, please refer to the chapter Controller Generating.

  1. generated: /Users/john/Temp/demo/internal/controller/user/user_v1_create.go
  2. generated: /Users/john/Temp/demo/internal/controller/user/user_v1_update.go
  3. generated: /Users/john/Temp/demo/internal/controller/user/user_v1_delete.go
  4. generated: /Users/john/Temp/demo/internal/controller/user/user_v1_get_one.go
  5. generated: /Users/john/Temp/demo/internal/controller/user/user_v1_get_list.go

Let’s open one of the files to view the generated code template:

internal/controller/user/user_v1_create.go

  1. package user
  2. import (
  3. "context"
  4. "github.com/gogf/gf/v2/errors/gcode"
  5. "github.com/gogf/gf/v2/errors/gerror"
  6. "demo/api/user/v1"
  7. )
  8. func (c *ControllerV1) Create(ctx context.Context, req *v1.CreateReq) (res *v1.CreateRes, err error) {
  9. return nil, gerror.NewCode(gcode.CodeNotImplemented)
  10. }

As we can see, this is just the implementation template for the create API we defined. We just need to complete the specific business logic of this route function.

Learning Summary

Example source code for this chapter: https://github.com/gogf/quick-demo/tree/main/internal/controller/user

The GoFrame scaffolding tool helps us generate all the code unrelated to business logic. We only need to focus on the implementation of the business logic. Moreover, these auto-generated code files are mostly fully maintained by the tool, except for a few code files that developers can extend, thus we do not need to worry about their future maintenance.

Yes, the goal of the GoFrame scaffolding tool is to allow developers to focus on the business logic itself, while all work other than business logic is handled by the development framework and scaffolding tool.

Such a development method is extremely convenient, not too comfortable! Do you think that’s all? Of course not, in the Quick Start chapter we only introduce some beginner-level features. When you delve deeper into her, you will discover more of her goodness and her understanding nature.

Next, we will complete the business logic implementation of the API and feel the charm of the GoFrame database ORM component.