Skip to content
forked from achiku/varfmt

Convert string into Go variable name following Go naming convention

License

Notifications You must be signed in to change notification settings

swipe-io/varfmt

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

ExportVarName

package varfmt_test

import (
	"fmt"

	"github.com/swipe-io/varfmt"
)

func ExampleExportVarName() {
	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.ExportVarName(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
}

UnExportVarName

package varfmt_test

import (
	"fmt"

	"github.com/swipe-io/varfmt"
)

func ExampleUnExportVarName() {
	malformattedVars := []string{
		"foo",
		"foo_bar",
		"fooBar",
		"foo_bar_buz",
		"foo_bar___buz",
		"foo_id",
		"foo_tls",
		"foo_json",
		"id_test",
		"https_test",
		"test_https",
		"test124",
	}

	for _, s := range malformattedVars {
		fmt.Printf("%s -> %s\n", s, varfmt.UnExportVarName(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
	// id_test -> idTest
	// https_test -> httpsTest
	// test_https -> testHTTPS
	// test124 -> test124
}

Credits

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

About

Convert string into Go variable name following Go naming convention

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Languages

  • Go 100.0%