Refactor pretty printing
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / nb / rfc8040 / legacy / QueryParameters.java
1 /*
2  * Copyright (c) 2021 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.restconf.nb.rfc8040.legacy;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.Beta;
13 import java.util.List;
14 import java.util.Set;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.opendaylight.restconf.api.query.DepthParam;
18 import org.opendaylight.restconf.api.query.PrettyPrintParam;
19 import org.opendaylight.restconf.server.api.DataGetParams;
20 import org.opendaylight.restconf.server.api.FormatParameters;
21 import org.opendaylight.yangtools.yang.common.QName;
22
23 /**
24  * This holds various options acquired from a requests's query part. This class needs to be further split up to make
25  * sense of it, as parts of it pertain to how a {@link NormalizedNodePayload} should be created while others how it
26  * needs to be processed (for example filtered).
27  */
28 @Beta
29 // FIXME: this probably needs to be renamed back to WriterParams, or somesuch
30 public record QueryParameters(
31         @Nullable DepthParam depth,
32         @NonNull PrettyPrintParam prettyPrint,
33         @Nullable List<Set<QName>> fields) implements FormatParameters {
34     public static final @NonNull QueryParameters EMPTY = new QueryParameters(null, PrettyPrintParam.FALSE, null);
35
36     public QueryParameters {
37         requireNonNull(prettyPrint);
38     }
39
40     public static @NonNull QueryParameters of(final DataGetParams params) {
41         final var depth = params.depth();
42         final var prettyPrint = params.prettyPrint();
43         return depth == null && !prettyPrint.value() ? EMPTY : new QueryParameters(depth, prettyPrint, null);
44     }
45
46     public static @NonNull QueryParameters of(final DataGetParams params, final List<Set<QName>> fields) {
47         return fields == null ? of(params) : new QueryParameters(params.depth(), params.prettyPrint(), fields);
48     }
49 }