Skip to content

Commit 9774b66

Browse files
committed
Address review feedback
1 parent 5828ed5 commit 9774b66

File tree

3 files changed

+33
-24
lines changed

3 files changed

+33
-24
lines changed

modules/core/src/main/scala/com.snowplowanalytics.iglu/client/resolver/Resolver.scala

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,9 @@ final case class Resolver[F[_]](repos: List[Registry], cache: Option[ResolverCac
3838
private[client] val allRepos: NonEmptyList[Registry] =
3939
NonEmptyList[Registry](Registry.EmbeddedRegistry, repos)
4040

41-
private val allIgluCentral = repos.collect {
41+
private val allIgluCentral: Set[String] = repos.collect {
4242
case Registry.Http(config, connection)
43-
if config.name.toLowerCase.matches(".*iglu[- ]?central.*") && connection.uri.toString
44-
.contains("iglucentral") =>
43+
if Option(connection).filter(_.uri.getHost.matches(""".*\biglucentral\b.*""")).isDefined =>
4544
config.name
4645
}.toSet
4746

@@ -188,30 +187,38 @@ final case class Resolver[F[_]](repos: List[Registry], cache: Option[ResolverCac
188187
): F[Either[ResolutionError, Json]] =
189188
lookupSchemaResult(schemaKey).map(_.map(_.value.schema))
190189

190+
/**
191+
* Looks up all the schemas with the same model until `maxSchemaKey`.
192+
* For the schemas of previous revisions, it starts with addition = 0
193+
* and increments it until a NotFound.
194+
*
195+
* @param maxSchemaKey The SchemaKey until which schemas of the same model should get returned
196+
* @return All the schemas if all went well, [[Resolver.SchemaResolutionError]] with the first error that happened
197+
* while looking up the schemas if something went wrong.
198+
*/
191199
def lookupSchemasUntil(
192200
maxSchemaKey: SchemaKey
193201
)(implicit
194202
F: Monad[F],
195203
L: RegistryLookup[F],
196204
C: Clock[F]
197-
): F[Either[SchemaResolutionError, List[RawSchema]]] = {
205+
): F[Either[SchemaResolutionError, List[SelfDescribingSchema[Json]]]] = {
198206

199207
// If Iglu Central or any of its mirrors doesn't have a schema,
200208
// it should be considered NotFound, even if one of them returned an error
201209
def isNotFound(error: ResolutionError): Boolean = {
202210
val (igluCentral, custom) = error.value.partition { case (repo, _) =>
203211
allIgluCentral.contains(repo)
204212
}
205-
custom.values.flatMap(_.errors).forall(_ == RegistryError.NotFound) &&
206213
(igluCentral.isEmpty || igluCentral.values.exists(
207214
_.errors.forall(_ == RegistryError.NotFound)
208-
))
215+
)) && custom.values.flatMap(_.errors).forall(_ == RegistryError.NotFound)
209216
}
210217

211218
def go(
212219
current: SchemaVer.Full,
213-
acc: List[RawSchema]
214-
): F[Either[SchemaResolutionError, List[RawSchema]]] = {
220+
acc: List[SelfDescribingSchema[Json]]
221+
): F[Either[SchemaResolutionError, List[SelfDescribingSchema[Json]]]] = {
215222
val currentSchemaKey = maxSchemaKey.copy(version = current)
216223
lookupSchema(currentSchemaKey).flatMap {
217224
case Left(e) =>
@@ -225,15 +232,17 @@ final case class Resolver[F[_]](repos: List[Registry], cache: Option[ResolverCac
225232
if (current.revision < maxSchemaKey.version.revision)
226233
go(
227234
current.copy(addition = current.addition + 1),
228-
RawSchema(currentSchemaKey, json) :: acc
235+
SelfDescribingSchema(SchemaMap(currentSchemaKey), json) :: acc
229236
)
230237
else if (current.addition < maxSchemaKey.version.addition)
231238
go(
232239
current.copy(addition = current.addition + 1),
233-
RawSchema(currentSchemaKey, json) :: acc
240+
SelfDescribingSchema(SchemaMap(currentSchemaKey), json) :: acc
234241
)
235242
else
236-
Monad[F].pure(Right((RawSchema(currentSchemaKey, json) :: acc).reverse))
243+
Monad[F].pure(
244+
Right((SelfDescribingSchema(SchemaMap(currentSchemaKey), json) :: acc).reverse)
245+
)
237246
}
238247
}
239248

@@ -439,8 +448,6 @@ object Resolver {
439448
type SchemaListLookupResult = ResolverResult[SchemaListKey, SchemaList]
440449
type SupersededBy = Option[SchemaVer.Full]
441450

442-
case class RawSchema(schemaKey: SchemaKey, json: Json)
443-
444451
/**
445452
* The result of doing schema lookup
446453
*

modules/core/src/test/scala/com.snowplowanalytics.iglu.client/resolver/ResolverResultSpec.scala

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import io.circe.Json
3232
import io.circe.literal._
3333

3434
// Iglu Core
35-
import com.snowplowanalytics.iglu.core.{SchemaKey, SchemaVer}
35+
import com.snowplowanalytics.iglu.core.{SchemaKey, SchemaVer, SelfDescribingSchema}
3636

3737
// This project
3838
import com.snowplowanalytics.iglu.client.ClientError._
@@ -44,11 +44,7 @@ import com.snowplowanalytics.iglu.client.resolver.registries.{
4444
RegistryError,
4545
RegistryLookup
4646
}
47-
import com.snowplowanalytics.iglu.client.resolver.Resolver.{
48-
RawSchema,
49-
SchemaItem,
50-
SchemaResolutionError
51-
}
47+
import com.snowplowanalytics.iglu.client.resolver.Resolver.{SchemaItem, SchemaResolutionError}
5248

5349
// Specs2
5450
import com.snowplowanalytics.iglu.client.SpecHelpers._
@@ -769,7 +765,7 @@ class ResolverResultSpec extends Specification with ValidatedMatchers with CatsE
769765

770766
import ResolverSpecHelpers.LookupSchemasUntil._
771767

772-
def testLookupUntil(maxSchemaKey: SchemaKey, expected: List[RawSchema]) =
768+
def testLookupUntil(maxSchemaKey: SchemaKey, expected: List[SelfDescribingSchema[Json]]) =
773769
for {
774770
resolver <- mkResolver
775771
result <- resolver.lookupSchemasUntil(maxSchemaKey)

modules/core/src/test/scala/com.snowplowanalytics.iglu.client/resolver/ResolverSpecHelpers.scala

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,13 @@ import io.circe.parser._
2929

3030
// LRU Map
3131
import com.snowplowanalytics.iglu.core.circe.implicits._
32-
import com.snowplowanalytics.iglu.core.{SchemaKey, SchemaList, SchemaVer}
32+
import com.snowplowanalytics.iglu.core.{
33+
SchemaKey,
34+
SchemaList,
35+
SchemaMap,
36+
SchemaVer,
37+
SelfDescribingSchema
38+
}
3339
import com.snowplowanalytics.lrumap.LruMap
3440

3541
// This project
@@ -39,7 +45,6 @@ import com.snowplowanalytics.iglu.client.resolver.registries.{
3945
RegistryError,
4046
RegistryLookup
4147
}
42-
import com.snowplowanalytics.iglu.client.resolver.Resolver.RawSchema
4348

4449
object ResolverSpecHelpers {
4550

@@ -260,12 +265,13 @@ object ResolverSpecHelpers {
260265
revision: Int,
261266
addition: Int,
262267
embeddedFolder: String = "iglu-test-embedded"
263-
): RawSchema = {
268+
): SelfDescribingSchema[Json] = {
264269
val path =
265270
s"/$embeddedFolder/schemas/$vendor/$name/$format/$model-$revision-$addition"
266271
val content = Source.fromInputStream(getClass.getResourceAsStream(path)).mkString
267272
parse(content) match {
268-
case Right(json) => RawSchema(getUntilSchemaKey(model, revision, addition), json)
273+
case Right(json) =>
274+
SelfDescribingSchema(SchemaMap(getUntilSchemaKey(model, revision, addition)), json)
269275
case Left(err) =>
270276
throw new IllegalArgumentException(s"$path can't be parsed as JSON : [$err]")
271277
}

0 commit comments

Comments
 (0)