Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: Alternative approach to avoid reflection on load #448

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Commits on Aug 22, 2023

  1. Enhancement: Alternative approach to avoid reflection on load

    When loading information from the database gorp uses reflection to identify the
    field that matches with the returned column name. This search can cause some
    CPU/memory overhead on large systems.
    
    An alternative approach allows the target object to bypass this logic,
    forwarding the responsability of building the slice of attribute pointers to
    the caller.
    
    For example, the following type would bypass the reflection search with an extra method:
    
    ```go
    type Example struct {
      FieldA string
      FieldB int
      FieldC time.Time
    }
    
    func (e *Example) DBColumns(columnNames []string) ([]interface{}, error) {
      var columns []interface{}
      for _, columnName := range columnNames {
        switch columnName {
        case "fieldA":
          columns = append(columns, e.FieldA)
        case "fieldB":
          columns = append(columns, e.FieldB)
        case "fieldC":
          columns = append(columns, e.FieldC)
        default:
          return nil, fmt.Errorf("unknown column name %q", columnName)
        }
      }
      return columns, nil
    }
    ```
    
    Furthermore, the application could generate these `DBColumns` methods using `go
    generate`, keeping it as an automatic plugin code to speed up data loading.
    rafaeljusto committed Aug 22, 2023
    Configuration menu
    Copy the full SHA
    6977e79 View commit details
    Browse the repository at this point in the history