Help us shape the future of Go! Take the 2026 Go Developer Survey

gosimplifier

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: May 28, 2023 License: Apache-2.0 Imports: 3 Imported by: 1

README

Go Simplifier

This Go package simplifies your data structures according to the rules you define. It's perfect for reducing complex structures to just the data you need, providing a cleaner, simplified view of your data. The Go Simplifier is especially useful when dealing with large data structures where only a subset of the fields are required.

Installation

Install Go Simplifier with:

go get github.com/xhinliang/gosimplifier

Make sure to replace xhinliang with your GitHub username.

Usage

Here is a simple example of how to use Go Simplifier:

package main

import (
	"fmt"
	"github.com/xhinliang/gosimplifier"
)

type MyStruct struct {
	Field1 int
	Field2 string
}

func main() {
	rulesJson := `{
		"remove_properties": ["Field1"]
	}`

	simplifier, err := gosimplifier.NewSimplifier(rulesJson)
	if err != nil {
		// handle error
	}

	original := MyStruct{
		Field1: 1,
		Field2: "data",
	}

	simplified, err := simplifier.Simplify(original)
	if err != nil {
		// handle error
	}

	fmt.Printf("%v\n", simplified)
}

This will output:

{  data}

You can also use NewSimplifierByRule to create a simplifier from a Rule struct:

rule := &gosimplifier.Rule{
	RemoveProperties:    []string{"Field1"},
	PropertySimplifiers: map[string]*gosimplifier.Rule{
        
    },
}

simplifier, err := gosimplifier.NewSimplifierByRule(rule)
// ...

License

This project is licensed under the terms of the Apache 2.0 license. For more information, please see the LICENSE file.

Contributing

Contributions to Go Simplifier are very welcome! Please submit a pull request or create an issue on the GitHub repository.

Testing

Go Simplifier includes a suite of unit tests. To run them, use go test:

go test ./...

Contact

If you encounter any issues or have questions about Go Simplifier, feel free to reach out via GitHub issues.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewSimplifierByRule

func NewSimplifierByRule(rule *Rule) (*simplifierImpl, error)

NewSimplifierByRule creates a new instance of simplifierImpl with the given rule

Types

type Rule

type Rule struct {
	RemoveProperties    []string         `json:"remove_properties"`
	PropertySimplifiers map[string]*Rule `json:"property_simplifiers"`
}

Rule defines the rule structure for property removal and nested property rules.

type Simplifier

type Simplifier interface {
	// Simplify method:
	// 1. Receives any type of struct or pointer to it, returns the same type of struct(pointer)
	// 2. Will not modify the original, but just make a copy as the return value
	// 3. Removes the properties of the return value according to the rules
	Simplify(original interface{}) (interface{}, error)
}

Simplifier defines the interface for struct simplification.

func ExtendSimplifier

func ExtendSimplifier(base Simplifier, rulesJson string) (Simplifier, error)

ExtendSimplifier extends the base simplifier with the given rules. The new Simplifier will have the rules merge from the base and the given rules.

func ExtendSimplifierByRule

func ExtendSimplifierByRule(baseImpl *simplifierImpl, newRule *Rule) (Simplifier, error)

func NewSimplifier

func NewSimplifier(rulesJson string) (Simplifier, error)

NewSimplifier creates a new instance of simplifierImpl with the given rules

Example:

{
 "remove_properties": [ "field1"],
 "property_simplifiers": {
   "field2": {
     "remove_properties": [ "sub2" ]
     "property_simplifiers": {
       "sub1": {
         "remove_properties": [ "a", "b" ]
       }
     }
   }
 }
}

For struct instance root:

var root ExampleStruct

The following properties will be removed:

root.field1
root.field2.sub2
root.field2.sub1.a
root.field2.sub1.b

Jump to

Keyboard shortcuts

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