11package com .amadeus .dataio .config .fields
22
3+ import com .amadeus .dataio .core .Logging
34import com .amadeus .dataio .core .time .DateRange
45import com .typesafe .config .Config
56import org .apache .spark .sql .Column
67import org .apache .spark .sql .functions .col
78
9+ import java .time .LocalDate
810import scala .util .Try
911
10- trait DateFilterConfigurator {
12+ trait DateFilterConfigurator extends Logging {
13+ def getDateFilterConfig (implicit config : Config ): Option [DateFilterConfig ] = {
14+ if (! config.hasPath(" date_filter" )) {
15+ return None
16+ }
1117
12- /** @param config The typesafe Config object holding the configuration.
13- * @return The date range, or None.
14- * @throws IllegalArgumentException If the data was found but is not formatted properly.
15- */
16- def getDateFilterRange (implicit config : Config ): Option [DateRange ] = {
17- // If only reference or offset is present for a given syntax, this is incorrect
18- val syntaxTwoOk = testArguments(" date_filter.reference" , " date_filter.offset" )
19-
20- // if any is false, we have an incomplete conf
21- if (! syntaxTwoOk) {
22- throw new IllegalArgumentException (" Configuration incomplete for date filter reference/offset" )
23- } else {
24- Try {
25- getArguments(" date_filter.reference" , " date_filter.offset" )
26- }.toOption
18+ val filterConfig = config.getConfig(" date_filter" )
19+
20+ val referenceOffsetConfig = getReferenceOffset(filterConfig)
21+ val fromUntilConfig = getFromUntil(filterConfig)
22+
23+ (referenceOffsetConfig, fromUntilConfig) match {
24+ case (Some (_), Some (_)) =>
25+ throw new IllegalArgumentException (
26+ " date_filter: Cannot use both reference+offset and from/until syntaxes simultaneously"
27+ )
28+
29+ case (Some (config), None ) =>
30+ Some (config)
31+
32+ case (None , Some (config)) =>
33+ Some (config)
34+
35+ case (None , None ) =>
36+ logger.warn(" date_filter: configuration found but no valid syntax detected (expected reference+offset or from/until). Skipping." )
37+ None
2738 }
2839 }
2940
30- /** @param dateReferencePath the path in the config holding Date Reference
31- * @param offsetPath the path in the config holding Date Offset
32- * @return True if the config holds either both keys or none
33- */
34- private def testArguments (dateReferencePath : String , offsetPath : String )(implicit config : Config ): Boolean = {
35- // there must be both keys or none => true
36- (config.hasPath(dateReferencePath) && config.hasPath(offsetPath)) ||
37- (! config.hasPath(dateReferencePath) && ! config.hasPath(offsetPath))
41+ private def getFromUntil (config : Config ): Option [DateFilterConfig ] = {
42+ val hasFrom = config.hasPath(" from" )
43+ val hasUntil = config.hasPath(" until" )
3844
45+ (hasFrom, hasUntil) match {
46+ case (true , false ) =>
47+ val from = LocalDate .parse(config.getString(" from" ))
48+ Some (DateFilterConfig .FromOnly (from))
49+ case (false , true ) =>
50+ val until = LocalDate .parse(config.getString(" until" ))
51+ Some (DateFilterConfig .UntilOnly (until))
52+ case (true , true ) =>
53+ val from = LocalDate .parse(config.getString(" from" ))
54+ val until = LocalDate .parse(config.getString(" until" ))
55+ Some (DateFilterConfig .Range (DateRange (from, until)))
56+ case _ => None
57+ }
3958 }
4059
41- /** @param dateReferencePath the path in the config holding Date Reference
42- * @param offsetPath the path in the config holding Date Offset
43- * @return The DateRange corresponding to the reference and the offset
44- */
45- private def getArguments (dateReferencePath : String , offsetPath : String )(implicit config : Config ): DateRange = {
46- val referenceDate = config.getString(dateReferencePath)
47- val offset = config.getString(offsetPath)
48- DateRange (referenceDate, offset)
60+ private def getReferenceOffset (config : Config ): Option [DateFilterConfig ] = {
61+ val hasReference = config.hasPath(" reference" )
62+ val hasOffset = config.hasPath(" offset" )
63+
64+ (hasReference, hasOffset) match {
65+ case (true , true ) =>
66+ val reference = config.getString(" reference" )
67+ val offset = config.getString(" offset" )
68+ Some (DateFilterConfig .Range (DateRange (reference, offset)))
69+ case (true , false ) | (false , true ) =>
70+ throw new IllegalArgumentException (
71+ " date_filter with reference/offset requires both 'reference' and 'offset'"
72+ )
73+ case _ =>
74+ None
75+ }
4976 }
5077
5178 /** @param config The typesafe Config object holding the configuration.
@@ -57,3 +84,11 @@ trait DateFilterConfigurator {
5784 }.toOption
5885 }
5986}
87+
88+ sealed trait DateFilterConfig
89+
90+ object DateFilterConfig {
91+ case class Range (dateRange : DateRange ) extends DateFilterConfig
92+ case class FromOnly (from : LocalDate ) extends DateFilterConfig // >= from (inclusive)
93+ case class UntilOnly (until : LocalDate ) extends DateFilterConfig // < until (exclusive)
94+ }
0 commit comments