Skip to content

Commit

Permalink
Properly serialize options value
Browse files Browse the repository at this point in the history
  • Loading branch information
pipe01 committed Jan 2, 2021
1 parent 62686a2 commit b75bc4d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 20 deletions.
7 changes: 4 additions & 3 deletions cmd/ui/front/src/components/Widget.vue
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
//- Few options - addons layout
.field.has-addons(v-if="widget.options.length < 5")
.control(v-for="(opt, i) in widget.options")
button.button(:class="{'is-info': i == widget.value}" @click="setValue(i)") {{opt}}
button.button(:class="{'is-info': i == value}" @click="setValue(i)") {{opt}}

//- Many options - flex layout
.many.is-flex.is-flex-wrap-wrap.is-justify-content-space-around(v-else)
Expand All @@ -53,15 +53,16 @@

<script lang="ts">
import axios from 'axios'
import { computed, defineComponent, inject, Ref, ref } from 'vue'
import { computed, defineComponent, inject, PropType, Ref, ref } from 'vue'
import { Widget } from '../types/widget'
import OnOff from "./OnOff.vue"
export default defineComponent({
components: {
OnOff
},
props: {
widget: Object,
widget: Object as PropType<Widget>,
path: String,
},
setup(props, { emit }) {
Expand Down
16 changes: 14 additions & 2 deletions cmd/ui/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func main() {
continue
}

value, err := w.UnmarshalValue(clvalue.Raw())
value, err := w.UnmarshalValue([]byte(clvalue.Raw()))
if err != nil {
//TODO Handle error
continue
Expand All @@ -61,13 +61,25 @@ func main() {
return
}

widget, ok := cfg.Widgets[path]
if !ok {
ctx.StatusCode(iris.StatusBadRequest)
return
}

var value interface{}
if err := json.Unmarshal(body, &value); err != nil {
ctx.StatusCode(iris.StatusBadRequest)
return
}

if err := hat.Set(string(body), client.SplitPath(path)...); err != nil {
mval, err := widget.MarshalValue(value)
if err != nil {
ctx.StatusCode(iris.StatusBadRequest)
return
}

if err := hat.Set(string(mval), client.SplitPath(path)...); err != nil {
ctx.StatusCode(iris.StatusInternalServerError)
log.Printf("failed to set value: %s", err)
}
Expand Down
35 changes: 20 additions & 15 deletions cmd/ui/widget/widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,29 +128,33 @@ func (w *Widget) isValid() (reason error) {
return nil
}

func (w *Widget) UnmarshalValue(str string) (value interface{}, err error) {
b := []byte(str)

func (w *Widget) UnmarshalValue(jsonval []byte) (value interface{}, err error) {
switch w.Type {
case WidgetOnOff:
var on bool
err = json.Unmarshal(b, &on)
err = json.Unmarshal(jsonval, &on)
value = on

case WidgetText:
var str string
err = json.Unmarshal(b, &str)
err = json.Unmarshal(jsonval, &str)
value = str

case WidgetOptions:
if w.StoreIndex {
var idx int
err = json.Unmarshal(b, &idx)
err = json.Unmarshal(jsonval, &idx)
value = idx
} else {
var sel string
err = json.Unmarshal(b, &sel)
value = sel
err = json.Unmarshal(jsonval, &sel)

for i, opt := range w.Options {
if opt == sel {
value = i
break
}
}
}

default:
Expand All @@ -173,14 +177,15 @@ func (w *Widget) MarshalValue(val interface{}) ([]byte, error) {
}

case WidgetOptions:
if w.StoreIndex {
if v, ok := val.(float64); ok {
return json.Marshal(math.Floor(v))
}
} else {
if v, ok := val.(string); ok {
return json.Marshal(v)
var idx int32
if v, ok := val.(float64); ok {
idx = int32(math.Floor(v))

if w.StoreIndex {
return json.Marshal(idx)
}

return json.Marshal(w.Options[idx])
}

default:
Expand Down

0 comments on commit b75bc4d

Please sign in to comment.