strlang

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 4, 2023 License: MIT Imports: 2 Imported by: 0

README

strlang

🏗️ fluent string builder interface for generate code easily with go

strlang is a Go package for building string-based programming languages. It provides a simple way to generate strings of code that can be used in different programming languages.

Go Reference Go Version Latest tag Go Report Card Maintainability

🛠️ Installation

To use strlang in your Go project, you need to have Go installed on your system. Once you have Go installed, you can use the following command to install the package:

go get -u github.com/ermos/strlang

📚 Examples

Here are some examples of how to use strlang to generate code in different programming languages. If your wanted programming language isn't implemented, you can use directly the default builder.

Javascript

Here is an example of how to use Strlang to generate JavaScript code:

Code
package main

import (
	"github.com/ermos/strlang"
	"fmt"
)

func main() {
	js := strlang.NewJavascript()

	js.Object("const", "person", func() {
		js.WriteStringln(`name: "John",`)
		js.WriteStringln(`age: 31,`)
		js.WriteStringln(`city: "New York"`)
	})

	js.If("person.age > 18", func() {
		js.WriteStringln(`console.log("John is an adult");`)
	})
	js.Else(func() {
		js.WriteStringln("console.log('John is not an adult');")
	})
	
	fmt.Println(js.String())
}
Output
const person = {
    name: "John",
    age: 31,
    city: "New York"
};

if (person.age > 18) {
    console.log("John is an adult");
} else {
    console.log("John is not an adult");
}
PHP

Here is an example of how to use Strlang to generate PHP code:

Code
package main

import (
	"github.com/ermos/strlang"
	"fmt"
)

func main() {
	php := strlang.NewPHP("App/Models")

	php.Class("Person", func() {
		php.ClassFunc("public", "__construct", "$name, $age", "", func() {
			php.WriteStringln(`$this->name = $name;`)
			php.WriteStringln(`$this->age = $age;`)
		})
	})

	fmt.Println(php.String())
}
Output
<?php

namespace MyNamespace;

class Person {
    public function __construct($name, $age) {
        $this->name = $name;
        $this->age = $age;
    }
}

💡 Usage

strlang provides a set of builders for each programming language. Each builder has methods that allow you to build code constructs in that language. You can use the New method of your wanted language (example for javascript: NewJavascript()) functions to create instances of the corresponding builders. Once you have a builder instance, you can use its methods to build code constructs.

For more information about the available methods and how to use them, please refer to the package documentation.

🤝 Contributing

Contributions to strlang are always welcome! If you find a bug or have a feature request, please open an issue on GitHub. If you want to contribute code, please fork the repository and submit a pull request.

Documentation

Overview

Package strlang provides a string builder with indentation support, it allows you to generate code using a fluent string builder interface.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Builder

type Builder struct {
	// contains filtered or unexported fields
}

Builder is the base builder of the strlang package

func NewBuilder

func NewBuilder() *Builder

NewBuilder returns a new instance of Builder with default settings: an empty strings.Builder and an indentation character of two spaces.

func (*Builder) Block

func (b *Builder) Block(start string, f func(), end string, ln ...int)

Block writes the start string, indents the builder, calls the provided function f, strips the indent, and writes the end string with a newline character. Optionally, it can write additional newline characters with the ln argument.

func (*Builder) Indent

func (b *Builder) Indent(nb ...int)

Indent increases the current indentation level by the number of spaces passed as an optional argument. The default indentation increase is two spaces.

func (*Builder) SetIndentChar

func (b *Builder) SetIndentChar(s string)

SetIndentChar sets the indentation character for the builder. The default indentation character is one tab.

func (*Builder) String

func (b *Builder) String() string

String returns the resulting string built by the builder.

func (*Builder) StripIndent

func (b *Builder) StripIndent(nb ...int)

StripIndent decreases the current indentation level by the number of spaces passed as an optional argument. The default indentation decrease is two spaces.

func (*Builder) TrimLeft

func (b *Builder) TrimLeft(cutset string)

TrimLeft trims the resulting string from the builder by removing characters from the left side of the string that are contained in the provided cutset string.

func (*Builder) TrimRight

func (b *Builder) TrimRight(cutset string)

