-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MapAccess to better access sub maps in template code (#319)
- Loading branch information
Showing
7 changed files
with
204 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
cms-core/src/main/java/com/condation/cms/core/content/MapAccess.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package com.condation.cms.core.content; | ||
|
||
/*- | ||
* #%L | ||
* cms-core | ||
* %% | ||
* Copyright (C) 2023 - 2024 CondationCMS | ||
* %% | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public | ||
* License along with this program. If not, see | ||
* <http://www.gnu.org/licenses/gpl-3.0.html>. | ||
* #L% | ||
*/ | ||
|
||
import java.util.Collection; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
import com.condation.cms.api.utils.MapUtil; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@RequiredArgsConstructor | ||
public class MapAccess implements Map<String, Object> { | ||
|
||
private final Map<String, Object> wrapped; | ||
|
||
@Override | ||
public int size() { | ||
return wrapped.size(); | ||
} | ||
|
||
@Override | ||
public boolean isEmpty() { | ||
return wrapped.isEmpty(); | ||
} | ||
|
||
@Override | ||
public boolean containsKey(Object key) { | ||
return MapUtil.getValue(wrapped, (String)key) != null; | ||
} | ||
|
||
@Override | ||
public boolean containsValue(Object value) { | ||
throw new UnsupportedOperationException("Unimplemented method 'containsValue'"); | ||
} | ||
|
||
@Override | ||
public Object get(Object key) { | ||
return MapUtil.getValue(wrapped, (String)key); | ||
} | ||
|
||
@Override | ||
public Object put(String key, Object value) { | ||
throw new UnsupportedOperationException("Unimplemented method 'put'"); | ||
} | ||
|
||
@Override | ||
public Object remove(Object key) { | ||
throw new UnsupportedOperationException("Unimplemented method 'remove'"); | ||
} | ||
|
||
@Override | ||
public void putAll(Map<? extends String, ? extends Object> m) { | ||
throw new UnsupportedOperationException("Unimplemented method 'putAll'"); | ||
} | ||
|
||
@Override | ||
public void clear() { | ||
throw new UnsupportedOperationException("Unimplemented method 'clear'"); | ||
} | ||
|
||
@Override | ||
public Set<String> keySet() { | ||
return wrapped.keySet(); | ||
} | ||
|
||
@Override | ||
public Collection<Object> values() { | ||
return wrapped.values(); | ||
} | ||
|
||
@Override | ||
public Set<Entry<String, Object>> entrySet() { | ||
return wrapped.entrySet(); | ||
} | ||
|
||
} |
74 changes: 74 additions & 0 deletions
74
cms-core/src/test/java/com/condation/cms/core/content/MapAccessTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package com.condation.cms.core.content; | ||
|
||
/*- | ||
* #%L | ||
* cms-core | ||
* %% | ||
* Copyright (C) 2023 - 2024 CondationCMS | ||
* %% | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public | ||
* License along with this program. If not, see | ||
* <http://www.gnu.org/licenses/gpl-3.0.html>. | ||
* #L% | ||
*/ | ||
|
||
import java.util.Map; | ||
|
||
import org.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class MapAccessTest { | ||
|
||
static MapAccess sut; | ||
|
||
@BeforeAll | ||
static void setup () { | ||
sut = new MapAccess(Map.of( | ||
"title", "The title", | ||
"seo", Map.of( | ||
"title", "Seo title" | ||
) | ||
)); | ||
} | ||
|
||
@Test | ||
void test_containsKey_true () { | ||
Assertions.assertThat(sut.containsKey("title")).isTrue(); | ||
Assertions.assertThat(sut.containsKey("seo.title")).isTrue(); | ||
} | ||
|
||
@Test | ||
void test_containsKey_false() { | ||
Assertions.assertThat(sut.containsKey("desc")).isFalse(); | ||
Assertions.assertThat(sut.containsKey("seo.desc")).isFalse(); | ||
} | ||
|
||
@Test | ||
void test_get_with_value() { | ||
Assertions.assertThat(sut.get("title")).isEqualTo("The title"); | ||
Assertions.assertThat(sut.get("seo.title")).isEqualTo("Seo title"); | ||
} | ||
|
||
@Test | ||
void test_get_without_value() { | ||
Assertions.assertThat(sut.get("desc")).isNull(); | ||
Assertions.assertThat(sut.get("seo.desc")).isNull(); | ||
} | ||
|
||
@Test | ||
void test_getOrDefault() { | ||
Assertions.assertThat(sut.getOrDefault("desc", "default desc")).isEqualTo("default desc"); | ||
Assertions.assertThat(sut.getOrDefault("seo.desc", "default seo desc")).isEqualTo("default seo desc"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
[[formats]] | ||
name = "small" | ||
width = 256 | ||
height = 256 | ||
format = "webp" | ||
compression = true | ||
|
||
[[formats]] | ||
name = "big" | ||
width = 512 | ||
height = 512 | ||
format = "webp" | ||
compression = true | ||
|
||
[[formats]] | ||
name = "test2" | ||
width = 72 | ||
height = 72 | ||
format = "webp" | ||
compression = true | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters