Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
harris2012 committed Jun 11, 2024
1 parent f89586c commit c47fdaf
Show file tree
Hide file tree
Showing 13 changed files with 334 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# To get started with Dependabot version updates, you'll need to specify which
# package ecosystems to update and where the package manifests are located.
# Please see the documentation for all configuration options:
# https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates

version: 2
updates:
- package-ecosystem: "maven" # See documentation for possible values
directory: "/" # Location of package manifests
schedule:
interval: "daily"
45 changes: 45 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# 执行这个 Actions 可以发布代码带中央仓库
name: deploy

on:
workflow_dispatch:

release:
types:
- created

jobs:
build:

runs-on: ubuntu-latest

steps:

- uses: actions/checkout@v2

- name: Setup Java JDK
uses: actions/setup-java@v3.9.0
with:
distribution: 'adopt'
java-version: '8'

- name: Build
run: mvn -B clean package

- name: Set up Apache Maven Central
uses: actions/setup-java@v3.9.0
with:
distribution: 'adopt'
java-version: '8'
server-id: ossrh
server-username: MAVEN_USERNAME
server-password: MAVEN_PASSWORD
gpg-private-key: ${{ secrets.MAVEN_GPG_PRIVATE_KEY}}
gpg-passphrase: MAVEN_GPG_PASSPHRASE

- name: Publish to Apache Maven Central
run: mvn clean deploy -Dgpg.skip=false
env:
MAVEN_USERNAME: ${{ secrets.MAVEN_USERNAME }}
MAVEN_PASSWORD: ${{ secrets.MAVEN_PASSWORD }}
MAVEN_GPG_PASSPHRASE: ${{ secrets.MAVEN_GPG_PASSPHRASE }}
22 changes: 22 additions & 0 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: push

on:
push:
branches: [ master ]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Setup Java JDK
uses: actions/setup-java@v3.9.0
with:
distribution: 'adopt'
java-version: '8'

- name: Build
run: mvn -B clean package
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
target/
.idea/
.project
*.iml
11 changes: 11 additions & 0 deletions .mergify.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
pull_request_rules:
- name: automatic merge for dependeabot
conditions:
- and:
- "author=dependabot[bot]"
- or:
- "title~=Bump super-pom from"
- "title~=Bump super-version from"
actions:
merge:
method: rebase
54 changes: 54 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.panosen</groupId>
<artifactId>panosen-plain-schema</artifactId>
<version>0.1.0</version>

<parent>
<groupId>com.panosen</groupId>
<artifactId>super-pom</artifactId>
<version>1.6.0</version>
</parent>

<name>Panosen Plain Schema</name>
<packaging>jar</packaging>
<url>https://github.com/panosen/panosen-x-plain-schema</url>
<description>Panosen plain schema</description>

<licenses>
<license>
<name>MIT</name>
<url>https://github.com/panosen/panosen-x-plain-schema/blob/master/LICENSE</url>
</license>
</licenses>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.panosen</groupId>
<artifactId>super-version</artifactId>
<version>1.6.2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>

</dependencies>
</project>
5 changes: 5 additions & 0 deletions src/main/java/com/panosen/plainschema/Plain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.panosen.plainschema;

public abstract class Plain {
public abstract String type();
}
20 changes: 20 additions & 0 deletions src/main/java/com/panosen/plainschema/PlainArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.panosen.plainschema;

import java.util.List;

public class PlainArray extends Plain {
@Override
public String type() {
return "array";
}

private List<Plain> items;

public List<Plain> getItems() {
return items;
}

public void setItems(List<Plain> items) {
this.items = items;
}
}
20 changes: 20 additions & 0 deletions src/main/java/com/panosen/plainschema/PlainMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.panosen.plainschema;

import java.util.Map;

public class PlainMap extends Plain {
@Override
public String type() {
return "map";
}

private Map<String, Plain> map;

public Map<String, Plain> getMap() {
return map;
}

public void setMap(Map<String, Plain> map) {
this.map = map;
}
}
62 changes: 62 additions & 0 deletions src/main/java/com/panosen/plainschema/PlainParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.panosen.plainschema;

