-
Notifications
You must be signed in to change notification settings - Fork 390
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(codegen): streamline protocol implementations by providing conve…
…nience methods
- Loading branch information
Showing
24 changed files
with
283 additions
and
323 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
73 changes: 73 additions & 0 deletions
73
.../java/org/apache/plc4x/plugins/codegenerator/language/mspec/protocol/ProtocolHelpers.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,73 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.apache.plc4x.plugins.codegenerator.language.mspec.protocol; | ||
|
||
import org.apache.plc4x.plugins.codegenerator.protocol.Protocol; | ||
import org.apache.plc4x.plugins.codegenerator.types.exceptions.GenerationException; | ||
|
||
import java.io.InputStream; | ||
import java.util.Objects; | ||
|
||
public interface ProtocolHelpers extends Protocol { | ||
|
||
/** | ||
* Returns a mspec stream by using {@link Protocol#getName()} | ||
* | ||
* @return the {@link InputStream} of the referenced {@code mspecName} | ||
* @throws GenerationException if the mspec can't be found. | ||
*/ | ||
default InputStream getMspecStream() throws GenerationException { | ||
return getMspecStream(getName()); | ||
} | ||
|
||
/** | ||
* Returns a mspec stream for a give name | ||
* | ||
* @param mspecName the name without the .mspec extension | ||
* @return the {@link InputStream} of the referenced {@code mspecName} | ||
* @throws GenerationException if the mspec can't be found. | ||
*/ | ||
default InputStream getMspecStream(String mspecName) throws GenerationException { | ||
Objects.requireNonNull(mspecName, "mspecName must be set"); | ||
String versionSubPath = getVersion().map(version -> "/v" + version).orElse(""); | ||
String packageName = getPackageName(); | ||
String path = "/protocols/" + packageName + versionSubPath + "/" + mspecName + ".mspec"; | ||
InputStream inputStream = getClass().getResourceAsStream(path); | ||
if (inputStream == null) { | ||
throw new GenerationException("Error loading " + mspecName + " schema for protocol '" + getName() + "' (path " + path + ")"); | ||
} | ||
return inputStream; | ||
} | ||
|
||
/** | ||
* @return {@link Protocol#getName()} in sanitized form | ||
*/ | ||
default String getSanitizedName() { | ||
return Objects.requireNonNull(getName(), "protocol should return useful value at getName()") | ||
// Replace - with emptiness | ||
.replaceAll("-", ""); | ||
} | ||
|
||
/** | ||
* @return the package name | ||
*/ | ||
default String getPackageName() { | ||
return getSanitizedName(); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...rg/apache/plc4x/plugins/codegenerator/language/mspec/protocol/ValidatableTypeContext.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,37 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.apache.plc4x.plugins.codegenerator.language.mspec.protocol; | ||
|
||
import org.apache.plc4x.plugins.codegenerator.protocol.TypeContext; | ||
import org.apache.plc4x.plugins.codegenerator.types.exceptions.GenerationException; | ||
|
||
public interface ValidatableTypeContext extends TypeContext { | ||
|
||
/** | ||
* validates the {@link TypeContext} | ||
* | ||
* @throws GenerationException if {@link TypeContext} | ||
*/ | ||
default void validate() throws GenerationException { | ||
// TODO: check that we have at least of parsed type | ||
if (getUnresolvedTypeReferences().size() > 0) { | ||
throw new GenerationException("Unresolved types left: " + getUnresolvedTypeReferences()); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
Oops, something went wrong.