compare

package
v0.0.0-...-886b66b Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2022 License: Apache-2.0 Imports: 2 Imported by: 0

README

The deriveCompare function is a maintainable and fast way to implement fast Less functions.

Given the following input:

package compare

import "sort"

type MyStruct struct {
	Int64     int64
	StringPtr *string
}

func sortStructs(ss []*MyStruct) {
	sort.Slice(ss, func(i, j int) bool {
		return deriveCompare(ss[i], ss[j]) < 0
	})
}

goderive will generate the following code:

// Code generated by goderive DO NOT EDIT.

package compare

import (
	"strings"
)

// deriveCompare returns:
//   * 0 if this and that are equal,
//   * -1 is this is smaller and
//   * +1 is this is bigger.
func deriveCompare(this, that *MyStruct) int {
	if this == nil {
		if that == nil {
			return 0
		}
		return -1
	}
	if that == nil {
		return 1
	}
	if c := deriveCompare_(this.Int64, that.Int64); c != 0 {
		return c
	}
	if c := deriveCompare_1(this.StringPtr, that.StringPtr); c != 0 {
		return c
	}
	return 0
}

// deriveCompare_ returns:
//   * 0 if this and that are equal,
//   * -1 is this is smaller and
//   * +1 is this is bigger.
func deriveCompare_(this, that int64) int {
	if this != that {
		if this < that {
			return -1
		} else {
			return 1
		}
	}
	return 0
}

// deriveCompare_1 returns:
//   * 0 if this and that are equal,
//   * -1 is this is smaller and
//   * +1 is this is bigger.
func deriveCompare_1(this, that *string) int {
	if this == nil {
		if that == nil {
			return 0
		}
		return -1
	}
	if that == nil {
		return 1
	}
	return deriveCompare_s(*this, *that)
}

// deriveCompare_s returns:
//   * 0 if this and that are equal,
//   * -1 is this is smaller and
//   * +1 is this is bigger.
func deriveCompare_s(this, that string) int {
	return strings.Compare(this, that)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MyStruct

type MyStruct struct {
	Int64     int64
	StringPtr *string
}

Jump to

Keyboard shortcuts

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