varfmt

package module
v0.0.0-...-f820e1e Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2016 License: MIT Imports: 2 Imported by: 14

README

varfmt

Build Status GitHub license Doc Go Report Card

Convert string into Go variable name following Go naming convention

Why created

Although there are a lot of needs to generate go struct from various sources (DDL, yml, toml, JSON, etc), I couldn't find any common library to format struct field names that follow go naming convention. Plus, I found really nice little piece of code that formats variable name in ChimeraCoder/gojson.

How to use

package varfmt_test

import (
	"fmt"

	"github.com/achiku/varfmt"
)

func ExamplePublicVarName() {
	malformattedVars := []string{
		"foo",
		"foo_bar",
		"fooBar",
		"foo_bar_buz",
		"foo_bar___buz",
		"foo_id",
		"foo_tls",
		"foo_json",
	}

	for _, s := range malformattedVars {
		fmt.Printf("%s -> %s\n", s, varfmt.PublicVarName(s))
	}
	// Output:
	// foo -> Foo
	// foo_bar -> FooBar
	// fooBar -> FooBar
	// foo_bar_buz -> FooBarBuz
	// foo_bar___buz -> FooBarBuz
	// foo_id -> FooID
	// foo_tls -> FooTLS
	// foo_json -> FooJSON

}

Credits

Code is almost entirely borrowed from https://github.com/ChimeraCoder/gojson. Huge thanks to ChimeraCoder !!

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func PublicVarName

func PublicVarName(s string) string

PublicVarName formats a string as a public go variable name

Example
package main

import (
	"fmt"

	"github.com/achiku/varfmt"
)

func main() {
	malformattedVars := []string{
		"foo",
		"foo_bar",
		"fooBar",
		"foo_bar_buz",
		"foo_bar___buz",
		"foo_id",
		"foo_tls",
		"foo_json",
	}

	for _, s := range malformattedVars {
		fmt.Printf("%s -> %s\n", s, varfmt.PublicVarName(s))
	}
}
Output:

foo -> Foo
foo_bar -> FooBar
fooBar -> FooBar
foo_bar_buz -> FooBarBuz
foo_bar___buz -> FooBarBuz
foo_id -> FooID
foo_tls -> FooTLS
foo_json -> FooJSON

Types

This section is empty.

Jump to

Keyboard shortcuts

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