summaryrefslogtreecommitdiff
path: root/bos-meta-attribute.sh
blob: 954ea8dbd18f053dd2908601208e18c2c093a2f2 (plain)
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!bash 
 
# this must be callable without a `self`, see 
# bos/meta/class/get-attribute-by-name-into 
function bos/meta/attribute/store-for-into() {
    local bos_meta_attribute_class bos_meta_attribute_objid
    bos-object/unpack-self-into bos_meta_attribute_class bos_meta_attribute_objid "$2"
 
    # meta-look! we can't depend on bos-dispatch/invoke or 
    # bos/meta/class/invoke to set these up 
 
    # the `if` is there because bos-meta-bootstrap.sh sets these manually 
    if [[ -z "$attribute_name" ]]; then
        local bos_meta_attribute_get_name; $self get-attribute-name-into bos_meta_attribute_get_name
        local bos_meta_attribute_get_type; $self get-attribute-type-into bos_meta_attribute_get_type
        local bos_meta_attribute_get_class; $self get-associated-class-into bos_meta_attribute_get_class
 
        local attribute_name="$bos_meta_attribute_get_name"
        local attribute_type="$bos_meta_attribute_get_type"
        local associated_class="$bos_meta_attribute_get_class"
    fi
 
    "bos-namespaces/store-${attribute_type}-for-into" "$1" attribute "$attribute_name" "$associated_class" "$bos_meta_attribute_class" "$bos_meta_attribute_objid"
}
 
function bos/meta/attribute/get-attribute-name-into() {
        local attribute_type='scalar'
        local associated_class='bos/meta/attribute'
 
        local bos_meta_attribute_name_store
        local attribute_name='attribute_name'
        bos/meta/attribute/store-for-into bos_meta_attribute_name_store "$self"
 
        local -n bos_meta_attribute_name="$bos_meta_attribute_name_store"
 
        bos/log 5 "get-attribute-name-into($1)<-$bos_meta_attribute_name_store"
        local -n bos_meta_attribute_name_dest="$1"
        bos_meta_attribute_name_dest="$bos_meta_attribute_name"
}
 
function bos/meta/attribute/get-attribute-type-into() {
        local attribute_type='scalar'
        local associated_class='bos/meta/attribute'
 
        local bos_meta_attribute_type_store
        local attribute_name='attribute_type'
        bos/meta/attribute/store-for-into bos_meta_attribute_type_store "$self"
 
        local -n bos_meta_attribute_type="$bos_meta_attribute_type_store"
 
        local -n bos_meta_attribute_type_dest="$1"
        bos_meta_attribute_type_dest="$bos_meta_attribute_type"
}
 
function bos/meta/attribute/get-associated-class-into() {
        local attribute_type='scalar'
        local associated_class='bos/meta/attribute'
 
        local bos_meta_attribute_class_store
        local attribute_name='associated_class'
        bos/meta/attribute/store-for-into bos_meta_attribute_class_store "$self"
 
        local -n bos_meta_attribute_class="$bos_meta_attribute_class_store"
 
        local -n bos_meta_attribute_class_dest="$1"
        bos_meta_attribute_class_dest="$bos_meta_attribute_class"
}
 
function bos/meta/attribute/set-value() {
    # the `if` is there because bos-meta-bootstrap.sh sets these manually 
    if [[ -z "$attribute_name" ]]; then
        local bos_meta_attribute_set_name; $self get-attribute-name-into bos_meta_attribute_set_name
        local bos_meta_attribute_set_type; $self get-attribute-type-into bos_meta_attribute_set_type
        local bos_meta_attribute_set_class; $self get-associated-class-into bos_meta_attribute_set_class
 
        local attribute_name="$bos_meta_attribute_set_name"
        local attribute_type="$bos_meta_attribute_set_type"
        local associated_class="$bos_meta_attribute_set_class"
    fi
 
    local bos_meta_attribute_store_name; $self store-for-into bos_meta_attribute_store_name "$1"
    shift
    local -n bos_meta_attribute_store="$bos_meta_attribute_store_name"
 
    case "$attribute_type" in
        scalar) bos_meta_attribute_store="$1" ;;
        array) bos_meta_attribute_store=( "$@" ) ;;
        assoc) eval "bos_meta_attribute_store=( ${@@Q} )" ;;
    esac
 
    bos/log 5 "set-value() $(declare -p "$bos_meta_attribute_store_name")"
    
    return 0
}
 
function bos/meta/attribute/get-value-into() {
    local bos_meta_attribute_result_name="$1"
    local -n bos_meta_attribute_result="$1"; shift;
 
    # meta-look! we can't depend on bos-dispatch/invoke or 
    # bos/meta/class/invoke to set these up 
 
    local bos_meta_attribute_get_name; $self get-attribute-name-into bos_meta_attribute_get_name
    local bos_meta_attribute_get_type; $self get-attribute-type-into bos_meta_attribute_get_type
    local bos_meta_attribute_get_class; $self get-associated-class-into bos_meta_attribute_get_class
 
    local attribute_name="$bos_meta_attribute_get_name"
    local attribute_type="$bos_meta_attribute_get_type"
    local associated_class="$bos_meta_attribute_get_class"
 
    bos/log 5 "getting ($self) ($attribute_name) ($attribute_type) ($associated_class) ($1)"
 
    local bos_meta_attribute_store_name; $self store-for-into bos_meta_attribute_store_name "$1"
    local -n bos_meta_attribute_store="$bos_meta_attribute_store_name"
 
    local bos_meta_attribute_value_expr
 
    case "$attribute_type" in
        scalar) bos_meta_attribute_result="$bos_meta_attribute_store" ;;
        array) bos_meta_attribute_result=( "${bos_meta_attribute_store[@]}" ) ;;
        assoc)
            bos_meta_attribute_value_expr="$(declare -p "$bos_meta_attribute_store_name")"
            # return if there's no value, otherwise the `eval` would fail 
            [[ "$bos_meta_attribute_value_expr" =~ = ]] || return 0
            eval "bos_meta_attribute_result=${bos_meta_attribute_value_expr#*=}"
            ;;
    esac
 
    bos/log 5 "$(declare -p "$bos_meta_attribute_result_name")"
    
    return 0
}