Skip to content
/ tern Public

tern is a lightweight Go package for simple, concise ternary expressions, enabling clear conditional logic.

License

Notifications You must be signed in to change notification settings

yyle88/tern

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

11 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

golang-tern-expression logo

golang-tern-expression

simple tern expression condition ? expression 1 : expression 2 in golang


GitHub Workflow Status (branch) GoDoc Coverage Status Supported Go Versions GitHub Release Go Report Card

tern

Lightweight and adaptable Go package for streamlined condition-based logic with elegant tern expressions.


CHINESE README

δΈ­ζ–‡θ―΄ζ˜Ž

Main Features

🎯 Generic Tern Operations: Type-safe condition-based expressions with comprehensive generic support
⚑ Deferred Evaluation: Deferred execution with function-based parameters to optimize performance
πŸ”„ Adaptable Conditions: Support for both boolean and function-based condition-based evaluation
🌍 Zero-Value Handling: Auto zero-value fallbacks and dedicated zero-value comparison utilities
πŸ“‹ Multiple Evaluation Modes: Direct values, functions, and zero-value aware operations

Installation

go install github.com/yyle88/tern@latest

Quick Start

Basic Tern Operations

This example demonstrates the core tern expression features with different evaluation modes.

package main

import (
	"fmt"
	"github.com/yyle88/tern"
)

func main() {
	// Basic condition-based selection
	result := tern.BVV(true, "Option A", "Option B")
	fmt.Println(result) // Output: Option A

	// Deferred execution for fallback value
	result = tern.BVF(false, "Default", func() string { return "Computed Fallback" })
	fmt.Println(result) // Output: Computed Fallback

	// Handling zero values as fallback
	result = tern.BV(false, "Fallback")
	fmt.Println(result) // Output: (blank string)
}

⬆️ Source: Source

Zero-Value Comparison

The zerotern subpackage provides utilities for zero-value aware condition-based logic.

package main

import (
	"fmt"
	"github.com/yyle88/tern/zerotern"
)

func main() {
	// Direct zero-value comparison
	result := zerotern.VV("non-zero", "fallback")
	fmt.Println(result) // Output: non-zero

	// Function-based fallback for zero values
	result = zerotern.VF("", func() string { return "fallback func" })
	fmt.Println(result) // Output: fallback func
}

⬆️ Source: Source

Function Overview

The tern package provides comprehensive condition-based evaluation functions:

Core Tern Functions

Function Condition Type Main Value Fallback Value
BVV bool Direct value Direct value
BVF bool Direct value Function returning value
BFV bool Function returning value Direct value
BFF bool Function returning value Function returning value
FVV func() bool Direct value Direct value
FVF func() bool Direct value Function returning value
FFV func() bool Function returning value Direct value
FFF func() bool Function returning value Function returning value

Zero-Value Fallback Functions

Function Condition Type Main Value Fallback Value
BV bool Direct value Zero value of type T
BF bool Function returning value Zero value of type T
FV func() bool Direct value Zero value of type T
FF func() bool Function returning value Zero value of type T

Advanced Usage

Deferred Evaluation Benefits

Performance optimization through deferred execution:

func expensiveComputation() string {
	fmt.Println("Computing...")
	return "Complex Result"
}

result := tern.BVF(false, "Default", expensiveComputation)
// Output: Default (expensiveComputation is not executed)

Working with Zero Values

Get zero value for all generic types:

fmt.Println(tern.Zero[int]())    // Output: 0
fmt.Println(tern.Zero[string]()) // Output: (blank string)

Zero-Value Comparison Package (zerotern)

The zerotern subpackage provides dedicated utilities for zero-value aware operations:

Function Comparison Type Main Value Fallback Value
VV Direct comparison Direct value Direct value
VF Direct comparison Direct value Function returning value
FV Function comparison Function returning value Direct value
FF Function comparison Function returning value Function returning value

Ref Utilities (zerotern)

Function Ref Handling Fallback Value
SetPV Ref to direct value Direct value
SetPF Ref to direct value Function returning value

Example: Ref Operations

Set ref values with zero-value awareness:

package main

import (
	"fmt"
	"github.com/yyle88/tern/zerotern"
)

func main() {
	var value int
	zerotern.SetPV(&value, 42)
	fmt.Println(value) // Output: 42

	value = 7
	zerotern.SetPF(&value, func() int { return 99 })
	fmt.Println(value) // Output: 7 (not changed since now non-zero)
}

⬆️ Source: Source

What makes tern unique?

  1. Enhanced Code clearness: Simplifies condition-based logic with concise expressions
  2. Performance Optimization: Deferred evaluation avoids needless computations
  3. Type Protection: Leverages Go's generics for maximum adaptiveness and robustness
  4. Comprehensive Coverage: Supports various condition-based scenarios including zero-value handling
  5. Simple Integration: Drop-in replacement for complex if-else chains

πŸ“„ License

MIT License. See LICENSE.


🀝 Contributing

Contributions are welcome! Report bugs, suggest features, and contribute code:

  • πŸ› Found a bug? Open an issue on GitHub with reproduction steps
  • πŸ’‘ Have a feature idea? Create an issue to discuss the suggestion
  • πŸ“– Documentation confusing? Report it so we can improve
  • πŸš€ Need new features? Share the use cases to help us understand requirements
  • ⚑ Performance issue? Help us optimize through reporting slow operations
  • πŸ”§ Configuration problem? Ask questions about complex setups
  • πŸ“’ Follow project progress? Watch the repo to get new releases and features
  • 🌟 Success stories? Share how this package improved the workflow
  • πŸ’¬ Feedback? We welcome suggestions and comments

πŸ”§ Development

New code contributions, follow this process:

  1. Fork: Fork the repo on GitHub (using the webpage UI).
  2. Clone: Clone the forked project (git clone https://github.com/yourname/repo-name.git).
  3. Navigate: Navigate to the cloned project (cd repo-name)
  4. Branch: Create a feature branch (git checkout -b feature/xxx).
  5. Code: Implement the changes with comprehensive tests
  6. Testing: (Golang project) Ensure tests pass (go test ./...) and follow Go code style conventions
  7. Documentation: Update documentation to support client-facing changes and use significant commit messages
  8. Stage: Stage changes (git add .)
  9. Commit: Commit changes (git commit -m "Add feature xxx") ensuring backward compatible code
  10. Push: Push to the branch (git push origin feature/xxx).
  11. PR: Open a pull request on GitHub (on the GitHub webpage) with detailed description.

Please ensure tests pass and include relevant documentation updates.


🌟 Support

Welcome to contribute to this project via submitting merge requests and reporting issues.

Project Support:

  • ⭐ Give GitHub stars if this project helps you
  • 🀝 Share with teammates and (golang) programming friends
  • πŸ“ Write tech blogs about development tools and workflows - we provide content writing support
  • 🌟 Join the ecosystem - committed to supporting open source and the (golang) development scene

Have Fun Coding with this package! πŸŽ‰


GitHub Stars

Stargazers

About

tern is a lightweight Go package for simple, concise ternary expressions, enabling clear conditional logic.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published