github.com/crossplane/upjet@v1.3.0/pkg/types/name/name.go (about)

     1  // SPDX-FileCopyrightText: 2023 The Crossplane Authors <https://crossplane.io>
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  
     5  package name
     6  
     7  import (
     8  	"strings"
     9  
    10  	"github.com/fatih/camelcase"
    11  	"github.com/iancoleman/strcase"
    12  )
    13  
    14  // NewFromSnake produces a Name, using given snake case string as source of
    15  // truth.
    16  func NewFromSnake(s string) Name {
    17  	originals := strings.Split(s, "_")
    18  	camels := make([]string, len(originals))
    19  	computedCamels := make([]string, len(originals))
    20  	for i, org := range originals {
    21  		computedCamels[i] = strcase.ToCamel(org)
    22  		if known, ok := lowerToCamelAcronyms[strings.ToLower(org)]; ok {
    23  			camels[i] = known
    24  			continue
    25  		}
    26  		camels[i] = computedCamels[i]
    27  	}
    28  	return Name{
    29  		Snake:              s,
    30  		Camel:              strings.Join(camels, ""),
    31  		CamelComputed:      strings.Join(computedCamels, ""),
    32  		LowerCamel:         strings.Join(append([]string{strings.ToLower(camels[0])}, camels[1:]...), ""),
    33  		LowerCamelComputed: strings.Join(append([]string{strings.ToLower(computedCamels[0])}, computedCamels[1:]...), ""),
    34  	}
    35  }
    36  
    37  // NewFromCamel produces a Name, using given camel case string as source of
    38  // truth.
    39  func NewFromCamel(s string) Name {
    40  	originals := camelcase.Split(s)
    41  	snakes := make([]string, len(originals))
    42  	for i, org := range originals {
    43  		snakes[i] = strings.ToLower(org)
    44  	}
    45  	return NewFromSnake(strings.Join(snakes, "_"))
    46  }
    47  
    48  // Name holds different variants of a name.
    49  type Name struct {
    50  	// Snake is the snake case version of the string: rds_instance
    51  	Snake string
    52  
    53  	// Camel is the camel case version of the string where known acronyms are
    54  	// are uppercase: RDSInstance
    55  	Camel string
    56  
    57  	// LowerCamel is the camel case version with the first word being lower case
    58  	// and the known acronyms are uppercase if they are not the first word: rdsInstance
    59  	LowerCamel string
    60  
    61  	// CamelComputed is the camel case version without any acronym changes: RdsInstance
    62  	CamelComputed string
    63  
    64  	// LowerCamelComputed is the camel case version without any acronym changes
    65  	// and the first word is lower case: rdsInstance
    66  	LowerCamelComputed string
    67  }
    68  
    69  // Add acronyms that can be safely assumed to be common for any kind of provider.
    70  // For provider-specific ones, like ARN for Amazon Web Services, provider
    71  // authors need to configure them in their provider repository.
    72  
    73  // NOTE(muvaf): We can have more maps like camel -> lower for reverse conversion,
    74  // but it's not necessary for now.
    75  
    76  var (
    77  	// Used to hold lower -> camel known exceptions.
    78  	lowerToCamelAcronyms = map[string]string{
    79  		"id": "ID",
    80  	}
    81  )
    82  
    83  // AddAcronym is used to add exception words that will be used to intervene
    84  // the conversion from lower case to camel case.
    85  // It is suggested that users of this package make all AddAcronym calls before
    86  // any usage (like init()) so that the conversions are consistent across the
    87  // board.
    88  func AddAcronym(lower, camel string) {
    89  	lowerToCamelAcronyms[lower] = camel
    90  }
    91  
    92  func init() {
    93  	// Written manually
    94  	AddAcronym("ipv6", "IPv6")
    95  	AddAcronym("ipv4", "IPv4")
    96  
    97  	// Taken from golangci-lint staticcheck
    98  	// https://github.com/dominikh/go-tools/blob/4049766cbbeee505b10996f03cd3f504aa238734/config/example.conf#L2
    99  	AddAcronym("acl", "ACL")
   100  	AddAcronym("adm", "ADM")
   101  	AddAcronym("ai", "AI")
   102  	AddAcronym("alb", "ALB")
   103  	AddAcronym("ami", "AMI")
   104  	AddAcronym("api", "API")
   105  	AddAcronym("apns", "APNS")
   106  	AddAcronym("ascii", "ASCII")
   107  	AddAcronym("bgp", "BGP")
   108  	AddAcronym("ca", "CA")
   109  	AddAcronym("cloudformation", "CloudFormation")
   110  	AddAcronym("cpu", "CPU")
   111  	AddAcronym("css", "CSS")
   112  	AddAcronym("dhcp", "DHCP")
   113  	AddAcronym("dicom", "DICOM")
   114  	AddAcronym("dkim", "DKIM")
   115  	AddAcronym("dns", "DNS")
   116  	AddAcronym("dnssec", "DNSSEC")
   117  	AddAcronym("ebs", "EBS")
   118  	AddAcronym("ec2", "EC2")
   119  	AddAcronym("efs", "EFS")
   120  	AddAcronym("eip", "EIP")
   121  	AddAcronym("elb", "ELB")
   122  	AddAcronym("eof", "EOF")
   123  	AddAcronym("fhir", "FHIR")
   124  	AddAcronym("fsx", "FSX")
   125  	AddAcronym("gcp", "GCP")
   126  	AddAcronym("gcm", "GCM")
   127  	AddAcronym("graphql", "GraphQL")
   128  	AddAcronym("grpc", "GRPC")
   129  	AddAcronym("guid", "GUID")
   130  	AddAcronym("haproxy", "HAProxy")
   131  	AddAcronym("hsm", "HSM")
   132  	AddAcronym("ipset", "IPSet")
   133  	AddAcronym("iscsi", "ISCSI")
   134  	AddAcronym("hl7", "HL7")
   135  	AddAcronym("html", "HTML")
   136  	AddAcronym("http", "HTTP")
   137  	AddAcronym("https", "HTTPS")
   138  	AddAcronym("hmac", "HMAC")
   139  	AddAcronym("iam", "IAM")
   140  	AddAcronym("id", "ID")
   141  	AddAcronym("ip", "IP")
   142  	AddAcronym("json", "JSON")
   143  	AddAcronym("kms", "KMS")
   144  	AddAcronym("lb", "LB")
   145  	AddAcronym("ml", "ML")
   146  	AddAcronym("mysql", "MySQL")
   147  	AddAcronym("nat", "NAT")
   148  	AddAcronym("nfs", "NFS")
   149  	AddAcronym("nodejs", "NodeJS")
   150  	AddAcronym("openid", "OpenID")
   151  	AddAcronym("php", "PHP")
   152  	AddAcronym("qps", "QPS")
   153  	AddAcronym("ram", "RAM")
   154  	AddAcronym("rds", "RDS")
   155  	AddAcronym("rpc", "RPC")
   156  	AddAcronym("saml", "SAML")
   157  	AddAcronym("sla", "SLA")
   158  	AddAcronym("slo", "SLO")
   159  	AddAcronym("smb", "SMB")
   160  	AddAcronym("sms", "SMS")
   161  	AddAcronym("smtp", "SMTP")
   162  	AddAcronym("sql", "SQL")
   163  	AddAcronym("ssh", "SSH")
   164  	AddAcronym("ssl", "SSL")
   165  	AddAcronym("tcp", "TCP")
   166  	AddAcronym("tls", "TLS")
   167  	AddAcronym("ttl", "TTL")
   168  	AddAcronym("udp", "UDP")
   169  	AddAcronym("ui", "UI")
   170  	AddAcronym("gid", "GID")
   171  	AddAcronym("uid", "UID")
   172  	AddAcronym("uuid", "UUID")
   173  	AddAcronym("uri", "URI")
   174  	AddAcronym("url", "URL")
   175  	AddAcronym("utf8", "UTF8")
   176  	AddAcronym("vm", "VM")
   177  	AddAcronym("voip", "VoIP")
   178  	AddAcronym("vpc", "VPC")
   179  	AddAcronym("vpn", "VPN")
   180  	AddAcronym("xml", "XML")
   181  	AddAcronym("xmpp", "XMPP")
   182  	AddAcronym("xsrf", "XSRF")
   183  	AddAcronym("xss", "XSS")
   184  	AddAcronym("sip", "SIP")
   185  	AddAcronym("rtp", "RTP")
   186  	AddAcronym("amqp", "AMQP")
   187  	AddAcronym("db", "DB")
   188  	AddAcronym("ts", "TS")
   189  
   190  }