File: gen_scalars.go

package info (click to toggle)
golang-github-aws-smithy-go 1.19.0-1~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 2,680 kB
  • sloc: java: 15,917; xml: 166; sh: 131; makefile: 66
file content (83 lines) | stat: -rw-r--r-- 1,617 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//go:build codegen
// +build codegen

package ptr

import "strings"

func GetScalars() Scalars {
	return Scalars{
		{Type: "bool"},
		{Type: "byte"},
		{Type: "string"},
		{Type: "int"},
		{Type: "int8"},
		{Type: "int16"},
		{Type: "int32"},
		{Type: "int64"},
		{Type: "uint"},
		{Type: "uint8"},
		{Type: "uint16"},
		{Type: "uint32"},
		{Type: "uint64"},
		{Type: "float32"},
		{Type: "float64"},
		{Type: "Time", Import: &Import{Path: "time"}},
		{Type: "Duration", Import: &Import{Path: "time"}},
	}
}

// Import provides the import path and optional alias
type Import struct {
	Path  string
	Alias string
}

// Package returns the Go package name for the import. Returns alias if set.
func (i Import) Package() string {
	if v := i.Alias; len(v) != 0 {
		return v
	}

	if v := i.Path; len(v) != 0 {
		parts := strings.Split(v, "/")
		pkg := parts[len(parts)-1]
		return pkg
	}

	return ""
}

// Scalar provides the definition of a type to generate pointer utilities for.
type Scalar struct {
	Type   string
	Import *Import
}

// Name returns the exported function name for the type.
func (t Scalar) Name() string {
	return strings.Title(t.Type)
}

// Symbol returns the scalar's Go symbol with path if needed.
func (t Scalar) Symbol() string {
	if t.Import != nil {
		return t.Import.Package() + "." + t.Type
	}
	return t.Type
}

// Scalars is a list of scalars.
type Scalars []Scalar

// Imports returns all imports for the scalars.
func (ts Scalars) Imports() []*Import {
	imports := []*Import{}
	for _, t := range ts {
		if v := t.Import; v != nil {
			imports = append(imports, v)
		}
	}

	return imports
}