Giter VIP home page Giter VIP logo

databundler's Introduction

databundler

Embed CSV data as a Golang package.

Install

go get github.com/mmcloughlin/databundler

Example

We'll use the Seven Summits as our example. The CSV we are going to embed into a package looks like

Mount Everest,8848,8848,Asia,Himalaya,Nepal/China,1953
Aconcagua,6961,6961,South America,Andes,Argentina,1897
Denali,6194,6144,North America,Alaska Range,United States,1913
Kilimanjaro,5895,5885,Africa,,Tanzania,1889
Mount Elbrus,5642,4741,Europe,Caucasus Mountains,Russia,1874
Mount Vinson,4892,4892,Antarctica,Sentinel Range,,1966
Puncak Jaya,4884,4884,Australasia,Sudirman Range,Indonesia,1962

We specify a schema in YAML format.

name: Peak
doc: Peak represents a mountain.
collection_doc: Peaks is the Messner version of the Seven Summits.
fields:
  - name: Name
    type: string
  - name: Elevation
    doc: Elevation in meters.
    type: int
  - null # ignore Prominence
  - name: Continent
    type: string
  - name: Range
    doc: Name of mountain range.
    type: string
  - name: Country
    type: string
  - name: FirstAscentYear
    type: int

Build the Go source with the following commands.

databundler -pkg sevensummits -data sevensummits.csv -schema schema.yaml -output sevensummits.go
gofmt -w sevensummits.go

Resulting in the following code.

// Generated code. DO NOT EDIT.

package sevensummits

// Peak represents a mountain.
type Peak struct {
	Name string
	// Elevation in meters.
	Elevation int
	Continent string
	// Name of mountain range.
	Range           string
	Country         string
	FirstAscentYear int
}

// Peaks is the Messner version of the Seven Summits.
var Peaks = []Peak{
	{"Mount Everest", 8848, "Asia", "Himalaya", "Nepal/China", 1953},
	{"Aconcagua", 6961, "South America", "Andes", "Argentina", 1897},
	{"Denali", 6194, "North America", "Alaska Range", "United States", 1913},
	{"Kilimanjaro", 5895, "Africa", "", "Tanzania", 1889},
	{"Mount Elbrus", 5642, "Europe", "Caucasus Mountains", "Russia", 1874},
	{"Mount Vinson", 4892, "Antarctica", "Sentinel Range", "", 1966},
	{"Puncak Jaya", 4884, "Australasia", "Sudirman Range", "Indonesia", 1962},
}

databundler's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

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