• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

leeonky / test-charm-java / 212

14 Apr 2025 05:15AM UTC coverage: 74.088% (-0.1%) from 74.209%
212

push

circleci

leeonky
Update version

7957 of 10740 relevant lines covered (74.09%)

0.74 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.55
/DAL-java/src/main/java/com/github/leeonky/dal/runtime/Data.java
1
package com.github.leeonky.dal.runtime;
2

3
import com.github.leeonky.dal.runtime.RuntimeContextBuilder.DALRuntimeContext;
4
import com.github.leeonky.dal.runtime.inspector.DumpingBuffer;
5
import com.github.leeonky.interpreter.InterpreterException;
6
import com.github.leeonky.util.BeanClass;
7
import com.github.leeonky.util.ConvertException;
8
import com.github.leeonky.util.ThrowingSupplier;
9

10
import java.util.Comparator;
11
import java.util.List;
12
import java.util.Optional;
13
import java.util.Set;
14
import java.util.function.Function;
15
import java.util.function.Supplier;
16

17
import static com.github.leeonky.dal.ast.node.SortGroupNode.NOP_COMPARATOR;
18
import static com.github.leeonky.dal.runtime.DalException.buildUserRuntimeException;
19
import static com.github.leeonky.dal.runtime.ExpressionException.illegalOperation;
20
import static com.github.leeonky.util.Sneaky.sneakyThrow;
21
import static java.lang.String.format;
22
import static java.util.stream.Collectors.toList;
23

