mirror of
https://github.com/dolthub/dolt.git
synced 2026-02-11 10:33:08 -06:00
Codegen for enum types
This generates a flow type for enums:
```noms
enum Handedness {
right
left
switch
}
```
Generates
```js
type E =
0 | // right
1 | // left
2; // switch
```
Issue #1081
This commit is contained in:
@@ -10,6 +10,7 @@ package code
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
"unicode"
|
||||
|
||||
@@ -447,6 +448,11 @@ func (gen Generator) ToTypeJS(t types.Type, fileID, nomsName string, indent int)
|
||||
panic("Unreachable")
|
||||
}
|
||||
|
||||
// IsLast determines if |index| is the last index in |seq|.
|
||||
func (gen Generator) IsLast(index int, seq interface{}) bool {
|
||||
return reflect.ValueOf(seq).Len() == index+1
|
||||
}
|
||||
|
||||
// ToTag replaces "-" characters in s with "_", so it can be used in a Go identifier.
|
||||
// TODO: replace other illegal chars as well?
|
||||
func ToTag(r ref.Ref) string {
|
||||
|
||||
@@ -280,6 +280,7 @@ func (gen *codeGen) readTemplates() *template.Template {
|
||||
"valueToDef": gen.generator.ValueToDef,
|
||||
"valueToUser": gen.generator.ValueToUser,
|
||||
"valueZero": gen.generator.ValueZero,
|
||||
"isLast": gen.generator.IsLast,
|
||||
}).ParseGlob(glob))
|
||||
}
|
||||
|
||||
|
||||
@@ -1 +1,3 @@
|
||||
// enum.tmpl
|
||||
{{$name := .Name}}
|
||||
export type {{.Name}} ={{range $index, $id := .Ids}}
|
||||
{{$index}}{{if isLast $index $.Ids | not}} |{{else}};{{end}} // {{$id}}{{end}}
|
||||
|
||||
@@ -20,7 +20,11 @@ import * as _noms from '@attic/noms';
|
||||
_noms.registerPackage(pkg);
|
||||
}
|
||||
|
||||
// enum.tmpl
|
||||
|
||||
export type Handedness =
|
||||
0 | // right
|
||||
1 | // left
|
||||
2; // switch
|
||||
|
||||
export interface EnumStruct extends _noms.Struct {
|
||||
hand: Handedness; // readonly
|
||||
|
||||
@@ -28,4 +28,8 @@ export interface S extends _noms.Struct {
|
||||
b: boolean; // readonly
|
||||
setB(value: boolean): S;
|
||||
}
|
||||
// enum.tmpl
|
||||
|
||||
export type E =
|
||||
0 | // e1
|
||||
1 | // e2
|
||||
2; // e3
|
||||
|
||||
@@ -22,7 +22,10 @@ import * as _noms from '@attic/noms';
|
||||
_noms.registerPackage(pkg);
|
||||
}
|
||||
|
||||
// enum.tmpl
|
||||
|
||||
export type LocalE =
|
||||
0 | // LocalE1
|
||||
1; // Ignored
|
||||
|
||||
export interface ImportUser extends _noms.Struct {
|
||||
importedStruct: D; // readonly
|
||||
|
||||
@@ -28,4 +28,8 @@ export interface S extends _noms.Struct {
|
||||
b: boolean; // readonly
|
||||
setB(value: boolean): S;
|
||||
}
|
||||
// enum.tmpl
|
||||
|
||||
export type E =
|
||||
0 | // e1
|
||||
1 | // e2
|
||||
2; // e3
|
||||
|
||||
Reference in New Issue
Block a user