wherefields

package
v5.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 10, 2022 License: MIT Imports: 1 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Collection

type Collection struct {
	// contains filtered or unexported fields
}

Collection stores field names of non-nil values. Meant to be used with the GORM .Where() clause.

Example
package main

import (
	"fmt"

	"github.com/iver-wharf/wharf-api/v5/internal/wherefields"
	"gorm.io/gorm"
)

func main() {
	db := &gorm.DB{} // placeholder GORM DB reference

	type Project struct {
		ProjectID uint
		Name      string
		GroupName string
	}

	params := struct {
		ProjectID *uint
		Name      *string
		GroupName *string
	}{}

	var where wherefields.Collection
	var dbProjects []Project
	err := db.
		Where(&Project{
			ProjectID: where.Uint("ProjectID", params.ProjectID),
			Name:      where.String("Name", params.Name),
			GroupName: where.String("GroupName", params.GroupName),
		}, where.NonNilFieldNames()...).
		Find(&dbProjects).
		Error

	if err != nil {
		fmt.Println("Err:", err)
		return
	}

	fmt.Println("Found projects:", dbProjects)
}
Output:

func (*Collection) AddFieldName

func (sc *Collection) AddFieldName(field string)

AddFieldName adds a string that will be returned later by NonNilFieldNames.

func (*Collection) Bool

func (sc *Collection) Bool(field string, value *bool) bool

Bool stores the field name if the value was non-nil and returns the value of the field, or false if it was nil.

func (*Collection) Int

func (sc *Collection) Int(field string, value *int) int

Int stores the field name if the value was non-nil and returns the value of the field, or zero (0) if it was nil.

func (*Collection) NonNilFieldNames

func (sc *Collection) NonNilFieldNames() []any

NonNilFieldNames returns the slice of field names. Meant to be used as the trailing arguments to the GORM .Where() clause.

func (*Collection) NullStringEmptyNull

func (sc *Collection) NullStringEmptyNull(field string, value *string) null.String

NullStringEmptyNull stores the field name if the value was non-nil and returns the value of the field and translates empty ("") to null.

func (*Collection) String

func (sc *Collection) String(field string, value *string) string

String stores the field name if the value was non-nil and returns the value of the field, or empty string ("") if it was nil.

func (*Collection) Uint

func (sc *Collection) Uint(field string, value *uint) uint

Uint stores the field name if the value was non-nil and returns the value of the field, or zero (0) if it was nil.

func (*Collection) UintPtrZeroNil

func (sc *Collection) UintPtrZeroNil(field string, value *uint) *uint

UintPtrZeroNil stores the field name if the value was non-nil and returns the value of the field and translates zero (0) to nil.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL