Skip to content

Commit

Permalink
Fix default value bug
Browse files Browse the repository at this point in the history
  • Loading branch information
mudream4869 committed Oct 21, 2024
1 parent 2a49857 commit f8d106b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 16 deletions.
18 changes: 9 additions & 9 deletions toolgui/tgcomp/tcinput/datepicker.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ func Datepicker(s *tgframe.State, c *tgframe.Container, label string) *Date {
comp := newDatepickerComponent(label, "date")
c.AddComponent(comp)

dateStr := s.GetString(comp.ID, "")
if dateStr == "" {
dateStr := s.GetString(comp.ID)
if dateStr == nil {
return nil
}

date, err := time.Parse("2006-01-02", dateStr)
date, err := time.Parse("2006-01-02", *dateStr)
if err != nil {
panic(fmt.Sprintf("failed to parse date: %v", err))
}
Expand Down Expand Up @@ -86,12 +86,12 @@ func Timepicker(s *tgframe.State, c *tgframe.Container, label string) *Time {
comp := newDatepickerComponent(label, "time")
c.AddComponent(comp)

timeStr := s.GetString(comp.ID, "")
if timeStr == "" {
timeStr := s.GetString(comp.ID)
if timeStr == nil {
return nil
}

t, err := time.Parse("15:04", timeStr)
t, err := time.Parse("15:04", *timeStr)
if err != nil {
panic(fmt.Sprintf("failed to parse time: %v", err))
}
Expand All @@ -108,12 +108,12 @@ func Datetimepicker(s *tgframe.State, c *tgframe.Container, label string) *time.
comp := newDatepickerComponent(label, "datetime-local")
c.AddComponent(comp)

datetimeStr := s.GetString(comp.ID, "")
if datetimeStr == "" {
datetimeStr := s.GetString(comp.ID)
if datetimeStr == nil {
return nil
}

datetime, err := time.Parse("2006-01-02T15:04", datetimeStr)
datetime, err := time.Parse("2006-01-02T15:04", *datetimeStr)
if err != nil {
panic(fmt.Sprintf("failed to parse datetime: %v", err))
}
Expand Down
7 changes: 6 additions & 1 deletion toolgui/tgcomp/tcinput/textarea.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,10 @@ func TextareaWithConf(s *tgframe.State, c *tgframe.Container, label string, conf
}

c.AddComponent(comp)
return s.GetString(comp.ID, comp.Default)
val := s.GetString(comp.ID)
if val == nil {
return comp.Default
}

return *val
}
7 changes: 6 additions & 1 deletion toolgui/tgcomp/tcinput/textbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,10 @@ func TextboxWithConf(s *tgframe.State, c *tgframe.Container, label string, conf
}

c.AddComponent(comp)
return s.GetString(comp.ID, comp.Default)
val := s.GetString(comp.ID)
if val == nil {
return comp.Default
}

return *val
}
13 changes: 8 additions & 5 deletions toolgui/tgframe/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,17 @@ func (s *State) GetObject(key string, out any) error {
}

// GetString gets the value of a key and returns it as a string.
func (s *State) GetString(key string, defaultVal string) string {
func (s *State) GetString(key string) *string {
s.rwLock.RLock()
defer s.rwLock.RUnlock()

val, ok := s.values[key]
if !ok {
return defaultVal
if !ok || val == nil {
return nil
}
return val.(string)

ss := val.(string)
return &ss
}

// GetFloat gets the value of a key and returns it as a float64.
Expand All @@ -149,7 +151,7 @@ func (s *State) GetInt(key string) *int {
defer s.rwLock.RUnlock()

val, ok := s.values[key]
if !ok {
if !ok || val == nil {
return nil
}

Expand All @@ -166,6 +168,7 @@ func (s *State) GetBool(key string) bool {
if !ok {
return false
}

return val.(bool)
}

Expand Down

0 comments on commit f8d106b

Please sign in to comment.