GithubHelp home page GithubHelp logo

bynil / optional Goto Github PK

View Code? Open in Web Editor NEW

This project forked from markphelps/optional

0.0 1.0 0.0 277 KB

Optional is a library of optional Go types

Home Page: https://godoc.org/github.com/markphelps/optional

License: MIT License

Go 94.20% Makefile 5.80%

optional's Introduction

Optional

CI Release Software License Go Doc Go Report Card SayThanks.io

Optional is a library that provides option types for the primitive Go types.

It can also be used as a tool to generate option type wrappers around your own types.

Motivation

In Go, variables declared without an explicit initial value are given their zero value. Most of the time this is what you want, but sometimes you want to be able to tell if a variable was set or if it's just a zero value. That's where option types come in handy.

Inspiration

Tool

Install

go get -u github.com/markphelps/optional/cmd/optional

Usage

Typically this process would be run using go generate, like this:

import _ "github.com/markphelps/optional

//go:generate optional -type=Foo
type Foo struct {
  ...
}

๐Ÿ—’๏ธ Note: the blank import is necessary to prevent go mod tidy from removing this library as a dependency if you are only using it to generate your own types.

running this command:

optional -type=Foo

in the same directory will create the file optional_foo.go containing a definition of:

type OptionalFoo struct {
  ...
}

The default type is OptionalT or optionalT (depending on if the type is exported) and output file is optional_t.go. This can be overridden with the -output flag.

Library

Usage

package main

import (
    "fmt"

    "github.com/markphelps/optional"
)

func main() {
    s := optional.NewString("foo")

    value, err := s.Get()
    if err != nil {
        // handle error!
    } else {
        fmt.Println(value)
    }

    t := optional.String{}
    fmt.Println(t.OrElse("bar"))
}

See example_test.go and the documentation for more usage.

Marshalling/Unmarshalling JSON

Note: v0.6.0 introduces a potential breaking change to anyone depending on marshalling non-present values to their zero values instead of null. See: #9 for more context.

Note: v0.8.0 removes JSON support from complex64 and complex128 types per #13

Option types marshal to/from JSON as you would expect:

Marshalling

package main

import (
    "encoding/json"
    "fmt"
)

func main() {
    var value = struct {
        Field optional.String `json:"field,omitempty"`
    }{
        Field: optional.NewString("bar"),
    }

    out, _ := json.Marshal(value)

    fmt.Println(string(out))
    // outputs: {"field":"bar"}
}

Unmarshalling

package main

import (
    "encoding/json"
    "fmt"
)

func main() {
    var value = &struct {
        Field optional.String `json:"field,omitempty"`
    }{}

    _ = json.Unmarshal([]byte(`{"field":"bar"}`), value)

    value.Field.If(func(s string) {
        fmt.Println(s)
    })
    // outputs: bar
}

See example_test.go for more examples.

Test Coverage

As you can see test coverage is a bit lacking for the library. This is simply because testing generated code is not super easy. I'm currently working on improving test coverage for the generated types, but in the meantime checkout string_test.go and int_test.go for examples.

Also checkout:

Golden Files

If changing the API you may need to update the golden files for your tests to pass by running:

go test ./cmd/optional/... -update.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'feat: Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Conventional Commits

Optional uses Conventional Commits for commit messages. This allows us to automatically generate changelogs and releases.

To help with this, we use pre-commit to automatically lint commit messages. To install pre-commit, run:

pip install pre-commit or brew install pre-commit (if you're on a Mac)

Then run pre-commit install to install the git hook.

optional's People

Contributors

markphelps avatar mwielbut avatar dmarkham avatar roudy16 avatar bynil avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.