Skip to content

Commit

Permalink
Fixes #25960: Remove unused and duplicate rest extractor lift-json me…
Browse files Browse the repository at this point in the history
…thods
  • Loading branch information
clarktsiory committed Dec 3, 2024
1 parent 3131863 commit cd64fe5
Show file tree
Hide file tree
Showing 25 changed files with 231 additions and 1,161 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ import io.scalaland.chimney.Transformer
import io.scalaland.chimney.dsl.*
import net.liftweb.common.*
import net.liftweb.http.Req
import zio.Chunk
import zio.NonEmptyChunk
import zio.json.*

Expand Down Expand Up @@ -115,6 +116,9 @@ object JsonQueryObjects {
final case class JQClasses(
classes: Option[List[String]]
)
final case class JQNodeIdChunk(
nodeId: Option[Chunk[NodeId]]
)
final case class JQNodeIdStatus(
nodeId: List[NodeId],
status: JQNodeStatusAction
Expand All @@ -141,6 +145,10 @@ object JsonQueryObjects {
)
}

final case class JQNodeInherited(
inherited: Option[Boolean]
)

final case class JQNodePropertyInfo(
inherited: Boolean,
value: String
Expand All @@ -166,6 +174,10 @@ object JsonQueryObjects {
}
}

final case class JQIncludeSystem(
includeSystem: Option[Boolean]
)

final case class JQDirectiveSectionVar(
name: String,
value: String
Expand Down Expand Up @@ -443,6 +455,7 @@ trait RudderJsonDecoders {
implicit val classesDecoder: JsonDecoder[JQClasses] = DeriveJsonDecoder.gen[JQClasses].orElse((_, _) => JQClasses(None))

implicit val nodeIdDecoder: JsonDecoder[NodeId] = JsonDecoder[String].map(NodeId.apply)
implicit val nodeIdChunkDecoder: JsonDecoder[JQNodeIdChunk] = DeriveJsonDecoder.gen[JQNodeIdChunk]
implicit val nodeStatusActionDecoder: JsonDecoder[JQNodeStatusAction] =
JsonDecoder[String].mapOrFail(JQNodeStatusAction.withNameInsensitiveEither(_).left.map(_.getMessage()))
implicit val nodeIdStatusDecoder: JsonDecoder[JQNodeIdStatus] = DeriveJsonDecoder
Expand All @@ -454,6 +467,7 @@ trait RudderJsonDecoders {
Right(x)
}
})
implicit val nodeInheritedDecoder: JsonDecoder[JQNodeInherited] = DeriveJsonDecoder.gen[JQNodeInherited]
implicit val nodestatusDecoder: JsonDecoder[JQNodeStatus] = DeriveJsonDecoder.gen[JQNodeStatus]
implicit val nodePropertyInfoDecoder: JsonDecoder[JQNodePropertyInfo] = DeriveJsonDecoder.gen[JQNodePropertyInfo]
implicit val nodeIdsSoftwareProperties: JsonDecoder[JQNodeIdsSoftwareProperties] =
Expand Down Expand Up @@ -517,6 +531,8 @@ trait RudderJsonDecoders {

implicit val ruleCategoryDecoder: JsonDecoder[JQRuleCategory] = DeriveJsonDecoder.gen

implicit val includeSystemDecoder: JsonDecoder[JQIncludeSystem] = DeriveJsonDecoder.gen

// RestDirective - lazy because section/sectionVar/directive are mutually recursive
implicit lazy val sectionDecoder: JsonDecoder[JQDirectiveSection] = DeriveJsonDecoder.gen
implicit lazy val variableDecoder: JsonDecoder[JQDirectiveSectionVar] = DeriveJsonDecoder.gen
Expand Down Expand Up @@ -664,6 +680,14 @@ class ZioJsonExtractor(queryParser: CmdbQueryParser with JsonQueryLexer) {
}
}

def extractNodeIdChunk(req: Req): PureResult[Option[Chunk[NodeId]]] = {
parseJson[JQNodeIdChunk](req).map(_.nodeId)
}

def extractNodeInherited(req: Req): PureResult[Option[Boolean]] = {
parseJson[JQNodeInherited](req).map(_.inherited)
}

def extractNodeIdStatus(req: Req): PureResult[JQNodeIdStatus] = {
if (req.json_?) {
parseJson[JQNodeIdStatus](req)
Expand Down Expand Up @@ -696,6 +720,14 @@ class ZioJsonExtractor(queryParser: CmdbQueryParser with JsonQueryLexer) {
}
}

def extractIncludeSystem(req: Req): PureResult[Option[Boolean]] = {
if (req.json_?) {
parseJson[JQIncludeSystem](req).map(_.includeSystem)
} else {
extractIncludeSystemFromParams(req.params)
}
}

def extractIdsFromParams(params: Map[String, List[String]]): PureResult[Option[List[String]]] = {
params.parseString("ids", s => Right(s.split(",").map(_.trim).toList))
}
Expand Down Expand Up @@ -900,4 +932,7 @@ class ZioJsonExtractor(queryParser: CmdbQueryParser with JsonQueryLexer) {
}
}

def extractIncludeSystemFromParams(params: Map[String, List[String]]): PureResult[Option[Boolean]] = {
params.parse("includeSystem", JsonDecoder[Boolean])
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,16 @@
*
*************************************************************************************
*/
package com.normation.rudder.web.services
package com.normation.rudder.config
import com.normation.box.*
import com.normation.errors.*
import zio.syntax.*

object ReasonBehavior extends Enumeration {
type ReasonBehavior = Value
val Disabled, Mandatory, Optionnal = Value
sealed trait ReasonBehavior

object ReasonBehavior {
case object Disabled extends ReasonBehavior
case object Mandatory extends ReasonBehavior
case object Optional extends ReasonBehavior
}

/**
Expand All @@ -54,20 +56,11 @@ trait UserPropertyService {
* @return the strategy
*/
// def reasonsFieldEnabled() : Boolean
def reasonsFieldBehavior: ReasonBehavior.ReasonBehavior
def reasonsFieldBehavior: ReasonBehavior
def reasonsFieldExplanation: String

}

class UserPropertyServiceImpl(val opt: ReasonsMessageInfo) extends UserPropertyService {

private val impl =
new StatelessUserPropertyService(() => opt.enabled.succeed, () => opt.mandatory.succeed, () => opt.explanation.succeed)

override val reasonsFieldBehavior = impl.reasonsFieldBehavior
override val reasonsFieldExplanation: String = impl.reasonsFieldExplanation
}

/**
* Reloadable service
*/
Expand All @@ -76,17 +69,16 @@ class StatelessUserPropertyService(
getMandatory: () => IOResult[Boolean],
getExplanation: () => IOResult[String]
) extends UserPropertyService {
import ReasonBehavior.*

// TODO: handle errors here!

override def reasonsFieldBehavior: ReasonBehavior.ReasonBehavior = {
override def reasonsFieldBehavior: ReasonBehavior = {
(getEnable().toBox.getOrElse(true), getMandatory().toBox.getOrElse(true)) match {
case (true, true) => ReasonBehavior.Mandatory
case (true, false) => ReasonBehavior.Optionnal
case (false, _) => ReasonBehavior.Disabled
case (true, true) => Mandatory
case (true, false) => Optional
case (false, _) => Disabled
}
}
override def reasonsFieldExplanation: String = getExplanation().toBox.getOrElse("")
override def reasonsFieldExplanation: String = getExplanation().toBox.getOrElse("")
}

class ReasonsMessageInfo(val enabled: Boolean, val mandatory: Boolean, val explanation: String)
Original file line number Diff line number Diff line change
Expand Up @@ -88,59 +88,44 @@ trait JsonExtractorUtils[A[_]] {
}
}

/*
* Still used in apiaccount/eventlog/group API, tags
*/
def extractJsonString[T](json: JValue, key: String, convertTo: String => Box[T] = boxedIdentity[String]): Box[A[T]] = {
extractJson(json, key, convertTo, { case JString(value) => value })
}
def extractJsonStringMultipleKeys[T](
json: JValue,
keys: List[String],
convertTo: String => Box[T] = boxedIdentity[String]
): Box[A[T]] = {
extractJson(json, keys, convertTo, { case JString(value) => value })
}

/*
* Still used in apiaccount API
*/
def extractJsonBoolean[T](json: JValue, key: String, convertTo: Boolean => Box[T] = boxedIdentity[Boolean]): Box[A[T]] = {
extractJson(json, key, convertTo, { case JBool(value) => value })
}

/*
* Still used in eventlog API
*/
def extractJsonInt(json: JValue, key: String): Box[A[Int]] = {
extractJsonBigInt(json, key, i => Full(i.toInt))
}

/*
* Still used in eventlog API
*/
def extractJsonBigInt[T](json: JValue, key: String, convertTo: BigInt => Box[T] = boxedIdentity[BigInt]): Box[A[T]] = {
extractJson(json, key, convertTo, { case JInt(value) => value })
}

def extractJsonObj[T](json: JValue, key: String, jsonValueFun: JObject => Box[T]): Box[A[T]] = {
/*
* Still used in eventlog API
*/
def extractJsonObj[T](json: JValue, key: String, jsonValueFun: JObject => Box[T]): Box[A[T]] = {
extractJson(json, key, jsonValueFun, { case obj: JObject => obj })
}
def extractJsonObjMultipleKeys[T](json: JValue, keys: List[String], jsonValueFun: JObject => Box[T]): Box[A[T]] = {
extractJson(json, keys, jsonValueFun, { case obj: JObject => obj })
}

def extractJsonListString[T](
json: JValue,
key: String,
convertTo: List[String] => Box[T] = boxedIdentity[List[String]]
): Box[A[T]] = {
json \ key match {
case JArray(values) =>
(for {
strings <- traverse(values) {
_ match {
case JString(s) => Full(s)
case x => Failure(s"Error extracting a string from json: '${x}'")
}
}
converted <- convertTo(strings.toList)
} yield {
converted
}).map(monad.pure(_))
case JNothing => emptyValue(key) ?~! s"Array of string is empty"
case _ => Failure(s"Not a good value for parameter ${key}")
}
}

/*
* Still used in tags, eventlog/apiaccount API
*/
def extractJsonArray[T](json: JValue, key: String)(convertTo: JValue => Box[T]): Box[A[List[T]]] = {
val trueJson =
if (key.isEmpty) json else json \ key
Expand All @@ -155,6 +140,9 @@ trait JsonExtractorUtils[A[_]] {
case _ => Failure(s"Invalid json to extract a json array, current value is: ${compactRender(json)}")
}
}
/*
* Still used in tags, eventlog/apiaccount API
*/
def extractJsonArray[T](json: JValue, keys: List[String])(convertTo: JValue => Box[T]): Box[A[List[T]]] = {
keys.map(k => extractJsonArray(json, k)(convertTo)).reduce[Box[A[List[T]]]] {
case (Full(res), _) => Full(res)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ import scala.reflect.ClassTag
import zio.Chunk
import zio.NonEmptyChunk


@RunWith(classOf[JUnitRunner])
class TestMergeGroupProperties extends Specification {

Expand Down
Loading

0 comments on commit cd64fe5

Please sign in to comment.