gobycontract

package module
v0.0.0-...-4900a46 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2019 License: BSD-3-Clause Imports: 3 Imported by: 0

README

Go By Contract

This is a simple implementation of production-safe Design by Contract in Go, using pre-conditions and post-conditions.

Whilst this implementation allows for errors to "fail hard" in development and staging environments, it also can be configured in a report-only mode for production environments.

This is an example of the following pattern: A Pattern for Validating Design by Contract Assertions in Production (with Go and Sentry)

Environment Variables

  • GOBYCONTRACT_DONTPANIC when set to "true" will stop panicking when the Require or `````Ensure```` checks fail
  • SENTRY_DSN is required to be set for the Sentry reporting of contract violations to be enabled

Per the Sentry Raven-Go documentation, the SENTRY_RELEASE and SENTRY_ENVIRONMENT environment variables may also be set.

Example Implementation

  • Require is used to determine if a precondition is met
  • Ensure is used to determine if a postcondition is met

Example of a function using such methods:

func SecondsToSecondsAndMinutes(seconds int) (minutes int, remainingSeconds int) {
	gobycontract.Require(seconds >= 0, "Input seconds must be positive")

	minutes = seconds/60
	remainingSeconds = seconds % 60

	gobycontract.Ensure(minutes >= 0, "Output minutes must be positive")
	gobycontract.Ensure(remainingSeconds >= 0, "Output remaining seconds must be positive")
	gobycontract.Ensure(remainingSeconds < 60, "There can be no more than 59 remaining seconds")

	return
}

Documentation

Overview

Go By Contract provides a lightweight mechanism to validate Design-by-Contract pre-conditions and post-conditions in production environments by using Sentry error reporting instead of "fail hard" behaviour.

This small library demonstrates the pattern described in the following blog post: https://icyapril.com/go/programming/2019/03/25/a-pattern-for-validating-design-by-contract-in-Production-with-go-and-sentry.html

Setting the GOBYCONTRACT_DONTPANIC environment variable disables panics for a production environment when set to "true", whilst the SENTRY_DSN environment will independently enable reporting to Sentry.

The following is an example of using Require for preconditions and Ensure for postconditions:

func SecondsToSecondsAndMinutes(seconds int) (minutes int, remainingSeconds int) {
	gobycontract.Require(seconds >= 0, "Input seconds must be positive")

	minutes = seconds/60
	remainingSeconds = seconds % 60

	gobycontract.Ensure(minutes >= 0, "Output minutes must be positive")
	gobycontract.Ensure(remainingSeconds >= 0, "Output remaining seconds must be positive")
	gobycontract.Ensure(remainingSeconds < 60, "There can be no more than 59 remaining seconds")

	return
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Ensure

func Ensure(pass bool, description string)

func Require

func Require(pass bool, description string)

Types

This section is empty.

Jump to

Keyboard shortcuts

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