Pass section
This commit is contained in:
73
decode.odin
73
decode.odin
@@ -4,21 +4,48 @@ import "base:runtime"
|
||||
import "core:reflect"
|
||||
import "core:strings"
|
||||
import "core:mem"
|
||||
import "core:fmt"
|
||||
|
||||
import "core:log"
|
||||
|
||||
decode :: proc( v: any, key: string ) -> any {
|
||||
v := reflect.any_base(v)
|
||||
ti := type_info_of(v.id)
|
||||
@private
|
||||
_clean_html :: proc(s: string) -> string {
|
||||
ret: strings.Builder
|
||||
|
||||
#partial switch i in runtime.type_info_base(ti).variant {
|
||||
for c in s do switch c {
|
||||
case '<':
|
||||
strings.write_string(&ret, "<")
|
||||
case '>':
|
||||
strings.write_string(&ret, ">")
|
||||
case '&':
|
||||
strings.write_string(&ret, "&")
|
||||
case '"':
|
||||
strings.write_string(&ret, """)
|
||||
case '\'':
|
||||
strings.write_string(&ret, "'")
|
||||
case:
|
||||
strings.write_rune(&ret, c)
|
||||
}
|
||||
|
||||
return strings.to_string(ret)
|
||||
}
|
||||
|
||||
decode :: proc( v: any, key: string ) -> any {
|
||||
ti := type_info_of(v.id)
|
||||
variant := runtime.type_info_base(ti).variant
|
||||
|
||||
#partial switch i in variant {
|
||||
case runtime.Type_Info_Enumerated_Array:
|
||||
return decode(any{v.data, ti.id}, key)
|
||||
|
||||
case runtime.Type_Info_Union:
|
||||
return decode(any{v.data, reflect.union_variant_typeid(v) }, key)
|
||||
}
|
||||
|
||||
if key == "." {
|
||||
return v
|
||||
}
|
||||
|
||||
#partial switch i in variant {
|
||||
case runtime.Type_Info_Struct:
|
||||
newkey, err := strings.split_after_n(key, ".", 2)
|
||||
|
||||
@@ -74,36 +101,31 @@ decode :: proc( v: any, key: string ) -> any {
|
||||
}
|
||||
}
|
||||
|
||||
return ""
|
||||
return nil
|
||||
}
|
||||
|
||||
if key == "." {
|
||||
return v
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
decode_string :: proc( v: any, key: string ) -> string {
|
||||
return fmt.tprintf("%v", decode(v, key))
|
||||
return nil
|
||||
}
|
||||
|
||||
@private
|
||||
mustache_section :: proc( r: ^strings.Reader, v: any, key: string, inv: bool = false ) -> string {
|
||||
tmp := mustache(r, v, key)
|
||||
mustache_section :: proc( r: ^strings.Reader, v, p: any, section_key: string, inv: bool = false ) -> string {
|
||||
tmp := mustache(r, v, section_key)
|
||||
defer delete(tmp)
|
||||
|
||||
tmp2 := mustache(tmp, p, section_key)
|
||||
|
||||
if inv {
|
||||
return tmp
|
||||
return tmp2
|
||||
}
|
||||
|
||||
defer delete(tmp)
|
||||
delete(tmp2)
|
||||
return ""
|
||||
}
|
||||
|
||||
section :: proc( r: ^strings.Reader, v: any, key: string, inv: bool = false ) -> string {
|
||||
section :: proc( r: ^strings.Reader, v: any, section_key: string, inv: bool = false ) -> string {
|
||||
save := r.i
|
||||
|
||||
t := reflect.any_base(decode(v, key))
|
||||
t := reflect.any_base(decode(v, section_key))
|
||||
ti := type_info_of(t.id)
|
||||
|
||||
#partial switch i in runtime.type_info_base(ti).variant {
|
||||
@@ -113,7 +135,7 @@ section :: proc( r: ^strings.Reader, v: any, key: string, inv: bool = false ) ->
|
||||
elem :=reflect.index(t, i);
|
||||
strings.reader_seek(r, save, .Start)
|
||||
|
||||
tmp := mustache_section(r, elem, key, !inv)
|
||||
tmp := mustache_section(r, elem, v, section_key, !inv)
|
||||
defer delete(tmp)
|
||||
|
||||
ret = strings.concatenate({ret, tmp})
|
||||
@@ -122,13 +144,12 @@ section :: proc( r: ^strings.Reader, v: any, key: string, inv: bool = false ) ->
|
||||
case runtime.Type_Info_Boolean:
|
||||
b, _ := reflect.as_bool(t)
|
||||
|
||||
return mustache_section(r, t, key, b~inv)
|
||||
return mustache_section(r, t, v, section_key, b~inv)
|
||||
}
|
||||
|
||||
if t == nil {
|
||||
return mustache_section(r, t, key, inv)
|
||||
return mustache_section(r, t, v, section_key, inv)
|
||||
}
|
||||
|
||||
return mustache_section(r, t, key, !inv)
|
||||
return mustache_section(r, t, v, section_key, !inv)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user