Clean up
This commit is contained in:
48
decode.odin
48
decode.odin
@@ -1,16 +1,14 @@
|
||||
package mustache
|
||||
|
||||
import "base:runtime"
|
||||
import "core:mem"
|
||||
|
||||
import "core:reflect"
|
||||
import "core:strings"
|
||||
import "core:log"
|
||||
import "core:mem"
|
||||
import "core:fmt"
|
||||
|
||||
decode :: proc( v: any, key: string ) -> any {
|
||||
if key == "." do return v
|
||||
import "core:log"
|
||||
|
||||
decode :: proc( v: any, key: string ) -> any {
|
||||
v := reflect.any_base(v)
|
||||
ti := type_info_of(v.id)
|
||||
|
||||
@@ -33,7 +31,7 @@ decode :: proc( v: any, key: string ) -> any {
|
||||
newkey_0 := newkey[0]
|
||||
newkey_1 := "."
|
||||
|
||||
if len(newkey) == 2 {
|
||||
if len(newkey) != 2 {
|
||||
newkey_0 = newkey[0][:len(newkey[0])-1]
|
||||
newkey_1 = newkey[1]
|
||||
}
|
||||
@@ -79,32 +77,58 @@ decode :: proc( v: any, key: string ) -> any {
|
||||
return ""
|
||||
}
|
||||
|
||||
return ""
|
||||
if key == "." {
|
||||
return v
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
decode_string :: proc( v: any, key: string ) -> string {
|
||||
return fmt.tprintf("%v", decode(v, key))
|
||||
}
|
||||
|
||||
section :: proc( r: ^strings.Reader, v: any, key: string ) -> string {
|
||||
@private
|
||||
mustache_section :: proc( r: ^strings.Reader, v: any, key: string, inv: bool = false ) -> string {
|
||||
tmp := mustache(r, v, key)
|
||||
|
||||
if inv {
|
||||
return tmp
|
||||
}
|
||||
|
||||
defer delete(tmp)
|
||||
return ""
|
||||
}
|
||||
|
||||
section :: proc( r: ^strings.Reader, v: any, key: string, inv: bool = false ) -> string {
|
||||
save := r.i
|
||||
|
||||
t := reflect.any_base(decode(v, key))
|
||||
ti := type_info_of(t.id)
|
||||
|
||||
ret : string= ""
|
||||
#partial switch i in runtime.type_info_base(ti).variant {
|
||||
case runtime.Type_Info_Slice:
|
||||
ret : string= ""
|
||||
for i in 0..<reflect.length(t) {
|
||||
elem :=reflect.index(t, i);
|
||||
|
||||
strings.reader_seek(r, save, .Start)
|
||||
tmp := mustache(r, elem, key)
|
||||
|
||||
tmp := mustache_section(r, elem, key, !inv)
|
||||
defer delete(tmp)
|
||||
|
||||
ret = strings.concatenate({ret, tmp})
|
||||
}
|
||||
return ret
|
||||
case runtime.Type_Info_Boolean:
|
||||
b, _ := reflect.as_bool(t)
|
||||
|
||||
return mustache_section(r, t, key, b~inv)
|
||||
}
|
||||
|
||||
return ret
|
||||
if t == nil {
|
||||
return mustache_section(r, t, key, inv)
|
||||
}
|
||||
|
||||
return mustache_section(r, t, key, !inv)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user