import com.google.gson.*;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;

public class PlainParser {

private static final Gson GSON = new Gson();

public Plain parse(String json) {
JsonObject jsonObject = GSON.fromJson(json, JsonObject.class);

return parse(jsonObject);
}

private Plain parse(JsonElement jsonElement) {
if (jsonElement.isJsonNull()) {
return null;
}

if (jsonElement.isJsonObject()) {
return parseMap((JsonObject) jsonElement);
}

if (jsonElement.isJsonArray()) {
return parseArray((JsonArray) jsonElement);
}

if (jsonElement.isJsonPrimitive()) {
return parseValue((JsonPrimitive) jsonElement);
}

return null;
}

private PlainArray parseArray(JsonArray jsonArray) {
PlainArray sampleArray = new PlainArray();
sampleArray.setItems(new ArrayList<>());
for (int index = 0, length = jsonArray.size(); index < length; index++) {
sampleArray.getItems().add(parse(jsonArray.get(index)));
}
return sampleArray;
}

private PlainMap parseMap(JsonObject jsonObject) {
PlainMap sampleMap = new PlainMap();
sampleMap.setMap(new LinkedHashMap<>());
for (Map.Entry<String, JsonElement> entry : jsonObject.entrySet()) {
sampleMap.getMap().putIfAbsent(entry.getKey(), parse(entry.getValue()));
}
return sampleMap;
}

private PlainValue parseValue(JsonPrimitive jsonPrimitive) {
PlainValue sampleValue = new PlainValue();
sampleValue.setValue(jsonPrimitive);
return sampleValue;
}
}
20 changes: 20 additions & 0 deletions src/main/java/com/panosen/plainschema/PlainValue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.panosen.plainschema;

import com.google.gson.JsonPrimitive;

public class PlainValue extends Plain {
@Override
public String type() {
return "value";
}

private JsonPrimitive value;

public JsonPrimitive getValue() {
return value;
}

public void setValue(JsonPrimitive value) {
this.value = value;
}
}
46 changes: 46 additions & 0 deletions src/test/java/com/panosen/localconfig/mysql/PlainParserTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.panosen.localconfig.mysql;

import com.panosen.plainschema.*;
import org.junit.Assert;
import org.junit.Test;

import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

public class PlainParserTest {

@Test
public void test() throws URISyntaxException, IOException {

URL mm = this.getClass().getClassLoader().getResource("sample.json");
Path path = Paths.get(mm.toURI());
List<String> lines = Files.readAllLines(path);
String json1 = String.join("", lines);

Plain plain = new PlainParser().parse(json1);

PlainMap plainMap = (PlainMap) plain;

Assert.assertEquals(5, plainMap.getMap().size());
Assert.assertEquals("tom", ((PlainValue) plainMap.getMap().get("name")).getValue().getAsString());
Assert.assertEquals(17, ((PlainValue) plainMap.getMap().get("age")).getValue().getAsInt());
Assert.assertNull(plainMap.getMap().get("other"));

PlainArray plainArray = (PlainArray) plainMap.getMap().get("scores");
Assert.assertEquals(3, plainArray.getItems().size());
Assert.assertEquals(1, ((PlainValue) plainArray.getItems().get(0)).getValue().getAsInt());
Assert.assertNull(plainArray.getItems().get(1));
Assert.assertEquals("2", ((PlainValue) plainArray.getItems().get(2)).getValue().getAsString());


PlainMap plainMap1 = (PlainMap) plainMap.getMap().get("items");
Assert.assertEquals(2, plainMap1.getMap().size());
Assert.assertEquals("a1",((PlainValue)plainMap1.getMap().get("a")).getValue().getAsString());
Assert.assertEquals(11,((PlainValue)plainMap1.getMap().get("b")).getValue().getAsInt());
}
}
14 changes: 14 additions & 0 deletions src/test/resources/sample.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "tom",
"age": 17,
"other": null,
"scores": [
1,
null,
2
],
"items": {
"a": "a1",
"b": 11
}
}

0 comments on commit c47fdaf

Please sign in to comment.