mapper

package module
v0.0.0-...-fb54b78 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2024 License: MIT Imports: 9 Imported by: 2

README

mapper

Build Status | codecov | Go Report Card | GoDoc

Translates any struct to other data types.

If i miss something or you have something interesting, please be part of this project. Let me know! My contact is at the end.

Convertions

  • to map with key = path and value = the value
  • to string with path = value

Dependecy Management

Dep

Project dependencies are managed using Dep. Read more about Dep.

  • Install dependencies: dep ensure
  • Update dependencies: dep ensure -update
Go
go get github.com/joaosoft/mapper

Usage

This examples are available in the project at mapper/example

type First struct {
	One   string            `json:"one"`
	Two   int               `json:"two"`
	Three map[string]string `json:"three"`
	Four  Four              `json:"four"`
	Seven []string          `json:"seven"`
	Eight []Four            `json:"eight"`
}

type Four struct {
	Five string `json:"five"`
	Six  int    `json:"six"`
}

type Second struct {
	Eight []Four          `json:"eight"`
	Nine  map[Four]Second `json:"nine"`
}

obj1 := First{
    One:   "one",
    Two:   2,
    Three: map[string]string{"a": "1", "b": "2"},
    Four: Four{
        Five: "five",
        Six:  6,
    },
    Seven: []string{"a", "b", "c"},
    Eight: []Four{Four{Five: "5", Six: 66}},
}
obj2 := Second{
    Eight: []Four{Four{Five: "5", Six: 66}},
    Nine:  map[Four]Second{Four{Five: "111", Six: 1}: Second{Eight: []Four{Four{Five: "222", Six: 2}}}},
}
Convert struct to string
fmt.Println(":::::::::::: STRUCT ONE")
m, := mapper.NewMapper(mapper.WithLogger(log))
if translated, err := m.String(obj1); err != nil {
    log.Error("error on translation!")
} else {
    fmt.Println(translated)
}

fmt.Println(":::::::::::: STRUCT TWO")
if translated, err := m.String(obj2); err != nil {
    log.Error("error on translation!")
} else {
    fmt.Println(translated)
}
Result:
:::::::::::: STRUCT ONE
One: one
Two: 2
Three
  {a}: 1
  {b}: 2
Four
  Five: five
  Six: 6
Seven
  [0]: a
  [1]: b
  [2]: c
Eight
  [0]
    Five: 5
    Six: 66

:::::::::::: STRUCT TWO
Eight
  [0]
    Five: 5
    Six: 66
Nine
  {Five: 111 Six: 1}
    Eight
      [0]
        Five: 222
        Six: 2
    Nine

:::::::::::: JSON STRING OF STRUCT ONE
{seven}
  [0]: a
  [1]: b
  [2]: c
{eight}
  [0]
    {five}: 5
    {six}: 66
{one}: one
{two}: 2
{three}
  {a}: 1
  {b}: 2
{four}
  {five}: five
  {six}: 6
Convert struct to map
fmt.Println(":::::::::::: STRUCT ONE")
m := mapper.NewMapper(mapper.WithLogger(log))
if translated, err := m.Map(obj1); err != nil {
    log.Error("error on translation!")
} else {
    for key, value := range translated {
        fmt.Printf("%s: %+v\n", key, value)
    }
}

fmt.Println(":::::::::::: STRUCT TWO")
if translated, err := m.Map(obj2); err != nil {
    log.Error("error on translation!")
} else {
    for key, value := range translated {
        fmt.Printf("%s: %+v\n", key, value)
    }
}
Result:
:::::::::::: STRUCT ONE
Four.Six: 6
Seven.[0]: a
Seven.[1]: b
Seven.[2]: c
Eight.[0].Six: 66
One: one
Four.Five: five
Three.{b}: 2
Eight.[0].Five: 5
Two: 2
Three.{a}: 1

:::::::::::: STRUCT TWO
Eight.[0].Five: 5
Eight.[0].Six: 66
Nine.{Five=111,Six=1}.Eight.[0].Five: 222
Nine.{Five=111,Six=1}.Eight.[0].Six: 2

:::::::::::: JSON STRING OF STRUCT ONE
{four}.{five}: five
{seven}.[0]: a
{seven}.[1]: b
{two}: 2
{three}.{b}: 2
{one}: one
{three}.{a}: 1
{four}.{six}: 6
{seven}.[2]: c
{eight}.[0].{five}: 5
{eight}.[0].{six}: 66

Known issues

Follow me at

Facebook: https://www.facebook.com/joaosoft

LinkedIn: https://www.linkedin.com/in/jo%C3%A3o-ribeiro-b2775438/

If you have something to add, please let me know [email protected]

Documentation

Overview

Mapper helps to create integration test in a easy way

you just need to define the test structure and run it.

examples at https://github.com/joaosoft/go-mapper/tree/master/example

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Exists

func Exists(file string) bool

func GetEnv

func GetEnv() string

func ReadFile

func ReadFile(fileName string, obj interface{}) ([]byte, error)

func ReadFileLines

func ReadFileLines(fileName string) ([]string, error)

func WriteFile

func WriteFile(fileName string, obj interface{}) error

Types

type AppConfig

type AppConfig struct {
	Mapper *MapperConfig `json:"mapper"`
}

AppConfig ...

func NewConfig

func NewConfig() (*AppConfig, manager.IConfig, error)

NewConfig ...

type Mapper

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

Mapper ...

func NewMapper

func NewMapper(options ...MapperOption) *Mapper

NewMapper ...

func (*Mapper) Map

func (mapper *Mapper) Map(value interface{}) (map[string]interface{}, error)

Map ...

func (*Mapper) Reconfigure

func (mapper *Mapper) Reconfigure(options ...MapperOption)

Reconfigure ...

func (*Mapper) String

func (mapper *Mapper) String(value interface{}) (string, error)

String ...

type MapperConfig

type MapperConfig struct {
	Log struct {
		Level string `json:"level"`
	} `json:"log"`
}

MapperConfig ...

type MapperOption

type MapperOption func(mapper *Mapper)

MapperOption ...

func WithLogLevel

func WithLogLevel(level logger.Level) MapperOption

WithLogLevel ...

func WithLogger

func WithLogger(logger logger.ILogger) MapperOption

WithLogger ...

func WithManager

func WithManager(mgr *manager.Manager) MapperOption

WithManager ...

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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