TrimRight trims the resulting string from the builder by removing characters from the right side of the string that are contained in the provided cutset string.

func (*Builder) Write

func (b *Builder) Write(p []byte)

Write writes a byte slice to the builder with indentation. It adds the current indentation characters before the provided byte slice.

func (*Builder) WriteNoIdentString

func (b *Builder) WriteNoIdentString(s string)

WriteNoIdentString writes a string to the builder without indentation.

func (*Builder) WriteString

func (b *Builder) WriteString(s string)

WriteString writes a string to the builder without adding a newline character.

func (*Builder) WriteStringln

func (b *Builder) WriteStringln(s string, nb ...int)

WriteStringln writes a string with a newline character to the builder. Optionally, it can write multiple newline characters by passing an integer argument.

type Golang

type Golang struct {

	// Builder is an embedded struct that represents
	// the base builder for generating the Go code.
	*Builder
	// contains filtered or unexported fields
}

Golang represents a Golang code generator that extends the Builder struct.

func NewGolang

func NewGolang(packageName string) *Golang

NewGolang returns a new instance of Golang code builder.

func (*Golang) AddImports

func (b *Golang) AddImports(imports ...string)

AddImports adds import statements to the generated code.

func (*Golang) Else

func (b *Golang) Else(inside func(), ln ...int)

Else generates an else statement in the generated code.

func (*Golang) ElseIf

func (b *Golang) ElseIf(statement string, inside func(), ln ...int)

ElseIf generates an else if statement in the generated code.

func (*Golang) Func

func (b *Golang) Func(fromStruct, name, parameters, output string, inside func())

Func generates a function definition in the generated code.

func (*Golang) If

func (b *Golang) If(statement string, inside func(), ln ...int)

If generates an if statement in the generated code.

func (*Golang) String

func (b *Golang) String() string

String returns the generated code as a string.

func (*Golang) Struct

func (b *Golang) Struct(name string, inside func())

Struct generates a struct definition in the generated code.

func (*Golang) StructField

func (b *Golang) StructField(name, goType string, docs ...map[string]string)

StructField generates a struct field definition in the generated code.

type Javascript

type Javascript struct {
	// Builder is an embedded struct that represents
	// the base builder for generating the Javascript code.
	*Builder
}

Javascript represents a JavaScript code generator that extends the Builder struct.

func NewJavascript

func NewJavascript() *Javascript

NewJavascript returns a new instance of Javascript code builder.

func (*Javascript) Else

func (b *Javascript) Else(inside func())

Else generates an else statement in the generated code.

func (*Javascript) ElseIf

func (b *Javascript) ElseIf(statement string, inside func())

ElseIf generates an else if statement in the generated code.

func (*Javascript) Export

func (b *Javascript) Export() *Javascript

Export appends the "export" keyword and allows to define an exportable object or variable.

func (*Javascript) If

func (b *Javascript) If(statement string, inside func())

If generates an if statement in the generated code.

func (*Javascript) Object

func (b *Javascript) Object(varType, name string, inside func())

Object generates an object block with the provided variable type, name, and inside function.

type PHP

type PHP struct {

	// Builder is an embedded struct that represents
	// the base builder for generating the PHP code.
	*Builder
	// contains filtered or unexported fields
}

PHP represents a PHP code generator that extends the Builder struct.

func NewPHP

func NewPHP(namespace string) *PHP

NewPHP returns a new instance of PHP code builder.

func (*PHP) Class

func (b *PHP) Class(name string, inside func())

Class generates a class definition in the generated code.

func (*PHP) ClassFunc

func (b *PHP) ClassFunc(modifiers, name, parameters, output string, inside func())

ClassFunc generates class method inside a class in the generated code.

func (*PHP) Else

func (b *PHP) Else(inside func())

Else generates an else statement in the generated code.

func (*PHP) ElseIf

func (b *PHP) ElseIf(statement string, inside func())

ElseIf generates an else if statement in the generated code.

func (*PHP) If

func (b *PHP) If(statement string, inside func(), ln ...int)

If generates an if statement in the generated code.

func (*PHP) String

func (b *PHP) String() string

String returns the generated code as a string.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL