From 5c2597133c64ab0ffe2a54d1f23834249278db4a Mon Sep 17 00:00:00 2001
From: Martin Czygan <martin.czygan@gmail.com>
Date: Tue, 1 Jun 2021 17:38:11 +0200
Subject: add mapper: MapperReleaseResolvedContainerName

---
 skate/cmd/skate-map/main.go |  3 ++-
 skate/map.go                | 17 +++++++++++++++++
 2 files changed, 19 insertions(+), 1 deletion(-)

diff --git a/skate/cmd/skate-map/main.go b/skate/cmd/skate-map/main.go
index 060e324..3d5630b 100644
--- a/skate/cmd/skate-map/main.go
+++ b/skate/cmd/skate-map/main.go
@@ -66,7 +66,7 @@ func main() {
 	flag.Parse()
 	availableMappers := map[string]skate.Mapper{
 		// Add new mapper functions here. TODO: add more docs, and improve
-		// composability, e.g. like middleware.
+		// composability, e.g. like middleware. Also improve naming.
 		"id":   skate.Identity,
 		"ff":   skate.CreateFixedMapper(*extraValue),
 		"ti":   skate.MapperTitle,
@@ -77,6 +77,7 @@ func main() {
 		"cni":  skate.MapperContainerName,
 		"cns":  skate.MapperContainerNameSandcrawler,
 		"rcns": skate.MapperReleaseContainerName,
+		"vcns": skate.MapperReleaseResolvedContainerName,
 	}
 	if *logFile != "" {
 		f, err := os.OpenFile(*logFile, os.O_CREATE|os.O_APPEND, 0644)
diff --git a/skate/map.go b/skate/map.go
index a06b5ae..1d21818 100644
--- a/skate/map.go
+++ b/skate/map.go
@@ -257,6 +257,23 @@ func MapperReleaseContainerName(p []byte) (fields [][]byte, err error) {
 	return [][]byte{key, p}, nil
 }
 
+// MapperReleaseResolvedContainerName extracts slug container name from
+// resolved container names.
+func MapperReleaseResolvedContainerName(p []byte) (fields [][]byte, err error) {
+	var (
+		doc Release
+		key []byte
+	)
+	if err := json.Unmarshal(p, &doc); err != nil {
+		return nil, err
+	}
+	if doc.Extra.Skate.ResolvedContainerName == "" {
+		return nil, nil
+	}
+	key = []byte(sandcrawlerSlugify(wsReplacer.Replace(strings.TrimSpace(doc.Extra.Skate.ResolvedContainerName))))
+	return [][]byte{key, p}, nil
+}
+
 // MapperPartial works on partial documents.
 func MapperPartial(p []byte) (fields [][]byte, err error) {
 	return nil, nil
-- 
cgit v1.2.3