-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
78 lines (69 loc) · 2.46 KB
/
Copy pathmain.go
File metadata and controls
78 lines (69 loc) · 2.46 KB
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
package main
import (
"fmt"
"log"
"os"
"time"
"github.com/bntrtm/structly/menu"
tea "github.com/charmbracelet/bubbletea"
)
// This example is provided to demonstrate the use of 'exceptions.'
// In the context of Structly, this term refers to how we can tell
// Structly which struct fields we don't want exposed to users.
//
// If you've not yet read through the 'introduction' example, it is
// best to start there. You can compare it with this example to
// get an understanding of where exception logic comes into play.
// STEP 1: Declare the struct whose fields you wish to expose to users for input.
// applicationForm holds fields typical of a job application.
type applicationForm struct {
FirstName string `smname:"First Name"`
LastName string `smname:"Last Name"`
Email string
PhoneNo int `smname:"Phone"`
Country string `smname:"Country"`
Location string `smname:"Location (City)"`
CanTravel bool `smname:"Travel" smdes:"Can you travel for work?"`
BlacklistedField string `bl:""` // field blacklisted at the type level
BlacklistMe int // field to be blacklisted with an exception list
}
func main() {
// Declare options here, if you please...
// STEP 2: Define your struct...
newApplication := applicationForm{}
// STEP 3: Initialize a menu!
//
// This is the step wherein you may specify exception lists for your model.
//
// NOTE: Black() and White() exist as convenience wrappers to satisfy
// validation logic for exceptions under the hood.
model, err := menu.NewMenu(&newApplication, menu.Black("BlacklistMe")...)
if err != nil {
log.Fatalf("Trouble generating the application: %s", err)
}
// STEP 4: Run the bubbletea program.
// Blacklisted fields will not show up!
p := tea.NewProgram(model)
if _, err := p.Run(); err != nil {
log.Fatalf("Trouble generating the application: %s", err)
} else {
if model.EndState.QuitWithCancel {
fmt.Printf("Canceled application.\n")
os.Exit(0)
} else {
err = model.ParseStruct(&newApplication)
if err != nil {
log.Fatalf("Trouble getting data from the application: %s", err)
}
// Your struct is now full of user-input values!
// Do what you need with it.
// newApplication: "Wow, I feel like a new struct!"
}
if newApplication.FirstName == "" {
log.Fatal("ERROR: Missing First Name field!")
}
fmt.Printf("Thank you for applying, %s!\n", newApplication.FirstName)
time.Sleep(time.Second * 3)
os.Exit(0)
}
}