24
public class Data {
25
    private final SchemaType schemaType;
26
    private final DALRuntimeContext context;
27
    private final Object value;
28
    private DataList list;
29

30
    public Data(Object value, DALRuntimeContext context, SchemaType schemaType) {
1✔
31
        this.context = context;
1✔
32
        this.schemaType = schemaType;
1✔
33
        this.value = value;
1✔
34
    }
1✔
35

36
    public Object instance() {
37
        return value;
1✔
38
    }
39

40
    public Set<?> fieldNames() {
41
        return context.findPropertyReaderNames(instance());
1✔
42
    }
43

44
    public boolean isList() {
45
        Object instance = instance();
1✔
46
        return context.isRegisteredList(instance) || (instance != null && instance.getClass().isArray());
1✔
47
    }
48

49
    public DataList list() {
50
        if (list == null) {
1✔
51
            if (!isList())
1✔
52
                throw new DalRuntimeException(format("Invalid input value, expect a List but: %s", dump().trim()));
1✔
53
            list = new DataList(context.createCollection(instance()));
1✔
54
        }
55
        return list;
1✔
56
    }
57

58
    public boolean isNull() {
59
        return context.isNull(instance());
1✔
60
    }
61

62
    public Data getValue(List<Object> propertyChain) {
63
        return propertyChain.isEmpty() ? this :
1✔
64
                getValue(propertyChain.get(0)).getValue(propertyChain.subList(1, propertyChain.size()));
1✔
65
    }
66

67
    public Data getValue(Object propertyChain) {
68
        List<Object> chain = schemaType.access(propertyChain).getPropertyChainBefore(schemaType);
1✔
69
        if (chain.size() == 1 && chain.get(0).equals(propertyChain)) {
1✔
70
            try {
71
                Object value = isList() && !(propertyChain instanceof String) ? list().getByIndex((int) propertyChain)
1✔
72
                        : context.getPropertyValue(this, propertyChain);
1✔
73
                return new Data(value, context, propertySchema(propertyChain,
1✔
74
                        this.value instanceof AutoMappingList && propertyChain instanceof String));
75
            } catch (IndexOutOfBoundsException ex) {
1✔
76
                throw new DalRuntimeException(ex.getMessage());
1✔
77
            } catch (ListMappingElementAccessException | ExpressionException | InterpreterException ex) {
1✔
78
                throw ex;
1✔
79
            } catch (Throwable e) {
1✔
80
                throw new DalRuntimeException(format("Get property `%s` failed, property can be:\n" +
1✔
81
                        "  1. public field\n" +
82
                        "  2. public getter\n" +
83
                        "  3. public method\n" +
84
                        "  4. Map key value\n" +
85
                        "  5. customized type getter\n" +
86
                        "  6. static method extension", propertyChain), e);
87
            }
88
        }
89
        return getValue(chain);
1✔
90
    }
91

92
    public SchemaType propertySchema(Object property, boolean isListMapping) {
93
        return isListMapping ? schemaType.mappingAccess(property) : schemaType.access(property);
1✔
94
    }
95

96
    public Object firstFieldFromAlias(Object alias) {
97
        return schemaType.firstFieldFromAlias(alias);
1✔
98
    }
99

100
    public Data convert(Class<?>... targets) {
101
        return map(object -> {
1✔
102
            ConvertException e = null;
1✔
103
            for (Class<?> target : targets) {
1✔
104
                try {
105
                    return context.getConverter().convert(target, object);
1✔
106
                } catch (ConvertException convertException) {
1✔
107
                    e = convertException;
1✔
108
                }
109
            }
110
            throw e;
1✔
111
        });
112
    }
113

114
    public Data map(Function<Object, Object> mapper) {
115
        return new Data(mapper.apply(instance()), context, schemaType);
1✔
116
    }
117

118
    public Data filter(String prefix) {
119
        FilteredObject filteredObject = new FilteredObject();
1✔
120
        fieldNames().stream().filter(String.class::isInstance).map(String.class::cast)
1✔
121
                .filter(field -> field.startsWith(prefix)).forEach(fieldName ->
1✔
122
                        filteredObject.put(fieldName.substring(prefix.length()), getValue(fieldName).instance()));
1✔
123
        return new Data(filteredObject, context, schemaType);
1✔
124
    }
125

126
    public String dump() {
127
        return DumpingBuffer.rootContext(context).dump(this).content();
1✔
128
    }
129

130
    public String dumpValue() {
131
        return DumpingBuffer.rootContext(context).dumpValue(this).content();
1✔
132
    }
133

134
    public <T> T execute(Supplier<T> supplier) {
135
        return context.pushAndExecute(this, supplier);
1✔
136
    }
137

138
    public <T> Optional<T> cast(Class<T> type) {
139
        return BeanClass.cast(instance(), type);
1✔
140
    }
141

142
    public boolean instanceOf(Class<?> type) {
143
        try {
144
            return type.isInstance(instance());
1✔
145
        } catch (Throwable ignore) {
1✔
146
            return false;
1✔
147
        }
148
    }
149

150
    public static Data lazy(ThrowingSupplier<?> supplier, DALRuntimeContext context, SchemaType schemaType) {
151
        try {
152
            return new Data(supplier.get(), context, schemaType);
1✔
153
        } catch (Throwable e) {
1✔
154
            return new Data(null, context, schemaType) {
1✔
155
                @Override
156
                public Object instance() {
157
                    return sneakyThrow(buildUserRuntimeException(e));
×
158
                }
159
            };
160
        }
161
    }
162

163
    public class DataList extends DALCollection.Decorated<Object> {
164
        public DataList(DALCollection<Object> origin) {
1✔
165
            super(origin);
1✔
166
        }
1✔
167

168
        public DALCollection<Data> wraps() {
169
            return map((index, e) -> new Data(e, context, schemaType.access(index)));
1✔
170
        }
171

172
        public AutoMappingList autoMapping(Function<Data, Data> mapper) {
173
            return new AutoMappingList(mapper, wraps());
1✔
174
        }
175

176
        public DataList sort(Comparator<Data> comparator) {
177
            if (comparator != NOP_COMPARATOR)
1✔
178
                try {
179
                    return new DataList(new CollectionDALCollection<Object>(wraps().collect().stream()
1✔
180
                            .sorted(comparator).map(Data::instance).collect(toList())) {
1✔
181
                        @Override
182
                        public int firstIndex() {
183
                            return DataList.this.firstIndex();
1✔
184
                        }
185

186
                        @Override
187
                        public boolean infinite() {
188
                            return DataList.this.infinite();
1✔
189
                        }
190
                    });
191
                } catch (InfiniteCollectionException e) {
1✔
192
                    throw illegalOperation("Can not sort infinite collection");
1✔
193
                }
194
            return this;
1✔
195
        }
196

197
        public Data wrap() {
198
            return new Data(this, context, schemaType);
1✔
199
        }
200
    }
201
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc