Skip to content

Commit

Permalink
Merge pull request #3 from k0sproject/modernize
Browse files Browse the repository at this point in the history
Modernize
  • Loading branch information
kke authored Oct 24, 2023
2 parents bcae5d9 + c97c8bd commit 912cb53
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 161 deletions.
40 changes: 12 additions & 28 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -1,41 +1,25 @@
name: Go

on: [push, pull_request]
on: [pull_request]

jobs:

build:
go:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v2
if: github.ref != 'refs/heads/main'
with:
go-version: 1.15

- name: Go modules cache
uses: actions/cache@v2
if: github.ref != 'refs/heads/main'
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Run golangci-lint
uses: actions/setup-go@v4
if: github.ref != 'refs/heads/main'
uses: golangci/golangci-lint-action@v2.3.0
with:
version: v1.35.2
skip-go-installation: true

- name: Build
if: github.ref != 'refs/heads/main'
run: go build -v ./...

go-version-file: go.mod
check-latest: true

- name: Test
if: github.ref != 'refs/heads/main'
run: go test -v ./...

- name: Run golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: latest
args: --verbose
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Dig

A go package that provides a Ruby-like `Hash.dig` mechanism for `map[string]interface{}`, which in YAML terminology is refered to as "Mapping".
A simple zero-dependency go package that provides a Ruby-like `Hash.dig` mechanism for `map[string]any`, which in YAML is refered to as "Mapping".

## Usage

Dig's Mapping is useful for example to use as the Unmarshal target of arbitrary YAML/JSON documents.
The provided `dig.Mapping` is handy when unmarshaling arbitrary YAML/JSON documents.

### Example
```go
Expand All @@ -21,7 +21,7 @@ var yamlDoc = []byte(`---
i18n:
hello:
se: Hejsan
fi: Morjens
fi: Moi
world:
se: Värld
fi: Maailma
Expand Down Expand Up @@ -55,7 +55,7 @@ func main() {
Output:

```
Morjens, Maailma!
Moi, Maailma!
Hejsan, Värld!
Hola, Mundo!
```
40 changes: 0 additions & 40 deletions examples/readme.go

This file was deleted.

7 changes: 1 addition & 6 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
module github.com/k0sproject/dig

go 1.15

require (
github.com/stretchr/testify v1.7.0
gopkg.in/yaml.v2 v2.4.0
)
go 1.21
13 changes: 0 additions & 13 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,13 +0,0 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
26 changes: 13 additions & 13 deletions mapping.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
// Package dig provides a map[string]interface{} Mapping type that has ruby-like "dig" functionality.
// Package dig provides a map[string]any Mapping type that has ruby-like "dig" functionality.
//
// It can be used for example to access and manipulate arbitary nested YAML/JSON structures.
// It can be used for example to access and manipulate arbitrary nested YAML/JSON structures.
package dig

import "fmt"

// Mapping is a nested key-value map where the keys are strings and values are interface{}. In Ruby it is called a Hash (with string keys), in YAML it's called a "mapping".
type Mapping map[string]interface{}
// Mapping is a nested key-value map where the keys are strings and values are any. In Ruby it is called a Hash (with string keys), in YAML it's called a "mapping".
type Mapping map[string]any

// UnmarshalYAML for supporting yaml.Unmarshal
func (m *Mapping) UnmarshalYAML(unmarshal func(interface{}) error) error {
var result map[interface{}]interface{}
func (m *Mapping) UnmarshalYAML(unmarshal func(any) error) error {
var result map[string]any
if err := unmarshal(&result); err != nil {
return err
}
Expand All @@ -21,7 +21,7 @@ func (m *Mapping) UnmarshalYAML(unmarshal func(interface{}) error) error {
// Dig is a simplistic implementation of a Ruby-like Hash.dig functionality.
//
// It returns a value from a (deeply) nested tree structure.
func (m *Mapping) Dig(keys ...string) interface{} {
func (m *Mapping) Dig(keys ...string) any {
v, ok := (*m)[keys[0]]
if !ok {
return nil
Expand Down Expand Up @@ -111,16 +111,16 @@ func (m *Mapping) Dup() Mapping {
}

// Cleans up a slice of interfaces into slice of actual values
func cleanUpInterfaceArray(in []interface{}) []interface{} {
result := make([]interface{}, len(in))
func cleanUpInterfaceArray(in []any) []any {
result := make([]any, len(in))
for i, v := range in {
result[i] = cleanUpMapValue(v)
}
return result
}

// Cleans up the map keys to be strings
func cleanUpInterfaceMap(in map[interface{}]interface{}) Mapping {
func cleanUpInterfaceMap(in map[string]any) Mapping {
result := make(Mapping)
for k, v := range in {
result[fmt.Sprintf("%v", k)] = cleanUpMapValue(v)
Expand All @@ -129,11 +129,11 @@ func cleanUpInterfaceMap(in map[interface{}]interface{}) Mapping {
}

// Cleans up the value in the map, recurses in case of arrays and maps
func cleanUpMapValue(v interface{}) interface{} {
func cleanUpMapValue(v any) any {
switch v := v.(type) {
case []interface{}:
case []any:
return cleanUpInterfaceArray(v)
case map[interface{}]interface{}:
case map[string]any:
return cleanUpInterfaceMap(v)
case string, int, bool, nil:
return v
Expand Down
Loading

0 comments on commit 912cb53

Please # to comment.