Giter Site home page Giter Site logo

jimeh / go-render Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 100 KB

A simple and flexible solution to render a value to a io.Writer using different formats based on a format string argument.

Home Page: https://pkg.go.dev/github.com/jimeh/go-render

License: MIT License

Makefile 3.53% Go 96.47%
go golang golang-module golang-package marshaling render

go-render's Introduction

go-render

A simple and flexible solution to render a value to a io.Writer using different formats based on a format string argument.

GitHub tag (latest SemVer) Go Reference GitHub issues GitHub pull requests Coverage License Status

Designed around using a custom type/struct to render your output. Thanks to Go's marshaling interfaces, you get JSON, YAML, and XML support almost for free. While plain text output is supported by the type implementing io.Reader, io.WriterTo, fmt.Stringer, and error interfaces, or by simply being a type which can easily be type cast to a byte slice.

Originally intended to easily implement CLI tools which can output their data as plain text, as well as JSON/YAML with a simple switch of a format string. But it can just as easily render to any io.Writer.

The package is designed to be flexible and extensible with a sensible set of defaults accessible via package level functions. You can create your own Renderer for custom formats, or create new handlers that support custom formats.

Import

import "github.com/jimeh/go-render"

Usage

Basic usage to render a value to various formats into a io.Writer:

version := &Version{Version: "1.2.1", Stable: true, Latest: false}

err = render.Pretty(w, "text", version)
// 1.2.1 (stable: true, latest: false)

err = render.Pretty(w, "json", version)
// {
//   "version": "1.2.1",
//   "latest": false,
//   "stable": true
// }

err = render.Compact(w, "json", version)
// {"version":"1.2.1","latest":false,"stable":true}

err = render.Pretty(w, "yaml", version)
// version: 1.2.1
// latest: false
// stable: true

err = render.Pretty(w, "xml", version)
// <version latest="false" stable="true">1.2.1</version>

The above assumes the following Version struct:

type Version struct {
    Version string `json:"version" yaml:"version" xml:",chardata"`
    Latest  bool   `json:"latest" yaml:"latest" xml:"latest,attr"`
    Stable  bool   `json:"stable" yaml:"stable" xml:"stable,attr"`
}

func (v *Version) String() string {
    return fmt.Sprintf(
        "%s (stable: %t, latest: %t)", v.Version, v.Stable, v.Latest,
    )
}

Documentation

Please see the Go Reference for documentation and further examples.

License

MIT

go-render's People

Contributors

jimeh avatar jimehbot[bot] avatar

Watchers

 avatar  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.