gocase

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2022 License: MIT Imports: 5 Imported by: 14

README

gocase

GoDoc CI codecov MIT License

A golang package to convert normal CamelCase to Golang's CamelCase and vice versa.

Install

go get -u github.com/takuoki/gocase

Usage

Default converter
str := gocase.To("IpAddress") // "IPAddress"
str := gocase.To("defaultDNSServer") // "defaultDnsServer"
Custom converter
converter, _ := gocase.New(gocase.WithInitialisms("JSON", "CSV"))

str1 := converter.To("IpAddress") // "IpAddress" (no convert)
str2 := converter.To("JsonFile")  // "JSONFile"
str3 := converter.To("CsvFile")   // "CSVFile"

Example

The default converter converts as follows

  • To: converts a string from From to To.
  • Revert: converts a string from To to From.
From To
"" ""
"jsonFile" "jsonFile"
"IpAddress" "IPAddress"
"defaultDnsServer" "defaultDNSServer"
"somethingHttpApiId" "somethingHTTPAPIID"
"somethingUuid" "somethingUUID"
"somethingSip" "somethingSIP"

!!! WARNING !!!

Note that it is impossible to accurately determine the word break in a string of consecutive uppercase words, so the string converted with To and Revert may not match the original string.

example

  • IdB --To-> IDB --Revert-> IDb
  • UUid --To-> UUID --Revert-> Uuid

Reference

For more details, please see initialisms section in Staticcheck.

Documentation

Overview

Package gocase is a package to convert normal CamelCase to Golang's CamelCase and vice versa. Golang's CamelCase means a string that takes into account to Go's common initialisms. For more details, please see initialisms section in Staticcheck.

Example
package main

import (
	"fmt"

	"github.com/takuoki/gocase"
)

func main() {

	strs := []string{"IpAddress", "defaultDnsServer", "JsonFile", "CsvFile"}

	fmt.Println("-- Default converter --")
	for _, str := range strs {
		fmt.Printf("%s --To-> %s --Revert-> %s\n", str, gocase.To(str), gocase.Revert(gocase.To(str)))
	}
	fmt.Println()

	fmt.Println("-- Custom converter --")
	conv1, _ := gocase.New(gocase.WithInitialisms("JSON", "CSV"))
	for _, str := range strs {
		fmt.Printf("%s --To-> %s --Revert-> %s\n", str, conv1.To(str), conv1.Revert(conv1.To(str)))
	}
	fmt.Println()

	fmt.Println("-- Custom converter (add to default) --")
	initialisms := append([]string{"JSON", "CSV"}, gocase.DefaultInitialisms...)
	conv2, _ := gocase.New(gocase.WithInitialisms(initialisms...))
	for _, str := range strs {
		fmt.Printf("%s --To-> %s --Revert-> %s\n", str, conv2.To(str), conv2.Revert(conv2.To(str)))
	}

}
Output:

-- Default converter --
IpAddress --To-> IPAddress --Revert-> IpAddress
defaultDnsServer --To-> defaultDNSServer --Revert-> defaultDnsServer
JsonFile --To-> JSONFile --Revert-> JsonFile
CsvFile --To-> CsvFile --Revert-> CsvFile

-- Custom converter --
IpAddress --To-> IpAddress --Revert-> IpAddress
defaultDnsServer --To-> defaultDnsServer --Revert-> defaultDnsServer
JsonFile --To-> JSONFile --Revert-> JsonFile
CsvFile --To-> CSVFile --Revert-> CsvFile

-- Custom converter (add to default) --
IpAddress --To-> IPAddress --Revert-> IpAddress
defaultDnsServer --To-> defaultDNSServer --Revert-> defaultDnsServer
JsonFile --To-> JSONFile --Revert-> JsonFile
CsvFile --To-> CSVFile --Revert-> CsvFile

Index

Examples

Constants

This section is empty.

Variables

View Source
var DefaultInitialisms = []string{
	"ACL", "API", "ASCII", "CPU", "CSS", "DNS", "EOF", "GUID", "HTML", "HTTP",
	"HTTPS", "JSON", "QPS", "RAM", "RPC", "SLA", "SMTP", "SQL", "SSH", "TCP",
	"TLS", "TTL", "UDP", "GID", "UUID", "URI", "URL", "UTF8", "VM", "XML",
	"XMPP", "XSRF", "XSS", "SIP", "RTP", "AMQP", "DB", "TS",

	"UID", "ID", "IP", "UI",
}

DefaultInitialisms is a list of default initialisms. This list is based on Staticcheck. For more details, please see initialisms section.

Functions

func Revert

func Revert(s string) string

Revert returns a string converted from Go case to normal case. Note that it is impossible to accurately determine the word break in a string of consecutive uppercase words, so the conversion maynot work as expected.

func To

func To(s string) string

To returns a string converted to Go case.

Types

type Converter added in v1.1.0

type Converter struct {
	// contains filtered or unexported fields
}

Converter represents a conversion object. If you need a conversion other than the default, you need create a new conversion object.

func New added in v1.1.0

func New(opts ...Option) (*Converter, error)

New creates a new Converter.

func (*Converter) Revert added in v1.1.0

func (c *Converter) Revert(s string) string

Revert returns a string converted from Go case to normal case with converter. Note that it is impossible to accurately determine the word break in a string of consecutive uppercase words, so the conversion maynot work as expected.

func (*Converter) To added in v1.1.0

func (c *Converter) To(s string) string

To returns a string converted to Go case with converter.

type Option added in v1.1.0

type Option interface {
	// contains filtered or unexported methods
}

Option is an option that configures Converter.

func WithInitialisms added in v1.1.0

func WithInitialisms(initialisms ...string) Option

WithInitialisms is an option to set initialisms. If you want to add to the default initialisms, use `DefaultInitialisms`.

Jump to

Keyboard shortcuts

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