src

Go monorepo.
git clone git://code.dwrz.net/src
Log | Files | Refs

gen_scalars.go (1617B)


      1 //go:build codegen
      2 // +build codegen
      3 
      4 package ptr
      5 
      6 import "strings"
      7 
      8 func GetScalars() Scalars {
      9 	return Scalars{
     10 		{Type: "bool"},
     11 		{Type: "byte"},
     12 		{Type: "string"},
     13 		{Type: "int"},
     14 		{Type: "int8"},
     15 		{Type: "int16"},
     16 		{Type: "int32"},
     17 		{Type: "int64"},
     18 		{Type: "uint"},
     19 		{Type: "uint8"},
     20 		{Type: "uint16"},
     21 		{Type: "uint32"},
     22 		{Type: "uint64"},
     23 		{Type: "float32"},
     24 		{Type: "float64"},
     25 		{Type: "Time", Import: &Import{Path: "time"}},
     26 		{Type: "Duration", Import: &Import{Path: "time"}},
     27 	}
     28 }
     29 
     30 // Import provides the import path and optional alias
     31 type Import struct {
     32 	Path  string
     33 	Alias string
     34 }
     35 
     36 // Package returns the Go package name for the import. Returns alias if set.
     37 func (i Import) Package() string {
     38 	if v := i.Alias; len(v) != 0 {
     39 		return v
     40 	}
     41 
     42 	if v := i.Path; len(v) != 0 {
     43 		parts := strings.Split(v, "/")
     44 		pkg := parts[len(parts)-1]
     45 		return pkg
     46 	}
     47 
     48 	return ""
     49 }
     50 
     51 // Scalar provides the definition of a type to generate pointer utilities for.
     52 type Scalar struct {
     53 	Type   string
     54 	Import *Import
     55 }
     56 
     57 // Name returns the exported function name for the type.
     58 func (t Scalar) Name() string {
     59 	return strings.Title(t.Type)
     60 }
     61 
     62 // Symbol returns the scalar's Go symbol with path if needed.
     63 func (t Scalar) Symbol() string {
     64 	if t.Import != nil {
     65 		return t.Import.Package() + "." + t.Type
     66 	}
     67 	return t.Type
     68 }
     69 
     70 // Scalars is a list of scalars.
     71 type Scalars []Scalar
     72 
     73 // Imports returns all imports for the scalars.
     74 func (ts Scalars) Imports() []*Import {
     75 	imports := []*Import{}
     76 	for _, t := range ts {
     77 		if v := t.Import; v != nil {
     78 			imports = append(imports, v)
     79 		}
     80 	}
     81 
     82 	return imports
     83 }