diff --git a/openapi2beans/pkg/embedded/embedded.go b/openapi2beans/pkg/embedded/embedded.go index aa50c39..c77a60f 100644 --- a/openapi2beans/pkg/embedded/embedded.go +++ b/openapi2beans/pkg/embedded/embedded.go @@ -23,6 +23,11 @@ var embeddedFileSystem embed.FS // It just delegates to teh embed.FS var readOnlyFileSystem ReadOnlyFileSystem +const ( + GET_JAVA_TEMPLATE_CLASS_OPTION = "class" + GET_JAVA_TEMPLATE_ENUM_OPTION = "enum" +) + type templates struct { JavaClassTemplate *mustache.Template JavaEnumTemplate *mustache.Template @@ -44,28 +49,20 @@ func GetReadOnlyFileSystem() ReadOnlyFileSystem { return readOnlyFileSystem } -func GetJavaClassTemplate() (*mustache.Template, error) { - var err error - fs := GetReadOnlyFileSystem() - // Note: The cache is set when we read the versions from the embedded file. - templatesCache, err = readTemplatesFromEmbeddedFiles(fs, templatesCache) - var template *mustache.Template - if err == nil { - template = templatesCache.JavaClassTemplate - } else { - err = openapi2beans_errors.NewError("Failed to read templates from embedded file. Reason is: %s", err.Error()) - } - return template, err -} - -func GetJavaEnumTemplate() (*mustache.Template, error) { +func GetJavaTemplate(templateOption string) (*mustache.Template, error) { var err error fs := GetReadOnlyFileSystem() // Note: The cache is set when we read the versions from the embedded file. templatesCache, err = readTemplatesFromEmbeddedFiles(fs, templatesCache) var template *mustache.Template if err == nil { - template = templatesCache.JavaEnumTemplate + if templateOption == "enum" { + template = templatesCache.JavaEnumTemplate + } else if templateOption == "class" { + template = templatesCache.JavaClassTemplate + } else { + err = openapi2beans_errors.NewError("GetJavaTemplate: unable to find template. Reason is: code usage error.") + } } else { err = openapi2beans_errors.NewError("Failed to read templates from embedded file. Reason is: %s", err.Error()) } diff --git a/openapi2beans/pkg/embedded/embedded_test.go b/openapi2beans/pkg/embedded/embedded_test.go index 692bfde..13ef013 100644 --- a/openapi2beans/pkg/embedded/embedded_test.go +++ b/openapi2beans/pkg/embedded/embedded_test.go @@ -34,7 +34,7 @@ func (fs *MockReadOnlyFileSystem) ReadFile(filePath string) ([]byte, error) { return []byte(content), nil } -func TestGetJavaClassReturnsTemplate(t *testing.T) { +func TestGetJavaTemplateWithClassOptionReturnsTemplate(t *testing.T) { // Given... var ( err error @@ -43,7 +43,7 @@ func TestGetJavaClassReturnsTemplate(t *testing.T) { ) // When... - template, err = GetJavaClassTemplate() + template, err = GetJavaTemplate(GET_JAVA_TEMPLATE_CLASS_OPTION) // Then... assert.Nil(t, err) @@ -54,7 +54,7 @@ func TestGetJavaClassReturnsTemplate(t *testing.T) { assert.Contains(t, rendered, "public class") } -func TestGetJavaEnumReturnsTemplate(t *testing.T) { +func TestGetJavaTemplateWithEnumOptionReturnsTemplate(t *testing.T) { // Given... var ( err error @@ -63,7 +63,7 @@ func TestGetJavaEnumReturnsTemplate(t *testing.T) { ) // When... - template, err = GetJavaEnumTemplate() + template, err = GetJavaTemplate(GET_JAVA_TEMPLATE_ENUM_OPTION) // Then... assert.Nil(t, err)