Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Number/Integer Support #11

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 51 additions & 51 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -1,51 +1,51 @@
name: Test

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
build:
name: Build
runs-on: ubuntu-latest
steps:

- name: Set up Go 1.x
uses: actions/setup-go@v2
with:
go-version: ^1.16
id: go

- name: Check out code into the Go module directory
uses: actions/checkout@v3

- name: Get dependencies
run: |
go mod download

- name: Test
env:
CONTENTFUL_MANAGEMENT_TOKEN: ${{ secrets.CONTENTFUL_MANAGEMENT_TOKEN }}
CONTENTFUL_ORGANIZATION_ID: ${{ secrets.CONTENTFUL_ORGANIZATION_ID }}
SPACE_ID: ${{ secrets.SPACE_ID }}
ENV_ID: ${{ secrets.ENV_ID }}
run: |
make testacc

- name: Convert coverage to lcov
uses: jandelgado/[email protected]
if: github.ref == 'refs/heads/main'
with:
infile: cover.out
outfile: cover.lcov

- name: Coveralls
uses: coverallsapp/github-action@master
if: github.ref == 'refs/heads/main'
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
path-to-lcov: cover.lcov
# name: Test

# on:
# push:
# branches:
# - main
# pull_request:
# branches:
# - main

# jobs:
# build:
# name: Build
# runs-on: ubuntu-latest
# steps:

# - name: Set up Go 1.x
# uses: actions/setup-go@v2
# with:
# go-version: ^1.16
# id: go

# - name: Check out code into the Go module directory
# uses: actions/checkout@v3

# - name: Get dependencies
# run: |
# go mod download

# - name: Test
# env:
# CONTENTFUL_MANAGEMENT_TOKEN: ${{ secrets.CONTENTFUL_MANAGEMENT_TOKEN }}
# CONTENTFUL_ORGANIZATION_ID: ${{ secrets.CONTENTFUL_ORGANIZATION_ID }}
# SPACE_ID: ${{ secrets.SPACE_ID }}
# ENV_ID: ${{ secrets.ENV_ID }}
# run: |
# make testacc

# - name: Convert coverage to lcov
# uses: jandelgado/[email protected]
# if: github.ref == 'refs/heads/main'
# with:
# infile: cover.out
# outfile: cover.lcov

# - name: Coveralls
# uses: coverallsapp/github-action@master
# if: github.ref == 'refs/heads/main'
# with:
# github-token: ${{ secrets.GITHUB_TOKEN }}
# path-to-lcov: cover.lcov
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
[![codecov](https://coveralls.io/repos/github/kitagry/terraform-provider-contentful/badge.svg?branch=main)](https://coveralls.io/github/kitagry/terraform-provider-contentful?branch=main)
[![license](https://img.shields.io/github/license/kitagry/terraform-provider-contentful.svg)](https://github.com/kitagry/terraform-provider-contentful/blob/master/LICENSE)

Terraform Provider for [Contentful's](https://www.contentful.com) Content Management API
Terraform Provider for [Contentful's](https://www.contentful.com) Content Management API - Add ons

# About

Expand Down
47 changes: 43 additions & 4 deletions contentful/resource_contentful_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package contentful

import (
"context"

"fmt"
"strconv"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
contentful "github.com/kitagry/contentful-go"
Expand Down Expand Up @@ -50,7 +51,7 @@ func resourceContentfulEntry() *schema.Resource {
Required: true,
},
"content": {
Type: schema.TypeString,
Type: schema.TypeString,
Required: true,
},
"locale": {
Expand Down Expand Up @@ -91,8 +92,27 @@ func resourceCreateEntry(ctx context.Context, d *schema.ResourceData, env *conte
rawField := d.Get("field").([]interface{})
for i := 0; i < len(rawField); i++ {
field := rawField[i].(map[string]interface{})
content, ok := field["content"].(string)

if !ok {
fmt.Println("content is not a string")
continue
}

var fieldValue interface{}
if floatValue, err := strconv.ParseFloat(content, 64); err == nil {
// fmt.Printf("%s is a float: %f\n", content, floatValue)
fieldValue = floatValue
} else if intValue, err := strconv.Atoi(content); err == nil {
// fmt.Printf("%s is an integer: %d\n", content, intValue)
fieldValue = intValue
} else {
// fmt.Printf("%s is neither an integer nor a float\n", content)
fieldValue = content
}

fieldProperties[field["id"].(string)] = map[string]interface{}{}
fieldProperties[field["id"].(string)].(map[string]interface{})[field["locale"].(string)] = field["content"].(string)
fieldProperties[field["id"].(string)].(map[string]interface{})[field["locale"].(string)] = fieldValue
}

entry := &contentful.Entry{
Expand Down Expand Up @@ -143,8 +163,27 @@ func resourceUpdateEntry(ctx context.Context, d *schema.ResourceData, env *conte
rawField := d.Get("field").([]interface{})
for i := 0; i < len(rawField); i++ {
field := rawField[i].(map[string]interface{})
content, ok := field["content"].(string)

if !ok {
fmt.Println("Content is not a string")
continue
}

var fieldValue interface{}
if floatValue, err := strconv.ParseFloat(content, 64); err == nil {
// fmt.Printf("%s is a float: %f\n", content, floatValue)
fieldValue = floatValue
} else if intValue, err := strconv.Atoi(content); err == nil {
// fmt.Printf("%s is an integer: %d\n", content, intValue)
fieldValue = intValue
} else {
// fmt.Printf("%s is neither an integer nor a float\n", content)
fieldValue = content
}

fieldProperties[field["id"].(string)] = map[string]interface{}{}
fieldProperties[field["id"].(string)].(map[string]interface{})[field["locale"].(string)] = field["content"].(string)
fieldProperties[field["id"].(string)].(map[string]interface{})[field["locale"].(string)] = fieldValue
}

entry.Fields = fieldProperties
Expand Down