Skip to content
This repository has been archived by the owner on Sep 7, 2021. It is now read-only.

Commit

Permalink
Deprecate the module in favor of github.com/godogx/expandvars
Browse files Browse the repository at this point in the history
  • Loading branch information
nhatthm committed Sep 7, 2021
1 parent 854d027 commit 1357757
Show file tree
Hide file tree
Showing 14 changed files with 40 additions and 686 deletions.
38 changes: 0 additions & 38 deletions .github/workflows/golangci-lint.yaml

This file was deleted.

60 changes: 0 additions & 60 deletions .github/workflows/test.yaml

This file was deleted.

25 changes: 0 additions & 25 deletions Makefile

This file was deleted.

5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
> ⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️
>
> This module is deprecated. Use `github.com/godogx/expandvars` instead.
# Variables Expander for Cucumber Steps

[![GitHub Releases](https://img.shields.io/github/v/release/nhatthm/expandog)](https://github.com/nhatthm/expandog/releases/latest)
[![Build Status](https://github.com/nhatthm/expandog/actions/workflows/test.yaml/badge.svg)](https://github.com/nhatthm/expandog/actions/workflows/test.yaml)
[![codecov](https://codecov.io/gh/nhatthm/expandog/branch/master/graph/badge.svg?token=eTdAgDE2vR)](https://codecov.io/gh/nhatthm/expandog)
[![Go Report Card](https://goreportcard.com/badge/github.com/nhatthm/expandog)](https://goreportcard.com/report/github.com/nhatthm/expandog)
[![GoDevDoc](https://img.shields.io/badge/dev-doc-00ADD8?logo=go)](https://pkg.go.dev/github.com/nhatthm/expandog)
Expand Down
2 changes: 2 additions & 0 deletions doc.go
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
// Package expandog provides functionalities to expand variables in cucumber steps.
//
// Deprecated: Use github.com/godogx/expandvars instead.
package expandog
101 changes: 19 additions & 82 deletions expander.go
Original file line number Diff line number Diff line change
@@ -1,100 +1,37 @@
package expandog

import (
"errors"
"fmt"
"os"
"strings"
"github.com/godogx/expandvars"
)

const varDelimiter = "$"

// ErrUnsupportedExpander indicates that the provided expander is not supported.
var ErrUnsupportedExpander = errors.New("unsupported expander")
//
// Deprecated: Use expandvars.ErrUnsupportedExpander instead.
var ErrUnsupportedExpander = expandvars.ErrUnsupportedExpander

// EnvExpander expands variables using env vars.
var EnvExpander Expander = os.ExpandEnv
//
// Deprecated: Use expandvars.EnvExpander instead.
var EnvExpander = expandvars.EnvExpander

// Pairs is a pair of old and new to be replaced.
type Pairs = map[string]string
//
// Deprecated: Use expandvars.Pairs instead.
type Pairs = expandvars.Pairs

// Expander expands the variables in a string.
type Expander func(string) string
//
// Deprecated: Use expandvars.Expander instead.
type Expander = expandvars.Expander

// Replacer replace string.
type Replacer interface {
Replace(string) string
}
//
// Deprecated: Use expandvars.Replacer instead.
type Replacer = expandvars.Replacer

// BeforeScenario expands variables from a provider that will be called only once before every scenario.
//
// Deprecated: Use expandvars.BeforeScenario instead.
func BeforeScenario(provide func() Pairs) func() Expander {
return func() Expander {
return mapExpander(provide())
}
}

func placeholder(name string) string {
return fmt.Sprintf("%s%s", varDelimiter, name)
}

// runtimeExpander expands variables from a provider.
func runtimeExpander(provide func() Pairs) Expander {
return func(s string) string {
return mapExpander(provide())(s)
}
}

// mapExpander initiates a new variable expander from a map of values.
func mapExpander(pairs Pairs) Expander {
oldNew := make([]string, 0, 2*len(pairs))

for k, v := range pairs {
oldNew = append(oldNew, placeholder(k), v)
}

return strings.NewReplacer(oldNew...).Replace
}

func chainExpanders(expanders ...interface{}) Expander {
l := make([]Expander, 0, len(expanders))

for _, e := range expanders {
l = append(l, newExpander(e))
}

return func(s string) string {
for _, expand := range l {
s = expand(s)
}

return s
}
}

func newExpander(e interface{}) Expander {
switch e := e.(type) {
case Pairs:
return mapExpander(e)

case func() Pairs:
return runtimeExpander(e)

case func() Expander:
return e()

case Replacer:
return e.Replace

case Expander:
return e

case func(string) string:
return e
}

panic(fmt.Errorf("%w: got %T", ErrUnsupportedExpander, e))
}

func doExpand(expand Expander, s string) string {
return expand(s)
return expandvars.BeforeScenario(provide)
}
104 changes: 0 additions & 104 deletions expander_internal_test.go

This file was deleted.

Loading

0 comments on commit 1357757

Please sign in to comment.