github.com/yaegashi/msgraph.go@v0.1.4/gen/generator_types.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  )
     7  
     8  const (
     9  	nsPrefix  = "microsoft.graph."
    10  	colPrefix = "Collection("
    11  )
    12  
    13  var reservedTypeTable = map[string]string{
    14  	"Edm.Boolean":          "bool",
    15  	"Edm.Byte":             "byte",
    16  	"Edm.Int16":            "int",
    17  	"Edm.Int32":            "int",
    18  	"Edm.Int64":            "int",
    19  	"Edm.Decimal":          "int",
    20  	"Edm.Single":           "float64",
    21  	"Edm.Double":           "float64",
    22  	"Edm.Binary":           "Binary",
    23  	"Edm.Stream":           "Stream",
    24  	"Edm.Guid":             "UUID",
    25  	"Edm.String":           "string",
    26  	"Edm.DateTimeOffset":   "time.Time",
    27  	"Edm.Duration":         "Duration",
    28  	"Edm.TimeOfDay":        "TimeOfDay",
    29  	"Edm.Date":             "Date",
    30  	"microsoft.graph.Json": "json.RawMessage",
    31  }
    32  
    33  func ptrType(t string) string {
    34  	switch t {
    35  	case "json.RawMessage":
    36  		return t
    37  	}
    38  	return "*" + t
    39  }
    40  
    41  func stripNSPrefix(t string) (string, bool) {
    42  	ok := strings.HasPrefix(t, nsPrefix)
    43  	if ok {
    44  		t = t[len(nsPrefix):]
    45  	}
    46  	return t, ok
    47  }
    48  
    49  func exported(n string) string {
    50  	return lintName(strings.Title(n))
    51  }
    52  
    53  func isCollectionType(t string) bool {
    54  	return strings.HasPrefix(t, colPrefix)
    55  }
    56  
    57  func (g *Generator) SymBaseType(t string) string {
    58  	if x, ok := g.SymTypeMap[t]; ok {
    59  		return x
    60  	}
    61  	if x, ok := stripNSPrefix(t); ok {
    62  		return exported(x)
    63  	}
    64  	if strings.HasPrefix(t, colPrefix) {
    65  		return g.SymBaseType(t[len(colPrefix) : len(t)-1])
    66  	}
    67  	panic(fmt.Errorf("Unknown type %s", t))
    68  }
    69  
    70  func (g *Generator) SymFromType(t string) string {
    71  	if x, ok := g.SymTypeMap[t]; ok {
    72  		return x
    73  	}
    74  	if x, ok := stripNSPrefix(t); ok {
    75  		return exported(x)
    76  	}
    77  	if strings.HasPrefix(t, colPrefix) {
    78  		return g.SymBaseType(t[len(colPrefix):len(t)-1]) + "Collection"
    79  	}
    80  	panic(fmt.Errorf("Unknown type %s", t))
    81  }
    82  
    83  func (g *Generator) TypeFromType(t string) string {
    84  	if x, ok := reservedTypeTable[t]; ok {
    85  		return ptrType(x)
    86  	}
    87  	if x, ok := g.SymTypeMap[t]; ok {
    88  		return ptrType(x)
    89  	}
    90  	if x, ok := stripNSPrefix(t); ok {
    91  		return ptrType(exported(x))
    92  	}
    93  	if strings.HasPrefix(t, colPrefix) {
    94  		return "[]" + g.TypeFromType(t[len(colPrefix) : len(t)-1])[1:]
    95  	}
    96  	panic(fmt.Errorf("Unknown type %s", t))
    97  }