Skip to content

Commit f54ff3d

Browse files
committed
update MetaKey
1 parent 76df16c commit f54ff3d

File tree

11 files changed

+595
-456
lines changed

11 files changed

+595
-456
lines changed

common/enum/threeflag/threeflag.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
package threeflag
22

3-
// ThreeFlag implements a 3-value flag: "undefined", "true", "false"
4-
// Uses slugs from: https://threedots.tech/post/safer-enums-in-go/
5-
type ThreeFlag struct {
6-
slug string
7-
}
3+
import "fmt"
84

9-
func (i ThreeFlag) String() string {
10-
return i.slug
11-
}
5+
// ThreeFlag implements a 3-value flag: "undefined", "true", "false"
6+
type ThreeFlag int
127

13-
var (
14-
Undefined = ThreeFlag{"undefined"}
15-
False = ThreeFlag{"false"}
16-
True = ThreeFlag{"true"}
8+
const (
9+
Undefined ThreeFlag = 0
10+
False ThreeFlag = -1
11+
True ThreeFlag = 1
1712
)
13+
14+
func (tf ThreeFlag) String() string {
15+
return fmt.Sprintf("%d", tf)
16+
}

common/types/schema.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ func NewSchema(nativeDialect string) *Schema {
1717
pool := NewNodePool()
1818

1919
schema := &Schema{
20-
RootID: pool.NewRootNode("RootID", nativeDialect).ID,
21-
TypeRefID: pool.NewRootNode("TypeRefID", nativeDialect).ID,
20+
RootID: pool.NewRootNode("Root", nativeDialect).ID,
21+
TypeRefID: pool.NewRootNode("TypeRef", nativeDialect).ID,
2222

2323
NodePool: pool,
2424
}

common/types/typenode.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,15 @@ type TypeNode struct {
9191
NativeDialect string `json:",omitempty"`
9292

9393
// Native type features by dialect name.
94-
Native map[string]*NativeType `json:"-"`
94+
Native map[string]*NativeType
9595

9696
// Capture error if element cannot reflect.
9797
Error string `json:",omitempty"`
9898

99+
// MetaKey is a tag attached to a top-level node during schema derivation.
100+
// This can be used to attach additional metadata during rendering.
101+
MetaKey string `json:",omitempty"`
102+
99103
// Pointers to Parent and Child ID strings.
100104
Parent string `json:",omitempty"`
101105
Children []string `json:",omitempty"`
@@ -224,6 +228,7 @@ func (t *TypeNode) Copy() *TypeNode {
224228
n.TypeCategory = t.TypeCategory
225229
n.TypeRef = t.TypeRef
226230
n.Error = t.Error
231+
n.MetaKey = t.MetaKey
227232

228233
// Copy Children with new element as parent.
229234
for _, childID := range t.Children {
@@ -546,13 +551,13 @@ type NativeType struct {
546551
Dialect string
547552

548553
// Name of element if different from generic Name.
549-
Name string
554+
Name string `json:",omitempty"`
550555

551556
// Native type of element if different from the generic Type.
552-
Type string
557+
Type string `json:",omitempty"`
553558

554559
// TypeRef holds the native name of a type if different from the generic TypeRef.
555-
TypeRef string
560+
TypeRef string `json:",omitempty"`
556561

557562
// Include indicates whether an element should be included in output for a dialect.
558563
// Include has three value values:
@@ -568,7 +573,7 @@ type NativeType struct {
568573
Options NativeOption
569574

570575
// Capture error if element cannot reflect.
571-
Error string
576+
Error string `json:",omitempty"`
572577
}
573578

574579
// NewNativeType initializes a new NativeType with default settings.

common/util/util.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,10 @@ func Capitalize(s string) string {
100100
}
101101

102102
// AppendStrings adds non-empty strings from in to out and returns a new slice.
103-
func AppendStrings(out []string, in []string) []string {
103+
func AppendStrings(out []string, in []string, prefix string) []string {
104104
for _, s := range in {
105105
if s != "" {
106-
out = append(out, s)
106+
out = append(out, prefix+s)
107107
}
108108
}
109109
return out

main.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,25 @@ import (
99
// Hello, World for b9schema.
1010
type HelloStruct struct {
1111
Hello string
12-
World float64
1312
}
1413

15-
func main() {
16-
var h *HelloStruct
14+
type GoodbyeStruct struct {
15+
Bye float64
16+
}
17+
18+
type MorningStruct struct {
19+
Morning HelloStruct
20+
}
1721

22+
func main() {
1823
// Derive schema.
1924
r := reflector.NewReflector()
20-
schema := r.DeriveSchema(h)
25+
26+
r.DeriveSchema(HelloStruct{}, "/path/to/hello")
27+
r.DeriveSchema(GoodbyeStruct{}, "/path/to/goodbye")
28+
r.DeriveSchema(MorningStruct{}, "/path/to/morning")
29+
30+
schema := r.Schema
2131

2232
// Print schema as JSON.
2333
if b, err := json.MarshalIndent(schema, "", " "); err != nil {

0 commit comments

Comments
 (0)